Multithread (multithread)#

group multithread

Thread synchronization primitives and thread-safe containers.

For an overview, see Multithread.

rcsw/multithread/bsem.h#

Binary semaphore.

Functions

struct bsem *bsem_init(struct bsem *sem_in, uint32_t flags)#

Initialize a binary semaphore.

Parameters:
Returns:

The initialized binary semaphore, or NULL if an ERROR occurred.

void bsem_destroy(struct bsem *sem)#

Destroy a binary semaphore.

Any further use of the semaphore after calling this function is undefined.

Parameters:
  • sem – The semaphore to destroy.

status_t bsem_post(struct bsem *sem)#

Unlock a binary semaphore.

Parameters:
  • sem – The semaphore handle.

Returns:

status_t.

status_t bsem_flush(struct bsem *sem)#

  • Notify all waiting threads and make it available again.

Parameters:
  • sem – The semaphore handle.

Returns:

status_t.

status_t bsem_timedwait(struct bsem *sem, const struct timespec *to)#

Wait on binary semaphore with a timeout.

Parameters:
  • sem – The semaphore handle.

  • to – A relative timeout. See Timeouts .

Returns:

status_t.

status_t bsem_wait(struct bsem *sem)#

Block on binary semaphore until it becomes available.

Parameters:
  • sem – The semaphore handle.

Returns:

status_t.

struct bsem#
#include <rcsw/multithread/bsem.h>

Wrapper around binary semaphores from various implementations.

Currently supports:

  • Non-POSIX (not part of POSIX standard for various reasons)

Public Members

struct mutex mtx#
struct condv cv#
bool val#
uint32_t flush_gen#

Incremented by bsem_flush(); waiters that see it change are released without taking the semaphore.

uint32_t flags#

Configuration flags.

Valid flags are:

rcsw/multithread/condv.h#

Condition variable.

Functions

struct condv *condv_init(struct condv *cv_in, uint32_t flags)#

Initialize the signal condition.

All other flags are ignored.

Parameters:
Returns:

The initialized signal condition, or NULL if an ERROR occurred.

void condv_destroy(struct condv *cv)#

Destroy the signal condition.

Parameters:
  • cv – The cv handle.

status_t condv_signal(struct condv *cv)#

Signal on a condition variable.

Parameters:
  • cv – The cv handle.

Returns:

status_t.

status_t condv_broadcast(struct condv *cv)#

Broadcast to everyone waiting on a condition variable.

This function unblocks all threads currently blocked on the condition variable. Each thread, upon its return from condv_wait() or condv_timedwait() will own the mutex it entered its waiting function with.

Parameters:
  • cv – The cv handle.

Returns:

status_t.

status_t condv_wait(struct condv *cv, struct mutex *mtx)#

Unconditional wait on a condition variable.

Parameters:
  • cv – The cv handle.

  • mtx – The mutex the wait pairs with.

Returns:

status_t.

status_t condv_timedwait(struct condv *cv, struct mutex *mtx, const struct timespec *to)#

Timed wait on a condition variable.

Parameters:
  • cv – The cv handle.

  • mtx – The mutex the wait pairs with.

  • to – A relative timeout. See Timeouts .

Returns:

status_t.

status_t condv_timedwait_abs(struct condv *cv, struct mutex *mtx, const struct timespec *deadline)#

Timed wait on a condition variable, with an absolute deadline.

For callers that wait in a loop (to handle spurious wakeups): a relative timeout restarted on every iteration would never expire.

Parameters:
  • cv – The cv handle.

  • mtx – The mutex the wait pairs with.

  • deadline – An absolute CLOCK_REALTIME deadline. See Timeouts .

Returns:

status_t. On timeout, errno is ETIMEDOUT.

struct condv#
#include <rcsw/multithread/condv.h>

Wrapper around condition variables from various implementations.

Currently supports:

  • POSIX condition variables

Public Members

pthread_cond_t impl#
uint32_t flags#

Valid flags are:

All other flags are ignored.

rcsw/multithread/csem.h#

Counting semaphore.

Functions

struct csem *csem_init(struct csem *sem_in, size_t value, uint32_t flags)#

Initialize a counting semaphore.

Parameters:
  • sem_in – Caller storage for the handle, used only if RCSW_NOALLOC_HANDLE is passed; ignored (may be NULL) otherwise. See Memory Model .

  • value – The initial semaphore value.

  • flags – Configuration flags. See csem::flags for valid flags.

Returns:

The initialization counting semaphore, or NULL if an ERROR occurred.

