Thread safe ringbuffer

Thread-safe ringbuffer implementation. More...

Detailed Description

Thread-safe ringbuffer implementation.

Files

file  tsrb.h
 Thread-safe ringbuffer interface definition.
 

Data Structures

struct  tsrb
 thread-safe ringbuffer struct More...
 

Macros

#define TSRB_INIT(BUF)   { (BUF), sizeof (BUF), 0, 0 }
 Static initializer.
 

Typedefs

typedef struct tsrb tsrb_t
 thread-safe ringbuffer struct
 

Functions

static void tsrb_init (tsrb_t *rb, char *buffer, unsigned bufsize)
 Initialize a tsrb. More...
 
static int tsrb_empty (const tsrb_t *rb)
 Test if the tsrb is empty. More...
 
static unsigned int tsrb_avail (const tsrb_t *rb)
 Get number of bytes available for reading. More...
 
static int tsrb_full (const tsrb_t *rb)
 Test if the tsrb is full. More...
 
static unsigned int tsrb_free (const tsrb_t *rb)
 Get free space in ringbuffer. More...
 
int tsrb_get_one (tsrb_t *rb)
 Get a byte from ringbuffer. More...
 
int tsrb_get (tsrb_t *rb, char *dst, size_t n)
 Get bytes from ringbuffer. More...
 
int tsrb_add_one (tsrb_t *rb, char c)
 Add a byte to ringbuffer. More...
 
int tsrb_add (tsrb_t *rb, const char *src, size_t n)
 Add bytes to ringbuffer. More...
 

Function Documentation

◆ tsrb_add()

int tsrb_add ( tsrb_t rb,
const char *  src,
size_t  n 
)

Add bytes to ringbuffer.

Parameters
[in]rbRingbuffer to operate on
[in]srcbuffer to read from
[in]nmax number of bytes to read from src
Returns
nr of bytes read from src

◆ tsrb_add_one()

int tsrb_add_one ( tsrb_t rb,
char  c 
)

Add a byte to ringbuffer.

Parameters
[in]rbRingbuffer to operate on
[in]cCharacter to add to ringbuffer
Returns
0 on success
-1 if no space available

◆ tsrb_avail()

static unsigned int tsrb_avail ( const tsrb_t rb)
inlinestatic

Get number of bytes available for reading.

Parameters
[in]rbRingbuffer to operate on
Returns
nr of available bytes

Definition at line 87 of file tsrb.h.

◆ tsrb_empty()

static int tsrb_empty ( const tsrb_t rb)
inlinestatic

Test if the tsrb is empty.

Parameters
[in]rbRingbuffer to operate on
Returns
0 if not empty
1 otherwise

Definition at line 76 of file tsrb.h.

◆ tsrb_free()

static unsigned int tsrb_free ( const tsrb_t rb)
inlinestatic

Get free space in ringbuffer.

Parameters
[in]rbRingbuffer to operate on
Returns
nr of available bytes

Definition at line 108 of file tsrb.h.

◆ tsrb_full()

static int tsrb_full ( const tsrb_t rb)
inlinestatic

Test if the tsrb is full.

Parameters
[in]rbRingbuffer to operate on
Returns
0 if not full
1 otherwise

Definition at line 98 of file tsrb.h.

◆ tsrb_get()

int tsrb_get ( tsrb_t rb,
char *  dst,
size_t  n 
)

Get bytes from ringbuffer.

Parameters
[in]rbRingbuffer to operate on
[out]dstbuffer to write to
[in]nmax number of bytes to write to dst
Returns
nr of bytes written to dst

◆ tsrb_get_one()

int tsrb_get_one ( tsrb_t rb)

Get a byte from ringbuffer.

Parameters
[in]rbRingbuffer to operate on
Returns
>=0 byte that has been read
-1 if no byte available

◆ tsrb_init()

static void tsrb_init ( tsrb_t rb,
char *  buffer,
unsigned  bufsize 
)
inlinestatic

Initialize a tsrb.

Parameters
[out]rbDatum to initialize.
[in]bufferBuffer to use by tsrb.
[in]bufsizesizeof (buffer)

Definition at line 57 of file tsrb.h.