librfn
An ad-hoc utility library
Data Structures | Macros | Functions
Ring buffer

Lockless byte-based ring buffer using C11 atomic operations. More...

Data Structures

struct  ringbuf_t
 Ring buffer descriptor. More...
 

Macros

#define RINGBUF_VAR_INIT(bufp, buf_len)
 Static initializer for a ring buffer descriptor. More...
 

Functions

void ringbuf_init (ringbuf_t *rb, void *bufp, size_t buf_len)
 Runtime initializer for a ring buffer descriptor. More...
 
int ringbuf_get (ringbuf_t *rb)
 Extract a byte from the ring buffer. More...
 
bool ringbuf_empty (ringbuf_t *rb)
 Test whether the ring buffer contains any data. More...
 
bool ringbuf_put (ringbuf_t *rb, uint8_t d)
 Insert a byte into the ring buffer. More...
 
void ringbuf_putchar (void *rb, char c)
 Insert a character into the ring buffer. More...
 

Detailed Description

Lockless byte-based ring buffer using C11 atomic operations.

The message queue is thread safe (and SMP safe) only for one-to-one messaging. It cannot be used with multiple sender threads nor multiple receiver threads without additional locking.

Macro Definition Documentation

#define RINGBUF_VAR_INIT (   bufp,
  buf_len 
)
Value:
{ \
(uint8_t *) (bufp), \
(buf_len), \
ATOMIC_VAR_INIT(0), \
ATOMIC_VAR_INIT(0) \
}

Static initializer for a ring buffer descriptor.

Definition at line 48 of file ringbuf.h.

Function Documentation

bool ringbuf_empty ( ringbuf_t rb)

Test whether the ring buffer contains any data.

Returns
true if data is present, otherwise false.

Definition at line 52 of file ringbuf.c.

int ringbuf_get ( ringbuf_t rb)

Extract a byte from the ring buffer.

Returns
Unsigned byte on success, otherwise -1.

Definition at line 31 of file ringbuf.c.

void ringbuf_init ( ringbuf_t rb,
void *  bufp,
size_t  buf_len 
)

Runtime initializer for a ring buffer descriptor.

Definition at line 22 of file ringbuf.c.

bool ringbuf_put ( ringbuf_t rb,
uint8_t  d 
)

Insert a byte into the ring buffer.

Definition at line 58 of file ringbuf.c.

void ringbuf_putchar ( void *  rb,
char  c 
)

Insert a character into the ring buffer.

This function is intended to be use from callback functions. For this reason the ringbuffer is passed as a void pointer (i.e. user data) and there is no return value.

Warning
This function busy waits until there is space in the ring buffer. This leaves the system at risk of deadlock if this function is called from a calling context that can pre-empt the consumer. For example if the data is consumed by an interrupt handler then this function should not be called only from higher (or equal) priority interrupt handler.

Definition at line 75 of file ringbuf.c.