void csem_destroy(struct csem *sem)#

Destroy a counting semaphore.

Parameters:
  • sem – The semaphore to destroy.

status_t csem_post(struct csem *sem)#

Increment (unlock) a counting semaphore.

Parameters:
  • sem – The semaphore handle.

Returns:

status_t.

status_t csem_timedwait(struct csem *sem, const struct timespec *to)#

Wait on a counting semaphore with a timeout.

Parameters:
  • sem – The semaphore handle.

  • to – A relative timeout. See Timeouts .

Returns:

status_t.

status_t csem_timedwait_abs(struct csem *sem, const struct timespec *to)#

Wait on a counting semaphore with a timeout.

Parameters:
  • sem – The semaphore handle.

  • to – An absolute CLOCK_REALTIME deadline. See Timeouts .

Returns:

status_t.

status_t csem_wait(struct csem *sem)#

Wait on (lock) a counting semaphore.

Parameters:
  • sem – The semaphore handle.

Returns:

status_t.

status_t csem_trywait(struct csem *sem)#

Lock the semaphore only if it is currently available.

Otherwise, do nothing.

Parameters:
  • sem – The semaphore handle.

Returns:

status_t.

struct csem#
#include <rcsw/multithread/csem.h>

Wrapper around counting semaphores from various implementations.

Currently supports:

  • POSIX

Public Members

sem_t impl#
uint32_t flags#

Valid flags are:

All other flags are ignored.

rcsw/multithread/cvm.h#

Condition variable and mutex pair.

Functions

struct cvm *cvm_init(struct cvm *cvm_in, uint32_t flags)#

Initialize the signal condition (variable and mutex).

Parameters:
Returns:

The initialized CVM, or NULL if an ERROR occurred.

void cvm_destroy(struct cvm *cvm)#

Destroy the signal condition.

Parameters:
  • cvm – The CVM handle.

status_t cvm_signal(struct cvm *cvm)#

Signal on a condition variable while holding a mutex.

Parameters:
  • cvm – The CVM handle.

Returns:

status_t.

status_t cvm_broadcast(struct cvm *cvm)#

Broadcast to everyone waiting on a condition variable.

This function unblocks all threads currently blocked on the condition variable. Each thread, upon its return from cvm_wait() or cvm_timedwait() will own the mutex it entered its waiting function with.

Parameters:
  • cvm – The CVM handle.

Returns:

status_t.

status_t cvm_wait(struct cvm *cvm)#

Wait on a condition variable while holding a mutex.

Parameters:
  • cvm – The CVM handle.

Returns:

status_t.

status_t cvm_timedwait(struct cvm *cvm, const struct timespec *to)#

Timed wait on a condition variable while holding a mutex.

Parameters:
  • cvm – The CVM handle.

  • to – A relative timeout. See Timeouts .

Returns:

status_t

struct cvm#
#include <rcsw/multithread/cvm.h>

Convenience wrapper condv and mutex, because they often used together.

Currently supports:

  • POSIX

Public Members

struct mutex mtx#
struct condv cv#
uint32_t flags#

Valid flags are:

All other flags are ignored.

rcsw/multithread/mpool.h#

Thread-safe, reference-counted pool of fixed-size memory chunks.

Functions

static inline size_t mpool_meta_space(size_t max_elts)#

Get # of bytes needed for space for the mpool nodes.

Parameters:
  • max_elts – # of desired elements in pool.

Returns:

The # of bytes the application would need to allocate.

static inline size_t mpool_element_space(size_t max_elts, size_t elt_size)#

Get # of bytes needed for space for the mpool data.

Parameters:
  • max_elts – # of desired elements in pool.

  • elt_size – Size of elements in bytes.

Returns:

The # of bytes the application would need to allocate.

static inline bool mpool_isfull(const struct mpool *const pool)#

Determine if the memory pool is currently full.

Note

The returned value cannot be relied upon in concurrent contexts without additional synchronization.

Parameters:
  • pool – The pool handle.

static inline bool mpool_isempty(const struct mpool *const pool)#

Determine if the mpool is currently empty.

Note

The returned value cannot be relied upon in concurrent contexts without additional synchronization.

Parameters:
  • pool – The pool handle.

static inline size_t mpool_size(const struct mpool *const pool)#

Determine # elements currently in the memory pool.

Note

The returned value cannot be relied upon in concurrent contexts without additional synchronization.

Parameters:
  • pool – The pool handle.

Returns:

# elements in memory pool, or 0 on ERROR.

