Multithread#

Thin wrappers around the POSIX threading primitives, plus a memory pool and a producer-consumer queue built on them. Every module here is thread-safe by design, and POSIX-only: none of it is available in bare-metal builds.

Note

To use caller-supplied storage for a primitive’s handle, pass it with RCSW_NOALLOC_HANDLE. Without that flag the handle argument is ignored (NULL is fine) and the library allocates one. See Memory Model.

Primitives#

Module

Notes

Link

Mutex

Wrapper around POSIX mutexes (pthread_mutex_t). Non-recursive.

mutex

Condition variable

Wrapper around POSIX condition variables (pthread_cond_t).

condv

Condition variable / mutex pair

Convenience wrapper combining condv and mutex into a single handle, since they are almost always used together. As with a plain condition variable, the caller holds the mutex (cvm.mtx) around waits.

cvm

Binary semaphore

Built on mutex + condv. Provides post, wait, timed wait, and flush (release every waiter).

bsem

Counting semaphore

Wrapper around POSIX semaphores (sem_t).

csem

Fair reader/writer lock

Guarantees that neither readers nor writers will starve. Uses three csem instances (order, access, read) to enforce strict arrival-order fairness. Suitable for protecting shared data structures (e.g., ds/ modules) from concurrent access.

rdwrlock

Higher-Level Constructs#

Module

Notes

Link

Memory pool

Thread-safe fixed-size block allocator. Used by threads to request/release memory chunks of a specified size. Reference-counted; a block is returned to the pool only when all holders have released it. mpool_req() blocks until a chunk is free; mpool_tryreq() fails at once with ENOSPC instead. Particularly useful in publisher-subscriber settings (e.g., swbus).

mpool

Producer-consumer queue

Blocking FIFO queue supporting multiple producers and consumers. Internally synchronized; callers do not need additional locking.

pcqueue

Thread Management#

#include "rcsw/multithread/threadm.h"

threadm_core_lock() pins a thread to a CPU core. Like the rest of this module it is POSIX-only; it uses pthread_setaffinity_np(), so it needs Linux or another platform that provides it.

For timeouts and what is safe to call concurrently, see Concurrency.