SPSC
A shared memory single-producer single-consumer (SPSC) queue
spsc.h File Reference
#include <stdint.h>
#include <stdio.h>
Include dependency graph for spsc.h:

Go to the source code of this file.

Data Structures

struct  spsc_ring_data
 
struct  spsc_ring
 

Macros

#define MSG_SIZE_T   uint32_t
 
#define READ_MODE   1
 
#define WRITE_MODE   2
 

Functions

int spsc_create_sub (spsc_ring *ring, const char *pathname, const size_t size)
 
int spsc_create_pub (spsc_ring *ring, const char *pathname, const size_t size)
 
size_t spsc_read (spsc_ring *ring, void *dest, const size_t n)
 
MSG_SIZE_T spsc_write (spsc_ring *ring, const void *src, const MSG_SIZE_T n)
 
size_t spsc_size (const spsc_ring *ring)
 
size_t spsc_capacity (const spsc_ring *ring)
 
void spsc_destroy (spsc_ring *ring)
 

Macro Definition Documentation

◆ MSG_SIZE_T

#define MSG_SIZE_T   uint32_t

◆ READ_MODE

#define READ_MODE   1

◆ WRITE_MODE

#define WRITE_MODE   2

Function Documentation

◆ spsc_capacity()

size_t spsc_capacity ( const spsc_ring ring)

Gets the maximum size of the ring.

Returns
the maximum size of the ring.

◆ spsc_create_pub()

int spsc_create_pub ( spsc_ring ring,
const char *  pathname,
const size_t  size 
)

Creates a publisher for a shared memory spsc ring located at the specified path. A new file is created if it doesn't exist.

Parameters
ringpointer to a spsc_ring structure
pathnamepath to the shared memory file
sizemax size of the ring in bytes
Returns
0 if successful, otherwise an error code is returned.

◆ spsc_create_sub()

int spsc_create_sub ( spsc_ring ring,
const char *  pathname,
const size_t  size 
)

Creates a subscriber for a shared memory spsc ring located at the specified path. A new file is created if it doesn't exist.

Parameters
ringpointer to a spsc_ring structure
pathnamepath to the shared memory file
sizemax size of the ring in bytes
Returns
0 if successful, otherwise an error code is returned.

◆ spsc_destroy()

void spsc_destroy ( spsc_ring ring)

Destructor for spsc_ring.

Parameters
ringpointer to a spsc_ring structure

◆ spsc_read()

size_t spsc_read ( spsc_ring ring,
void *  dest,
const size_t  n 
)

Reads the next entry from the ring into the specified location.

Parameters
ringpointer to a spsc_ring structure
destpointer to a location to copy bytes to
nnumber of bytes to copy, must be less than or equal to the size of dest.
Returns
the number of bytes copied, or 0 if there is nothing left to read.

◆ spsc_size()

size_t spsc_size ( const spsc_ring ring)

Gets an approximate size of the ring. The returned number depends on whether the publishing thread or subscribing thread called the method. The behvaiour is undefined for any other calling thread.

Parameters
ringpointer to a spsc_ring structure
Returns
an approximate size of the ring.

◆ spsc_write()

MSG_SIZE_T spsc_write ( spsc_ring ring,
const void *  src,
const MSG_SIZE_T  n 
)

Writes an entry into the ring, copying from the specified location.

Parameters
ringpointer to a spsc_ring structure
srcpointer to a location to copy bytes from
nnumber of bytes to copy, must be less than or equal to the size of src
Returns
the number of bytes copied, or 0 if the ring is full.