static inline size_t mpool_capacity(const struct mpool *const pool)#

Determine maximum # elements in the memory pool.

Parameters:
  • pool – The pool handle.

Returns:

Max # elements in memory pool, or 0 on ERROR.

struct mpool *mpool_init(struct mpool *pool_in, const struct mpool_config *params)#

Initialize a mpool.

Parameters:
  • pool_in – Caller storage for the handle, used only if RCSW_NOALLOC_HANDLE is passed; ignored (may be NULL) otherwise. See Memory Model .

  • params – The initialization parameters.

Returns:

The initialized pool, or NULL if an error occurred.

void mpool_destroy(struct mpool *the_pool)#

Destroy a mpool.

Any further use of the pool handle after calling this function is undefined.

Parameters:
  • the_pool – The mpool handle.

void *mpool_req(struct mpool *the_pool)#

Request a memory chunk from a mpool.

If no memory of the requested type is currently available, wait indefinitely.

Parameters:
  • the_pool – The mpool handle.

Returns:

The allocated chunk, or NULL if an error occurred.

status_t mpool_timedreq(struct mpool *the_pool, const struct timespec *to, void **chunk)#

Request a memory chunk from a mpool with a timeout.

Parameters:
  • the_pool – The mpool handle.

  • to – Timeout.

  • chunk – The pointer to fill.

Returns:

OK with the chunk in *chunk, or ERROR on timeout or error.

status_t mpool_tryreq(struct mpool *the_pool, void **chunk)#

Request a memory chunk from a mpool without waiting.

Parameters:
  • the_pool – The mpool handle.

  • chunk – The pointer to fill.

Returns:

OK with the chunk in *chunk, or ERROR with errno=ENOSPC if no chunk is free right now.

status_t mpool_release(struct mpool *the_pool, void *ptr)#

Release a chunk of memory from a mpool.

Memory chunk will not actually be freed until the last reference has released it.

Parameters:
  • the_pool – The mpool handle.

  • ptr – The memory to release.

Returns:

status_t.

status_t mpool_ref_add(struct mpool *the_pool, const void *ptr)#

Increment ref count for a previously allocated chunk in a mpool.

This function is useful if a thread/module wants to give a non-owning reference to a chunk of memory to another thread/module.

Parameters:
  • the_pool – The mpool handle.

  • ptr – The chunk to add a reference to.

Returns:

status_t.

status_t mpool_ref_remove(struct mpool *the_pool, const void *ptr)#

Decrement ref count for a currently allocated chunk in a mpool.

If the reference count reaches 0 as a result of calling this function, the chunk is not freed.

Parameters:
  • the_pool – The mpool handle.

  • ptr – The chunk to remove a reference from.

Returns:

status_t

int mpool_ref_query(struct mpool *the_pool, const void *ptr)#

Get the index of the reference count (not the reference count)

Note

The returned value cannot be relied upon in concurrent contexts without additional synchronization.

Parameters:
  • the_pool – The mpool handle.

  • ptr – The chunk to query.

Returns:

The reference index, or -1 if does not exist.

size_t mpool_ref_count(struct mpool *the_pool, const void *ptr)#

Get the reference count of an allocated chunk in a mpool.

Note

The returned value cannot be relied upon in concurrent contexts without additional synchronization.

Parameters:
  • the_pool – The mpool handle.

  • ptr – The chunk to query.

Returns:

The reference count, or SIZE_MAX on error.

struct mpool_config#
#include <rcsw/multithread/mpool.h>

Memory pool initialization parameters.

Public Members

dptr_t *meta#

Pointer to application-allocated space for storing the llist_node objects used to carve up mpool_config::elements and reference counts. Ignored unless RCSW_NOALLOC_META is passed.

dptr_t *elements#

Pointer to application-allocated space for the pool. Ignored unless RCSW_NOALLOC_DATA is passed.

size_t elt_size#

Size of elements in bytes.

size_t max_elts#

Number of elements in the pool.

uint32_t flags#

Run time configuration flags. Valid flags are:

All other flags are ignored.

struct mpool#
#include <rcsw/multithread/mpool.h>

Memory pool: a threadsafe malloc()/free() over a set of memory chunks.

Memory chunks must be fixed size.

Public Members

struct mpool_config cfg#

The configuration the pool was initialized with. mpool_config::elements and mpool_config::meta are as passed (NULL if the library allocated the storage); use mpool::elements and mpool::meta for the storage in use.

dptr_t *elements#

The chunks of managed memory.

