[common] rb: correct invalid accesses of atomics

This commit is contained in:
Geoffrey McRae 2022-01-27 18:20:03 +11:00
parent 2201ed869e
commit 4ff39616b2

View File

@ -30,8 +30,8 @@ struct RingBuffer
{ {
uint32_t length; uint32_t length;
uint32_t valueSize; uint32_t valueSize;
uint32_t readPos; _Atomic(uint32_t) readPos;
uint32_t writePos; _Atomic(uint32_t) writePos;
bool unbounded; bool unbounded;
char values[0]; char values[0];
}; };
@ -44,8 +44,8 @@ RingBuffer ringbuffer_newInternal(int length, size_t valueSize,
struct RingBuffer * rb = calloc(1, sizeof(*rb) + valueSize * length); struct RingBuffer * rb = calloc(1, sizeof(*rb) + valueSize * length);
rb->length = length; rb->length = length;
rb->valueSize = valueSize; rb->valueSize = valueSize;
rb->readPos = 0; atomic_store(&rb->readPos , 0);
rb->writePos = 0; atomic_store(&rb->writePos, 0);
rb->unbounded = unbounded; rb->unbounded = unbounded;
return rb; return rb;
} }
@ -116,7 +116,7 @@ int ringbuffer_append(const RingBuffer rb, const void * values, int count)
return 0; return 0;
uint32_t readPos = atomic_load_explicit(&rb->readPos, memory_order_acquire); uint32_t readPos = atomic_load_explicit(&rb->readPos, memory_order_acquire);
uint32_t writePos = rb->writePos; uint32_t writePos = atomic_load_explicit(&rb->writePos, memory_order_relaxed);
uint32_t newWritePos = writePos; uint32_t newWritePos = writePos;
if (count < 0) if (count < 0)
@ -196,7 +196,7 @@ int ringbuffer_consume(const RingBuffer rb, void * values, int count)
if (count < 0 && !rb->unbounded) if (count < 0 && !rb->unbounded)
return 0; return 0;
uint32_t readPos = rb->readPos; uint32_t readPos = atomic_load_explicit(&rb->readPos, memory_order_relaxed);
uint32_t writePos = atomic_load_explicit(&rb->writePos, memory_order_acquire); uint32_t writePos = atomic_load_explicit(&rb->writePos, memory_order_acquire);
uint32_t newReadPos = readPos; uint32_t newReadPos = readPos;
@ -263,7 +263,7 @@ int ringbuffer_consume(const RingBuffer rb, void * values, int count)
void ringbuffer_forEach(const RingBuffer rb, RingBufferIterator fn, void ringbuffer_forEach(const RingBuffer rb, RingBufferIterator fn,
void * udata, bool reverse) void * udata, bool reverse)
{ {
uint32_t readPos = rb->readPos; uint32_t readPos = atomic_load_explicit(&rb->readPos, memory_order_relaxed);
uint32_t writePos = atomic_load_explicit(&rb->writePos, memory_order_acquire); uint32_t writePos = atomic_load_explicit(&rb->writePos, memory_order_acquire);
int32_t writeOffset = writePos - readPos; int32_t writeOffset = writePos - readPos;