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:
sem_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. See bsem::flags for valid flags.
- 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 bsem_flush(struct bsem *sem)#
Notify all waiting threads and make it available again.
- Parameters:
sem – The semaphore handle.
- Returns:
-
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
-
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:
cv_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. See condv::flags for valid flags.
- Returns:
The initialized signal condition, or NULL if an ERROR occurred.
-
status_t condv_signal(struct condv *cv)#
Signal on a condition variable.
- Parameters:
cv – The cv handle.
- Returns:
-
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 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 condv_timedwait(struct condv *cv, struct mutex *mtx, const struct timespec *to)#
Timed wait on a condition variable.
-
struct condv#
- #include <rcsw/multithread/condv.h>
Wrapper around condition variables from various implementations.
Currently supports:
POSIX condition variables
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 csem_timedwait(struct csem *sem, const struct timespec *to)#
Wait on a counting semaphore with a timeout.
-
status_t csem_timedwait_abs(struct csem *sem, const struct timespec *to)#
Wait on a counting semaphore with a timeout.
-
struct csem#
- #include <rcsw/multithread/csem.h>
Wrapper around counting semaphores from various implementations.
Currently supports:
POSIX
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:
cvm_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. see cvm::flags for valid flags.
- Returns:
The initialized CVM, or NULL if an ERROR occurred.
-
status_t cvm_signal(struct cvm *cvm)#
Signal on a condition variable while holding a mutex.
- Parameters:
cvm – The CVM handle.
- Returns:
-
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:
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.
-
status_t mpool_tryreq(struct mpool *the_pool, void **chunk)#
Request a memory chunk from a mpool without waiting.
-
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 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 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:
-
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_MAXon 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.
-
dptr_t *meta#
-
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.
-
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 mpool_config cfg#
rcsw/multithread/mutex.h#
Mutex.
Functions
-
struct mutex *mutex_init(struct mutex *mutex_in, uint32_t flags)#
Initialize a mutex.
- Parameters:
mutex_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. See mutex::flags for valid flags.
- Returns:
The initialized mutex, or NULL if an ERROR occurred.
-
struct mutex#
- #include <rcsw/multithread/mutex.h>
Wrapper around mutexes from various implementations.
Currently supports:
POSIX mutexes
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 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 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.
-
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.
-
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:
rcsw/multithread/rdwrlock.h#
Fair reader/writer lock.
Enums
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.
-
struct rdwrlock#
- #include <rcsw/multithread/rdwrlock.h>
Fair reader-writer lock that guarantees that neither readers nor writers will starve.
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().