Esempio n. 1
0
void async_file::write_cycle()
{
  int read_bs = 0;

  lock();
  read_bs = get_read_bs();
  unlock();

  while(!error && (read_bs > 0)) {

    int bytes = write_to_file(get_read_ptr(),read_bs);
    if(bytes < 0) {
      error  = true;
      ERROR("Error detected: stopped writing");
      break;
    }

    lock();
    skip(bytes);
    read_bs = get_read_bs();
    unlock();
  }

  lock();
  if(closed) {
    if(error || !fifo_buffer::get_buffered_bytes())
      on_flushed();
    else
      event_active(ev_write, 0, 0);
  }
  unlock();
}
Esempio n. 2
0
int rbuf::read(void* p, int size)
{
	void *q = NULL;
	int newsize = get_read_ptr(&q, size);

	/*  newsize will never be < 0, but this should satisfy the coverity checker */
	if (newsize > 0)
		memcpy(p, q, newsize);

	put_read_ptr(newsize);
	return newsize;
}
Esempio n. 3
0
static int parse_list (int *Res, int max_size, netbuffer_t *In, int bytes) {
    char *ptr = 0, *ptr_e = 0;
    int r = 0, s = 0, x;
    if (!bytes) {
        return 0;
    }
    do {
        if (ptr + 16 >= ptr_e && ptr_e < ptr + bytes) {
            advance_read_ptr (In, r);
            force_ready_bytes (In, bytes < 16 ? bytes : 16);
            ptr = get_read_ptr (In);
            r = get_ready_bytes (In);
            if (r > bytes) {
                r = bytes;
            }
            ptr_e = ptr + r;
            r = 0;
        }
        assert (ptr < ptr_e);
        x = 0;
        while (ptr < ptr_e && *ptr >= '0' && *ptr <= '9') {
            if (x >= 0x7fffffff / 10) {
                return -1;
            }
            x = x*10 + (*ptr++ - '0');
            r++;
            bytes--;
        }
        if (s >= max_size || (bytes > 0 && (ptr == ptr_e || *ptr != ','))) {
            advance_skip_read_ptr (In, r + bytes);
            return -1;
        }
        Res[s++] = x;
        if (!bytes) {
            advance_read_ptr (In, r);
            return s;
        }
        assert (*ptr == ',');
        ptr++;
        r++;
    } while (--bytes > 0);
    assert (!bytes);
    advance_read_ptr (In, r);
    return s;
}
Esempio n. 4
0
void LogWriter::run()
{
	while (!_exit_thread) {
		// Outer endless loop
		// Wait for _should_run flag
		while (!_exit_thread) {
			bool start = false;
			pthread_mutex_lock(&_mtx);
			pthread_cond_wait(&_cv, &_mtx);
			start = _should_run;
			pthread_mutex_unlock(&_mtx);

			if (start) {
				break;
			}
		}

		int poll_count = 0;
		int written = 0;

		while (true) {
			size_t available = 0;
			void *read_ptr = nullptr;
			bool is_part = false;

			/* lock _buffer
			 * wait for sufficient data, cycle on notify()
			 */
			pthread_mutex_lock(&_mtx);

			while (true) {
				available = get_read_ptr(&read_ptr, &is_part);

				/* if sufficient data available or partial read or terminating, exit this wait loop */
				if ((available >= _min_write_chunk) || is_part || !_should_run) {
					/* GOTO end of block */
					break;
				}

				/* wait for a call to notify()
				 * this call unlocks the mutex while waiting, and returns with the mutex locked
				 */
				pthread_cond_wait(&_cv, &_mtx);
			}

			pthread_mutex_unlock(&_mtx);
			written = 0;

			if (available > 0) {
				perf_begin(_perf_write);
				written = ::write(_fd, read_ptr, available);
				perf_end(_perf_write);

				/* call fsync periodically to minimize potential loss of data */
				if (++poll_count >= 100) {
					perf_begin(_perf_fsync);
					::fsync(_fd);
					perf_end(_perf_fsync);
					poll_count = 0;
				}

				if (written < 0) {
					PX4_WARN("error writing log file");
					_should_run = false;
					/* GOTO end of block */
					break;
				}

				pthread_mutex_lock(&_mtx);
				/* subtract bytes written from number in _buffer (_count -= written) */
				mark_read(written);
				pthread_mutex_unlock(&_mtx);

				_total_written += written;
			}

			if (!_should_run && written == static_cast<int>(available) && !is_part) {
				// Stop only when all data written
				_running = false;
				_head = 0;
				_count = 0;

				if (_fd >= 0) {
					int res = ::close(_fd);
					_fd = -1;

					if (res) {
						PX4_WARN("error closing log file");

					} else {
						PX4_INFO("closed logfile: %s, bytes written: %zu", _filename, _total_written);
					}
				}

				break;
			}
		}
	}
}