Thread safe ringbuffer

Detailed Description

Files

file  tsrb.h
 Thread-safe ringbuffer implementation.
 

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 
)
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 
)
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
Parameters
[in]rbRingbuffer to operate on
Returns
nr of available bytes

Definition at line 88 of file tsrb.h.

◆ tsrb_empty()

static int tsrb_empty ( const tsrb_t rb)
inlinestatic
Parameters
[in]rbRingbuffer to operate on
Returns
0 if not empty
1 otherwise

Definition at line 77 of file tsrb.h.

◆ tsrb_free()

static unsigned int tsrb_free ( const tsrb_t rb)
inlinestatic
Parameters
[in]rbRingbuffer to operate on
Returns
nr of available bytes

Definition at line 109 of file tsrb.h.

◆ tsrb_full()

static int tsrb_full ( const tsrb_t rb)
inlinestatic
Parameters
[in]rbRingbuffer to operate on
Returns
0 if not full
1 otherwise

Definition at line 99 of file tsrb.h.

◆ tsrb_get()

int tsrb_get ( tsrb_t rb,
char *  dst,
size_t  n 
)
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)
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
Parameters
[out]rbDatum to initialize.
[in]bufferBuffer to use by tsrb.
[in]bufsizesizeof (buffer)

Definition at line 58 of file tsrb.h.