dptr_t *meta#

Space for the llist nodes for both the free and allocated lists and reference counts.

struct llist free#

List of free chunks of memory.

struct llist alloc#

List of allocated chunks of memory.

int *refs#

Reference counting. Same length as max # elements.

struct csem slots_avail#

Used to wait for a chunk to become free in mpool_req().

struct mutex mutex#

Lock around most operations for concurrency safety.

rcsw/multithread/mutex.h#

Mutex.

Functions

struct mutex *mutex_init(struct mutex *mutex_in, uint32_t flags)#

Initialize a mutex.

Parameters:
Returns:

The initialized mutex, or NULL if an ERROR occurred.

void mutex_destroy(struct mutex *mutex)#

Destroy a mutex.

Parameters:
  • mutex – The mutex handle.

status_t mutex_lock(struct mutex *mutex)#

Acquire the lock.

Parameters:
  • mutex – The mutex handle.

Returns:

status_t.

status_t mutex_unlock(struct mutex *mutex)#

Release the lock.

Parameters:
  • mutex – The mutex handle.

Returns:

status_t.

struct mutex#
#include <rcsw/multithread/mutex.h>

Wrapper around mutexes from various implementations.

Currently supports:

  • POSIX mutexes

Public Members

pthread_mutex_t impl#
uint32_t flags#

Valid flags are:

All other flags are ignored.

rcsw/multithread/pcqueue.h#

Thread-safe producer-consumer queue.

Defines

pcqueue_config#

Producer-consumer queue initialization parameters.

Functions

static inline bool pcqueue_isfull(const struct pcqueue *const queue)#

Determine if the queue is currently full.

Parameters:
  • queue – The queue handle.

static inline bool pcqueue_isempty(const struct pcqueue *const queue)#

Determine if the queue is currently empty.

Parameters:
  • queue – The linked queue handle.

static inline size_t pcqueue_size(const struct pcqueue *const queue)#

Determine # elements currently in the queue. The value returned by this function should not be relied upon for accuracy among multiple threads without additional synchronization.

Parameters:
  • queue – The queue handle.

Returns:

# elements in queue, or 0 on ERROR.

static inline size_t pcqueue_capacity(const struct pcqueue *const queue)#

Get the capacity of the queue. The value returned can be relied upon in a multi-thread context, because it does not change during the lifetime of the queue.

Parameters:
  • queue – The queue handle.

Returns:

Queue capacity, or 0 on ERROR.

static inline size_t pcqueue_n_free(const struct pcqueue *const queue)#

Get the # slots available in the queue. The value returned cannot be relied upon in a multi-thread context without additional synchronization.

Parameters:
  • queue – The queue handle.

Returns:

# free slots, or 0 on ERROR.

struct pcqueue *pcqueue_init(struct pcqueue *pcqueue_in, const struct fifo_config *params)#

Initialize a producer-consumer queue.

Parameters:
  • pcqueue_in – Caller storage for the handle, used only if RCSW_NOALLOC_HANDLE is passed; ignored (may be NULL) otherwise. See Memory Model .

  • params – The initialization parameters.

Returns:

The initialized queue, or NULL if an error occurred.

void pcqueue_destroy(struct pcqueue *pcqueue)#

Destroy a producer-consumer queue.

Any further use of the queue handle after calling this function is undefined.

Parameters:
  • pcqueue – The queue handle.

status_t pcqueue_push(struct pcqueue *pcqueue, const void *e)#

Push an item to the back of the queue, waiting if necessary for space to become available.

Parameters:
  • pcqueue – The queue handle.

  • e – The item to enqueue.

Returns:

status_t.

status_t pcqueue_trypush(struct pcqueue *pcqueue, const void *e)#

Push an item to the back of the queue if there is space, without waiting.

Parameters:
  • pcqueue – The queue handle.

  • e – The item to enqueue.

Returns:

status_t. If the queue is full, ERROR with errno=ENOSPC.

status_t pcqueue_pop(struct pcqueue *pcqueue, void *e)#

Pop and return the first element in the queue, waiting if necessary for the queue to become non-empty.

Parameters:
  • pcqueue – The queue handle.

  • e – The item to dequeue. Can be NULL.

Returns:

status_t.

status_t pcqueue_timedpop(struct pcqueue *pcqueue, const struct timespec *to, void *e)#

Pop and return the first element in the queue, waiting until the timeout if necessary for the queue to become non-empty.

Parameters:
  • pcqueue – The queue handle.

  • to – A relative timeout. See Timeouts .

  • e – The item to dequeue. Can be NULL.

