lib: os: ring_buffer: Fix race condition

Ring buffer claims that no synchronization is needed
when there is a single producer and single consumer.
However, recent changes have broken that promise since
indexes rewind mechanism was modifing head and tail
when consuming. Patch fixes that by spliting rewinding
of indexes so that producer rewinds tail only and
consumer rewinds head.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
This commit is contained in:
Krzysztof Chruscinski 2021-07-30 08:29:48 +02:00 committed by Carles Cufí
commit 58942f3f13
2 changed files with 87 additions and 58 deletions

View file

@ -174,10 +174,7 @@ static inline void ring_buf_init(struct ring_buf *buf,
*
* @return 1 if the ring buffer is empty, or 0 if not.
*/
static inline int ring_buf_is_empty(struct ring_buf *buf)
{
return (buf->head == buf->tail);
}
int ring_buf_is_empty(struct ring_buf *buf);
/**
* @brief Reset ring buffer state.
@ -198,10 +195,7 @@ static inline void ring_buf_reset(struct ring_buf *buf)
*
* @return Ring buffer free space (in 32-bit words or bytes).
*/
static inline uint32_t ring_buf_space_get(struct ring_buf *buf)
{
return buf->size - (buf->tail - buf->head);
}
uint32_t ring_buf_space_get(struct ring_buf *buf);
/**
* @brief Return ring buffer capacity.
@ -222,10 +216,7 @@ static inline uint32_t ring_buf_capacity_get(struct ring_buf *buf)
*
* @return Ring buffer space used (in 32-bit words or bytes).
*/
static inline uint32_t ring_buf_size_get(struct ring_buf *buf)
{
return buf->tail - buf->head;
}
uint32_t ring_buf_size_get(struct ring_buf *buf);
/**
* @brief Write a data item to a ring buffer.