Concurrency#
Most of RCSW does no locking of its own. The pieces that do are built for it and say so; everything else needs the caller to serialize access.
What is thread-safe#
Component |
Guarantee |
|---|---|
Multithread primitives, |
Safe to call from any thread. |
Safe to call from any thread: publishing, receiving, creating receive queues and changing subscriptions are all serialized internally. |
|
Lock-free for exactly one producer and one consumer, such as an ISR and the main loop. More of either needs a lock. |
|
Add and remove take an internal busy flag; an asynchronous consumer
checks |
|
LOG4CL plugin |
On POSIX, reporting and installing modules are safe from any thread.
Removing modules, changing levels and |
All other data structures |
Not thread-safe. Lock around every call. |
The multithread primitives and the timespec helpers in rcsw/utils/time.h
need POSIX and are not available in bare-metal builds.
Snapshot values#
Size and state queries on thread-safe structures (mpool_size(),
pcqueue_size(), pcqueue_n_free(), rawfifo_size(), peeks, …)
return the value at the moment of the call. Another thread can change it
before the caller acts on it, so use them for monitoring, not to decide
whether a following call will succeed. Capacities don’t change and are safe to
rely on.
Timeouts#
Blocking calls with a timeout take a relative timeout: how long to wait
from now. This differs from the POSIX functions they wrap, which take an
absolute time. RCSW converts it against CLOCK_REALTIME with
utils_ts_make_abs().
A relative timeout restarts on every call. Code that waits in a loop, for
example to handle spurious wake-ups, would never time out. For that case use
the _abs variants (condv_timedwait_abs(),
csem_timedwait_abs()), which take an absolute CLOCK_REALTIME
deadline. utils_ts_make_abs() builds the deadline once before the
loop, and utils_ts_make_rel() turns it back into the time remaining.
struct timespec rel = {.tv_sec = 1, .tv_nsec = 0};
struct timespec deadline;
utils_ts_make_abs(&rel, &deadline);
mutex_lock(&mtx);
while (!ready) {
if (OK != condv_timedwait_abs(&cv, &mtx, &deadline)) {
break; /* errno == ETIMEDOUT once the deadline passes */
}
}
mutex_unlock(&mtx);