diff --git a/libraries/AP_HAL/utility/RingBuffer.cpp b/libraries/AP_HAL/utility/RingBuffer.cpp index 51ad08f97d..2c3ea87b02 100644 --- a/libraries/AP_HAL/utility/RingBuffer.cpp +++ b/libraries/AP_HAL/utility/RingBuffer.cpp @@ -36,8 +36,14 @@ bool ByteBuffer::set_size(uint32_t _size) uint32_t ByteBuffer::available(void) const { - uint32_t _tail; - return ((head > (_tail=tail))? (size - head) + _tail: _tail - head); + /* use a copy on stack to avoid race conditions of @tail being updated by + * the writer thread */ + uint32_t _tail = tail; + + if (head > _tail) { + return size - head + _tail; + } + return _tail - head; } void ByteBuffer::clear(void) @@ -47,8 +53,22 @@ void ByteBuffer::clear(void) uint32_t ByteBuffer::space(void) const { + if (size == 0) { + return 0; + } + + /* use a copy on stack to avoid race conditions of @head being updated by + * the reader thread */ uint32_t _head = head; - return size ? (_head > tail ? 0 : size) + _head - tail - 1 : 0; + uint32_t ret = 0; + + if (_head <= tail) { + ret = size; + } + + ret += _head - tail - 1; + + return ret; } bool ByteBuffer::empty(void) const