Returns:

status_t.

status_t pcqueue_timedpeek(struct pcqueue *queue, const struct timespec *to, void **e)#

Get the first element in the queue if it exists, with a timeout.

Note

The filled value returned by this function cannot be relied upon in a multi-threaded context without additional synchronization.

Parameters:
  • queue – The queue handle.

  • to – A relative timeout. See Timeouts .

  • e – To be filled with the address of the first element, if it exists, and set to NULL otherwise.

Returns:

status_t.

status_t pcqueue_peek(struct pcqueue *queue, void **e)#

Get the first element in the queue if it exists.

Note

The filled value returned by this function cannot be relied upon in a multi-threaded context without additional synchronization.

Parameters:
  • queue – The queue handle.

  • e – To be filled with the address of the first element, if it exists, and set to NULL otherwise.

Returns:

status_t.

status_t pcqueue_waitpeek(struct pcqueue *queue, void **e)#
struct pcqueue#
#include <rcsw/multithread/pcqueue.h>

Producer-consumer queue, providing thread-safe access to data at both ends of a FIFO.

Public Members

struct fifo fifo#

The underlying FIFO.

struct mutex mutex#

Mutex protecting fifo.

struct csem slots_avail#

Semaphore counting empty slots in FIFO.

struct csem slots_inuse#

Semaphore counting occupied/allocated slots in FIFO.

uint32_t flags#

Run-time configuration flags.

Valid flags are:

All other flags are ignored.

rcsw/multithread/rdwrlock.h#

Fair reader/writer lock.

Enums

enum rdwrlock_scope#

The scope of privileges requested for a critical section of code: reading or writing.

For use with rdwrlock, in which there can be multiple concurrent readers OR a single writer in a critical section at a given time.

Values:

enumerator SCOPE_RD#
enumerator SCOPE_WR#

Functions

struct rdwrlock *rdwrl_init(struct rdwrlock *rdwr_in, uint32_t flags)#

Initialize a reader/writer fair lock.

Parameters:
  • rdwr_in – Caller storage for the handle, used only if RCSW_NOALLOC_HANDLE is passed; ignored (may be NULL) otherwise. See Memory Model .

  • flags – Configuration flags

Returns:

The initialized RDWR lock, or NULL if an ERROR occurred.

void rdwrl_destroy(struct rdwrlock *rdwr)#

Destroy a reader/writer fair lock.

Any further use of the lock after calling this function is undefined.

Parameters:
  • rdwr – The lock handle.

void rdwrl_req(struct rdwrlock *rdwr, enum rdwrlock_scope scope)#

Request to enter a critical section.

This function is part of a fair (i.e. no rd/wr preference) sequence that guarantees that no reader or write will starve.

Parameters:
  • rdwr – The lock handle.

  • scope – The scope of the privileges requested.

void rdwrl_exit(struct rdwrlock *rdwr, enum rdwrlock_scope scope)#

Exit a critical section.

This function is part of a fair (i.e. no rd/wr preference) sequence that guarantees that no reader or write will starve.

Parameters:
  • rdwr – The lock handle.

  • scope – The scope of the privileges requested.

status_t rdwrl_timedreq(struct rdwrlock *rdwr, enum rdwrlock_scope scope, const struct timespec *to)#

Request to enter a critical section with a timeout.

This function is part of a fair (i.e. no rd/wr preference) sequence that guarantees that no reader or write will starve.

Parameters:
  • rdwr – The lock handle.

  • scope – The scope of the privileges requested.

  • to – A relative timeout. See Timeouts .

Returns:

status_t.

struct rdwrlock#
#include <rcsw/multithread/rdwrlock.h>

Fair reader-writer lock that guarantees that neither readers nor writers will starve.

Public Members

struct csem order#

Used to form a queue of readers/writers

struct csem access#

Exclusive access semaphore

struct csem read#

Semaphore for updating n_readers

size_t n_readers#

Number of readers currently accessing resource

uint32_t flags#

Configuration flags. Valid flags are:

All other flags are ignored.

rcsw/multithread/threadm.h#

Various thread management tools.

Functions

status_t threadm_core_lock(pthread_t thread, size_t core)#

Lock a thread to a core.

Parameters:
  • thread – The thread handle.

  • core – The core to lock to, 0-indexed.

Returns:

status_t. ERROR with errno=EINVAL if core >= CPU_SETSIZE or is not a core the thread may run on; otherwise errno is the error code from pthread_setaffinity_np().