cthreadcomm · Overview
cthreadcomm
NAME
cthreadcomm - overview of the thread communication API
SYNOPSIS
#include <ccollections/cthreadcomm.h>
DESCRIPTION
This module moves data between threads and lets a thread wait until a
queue or a file descriptor is ready. It is made of five parts:
ccircq A bounded queue. A send blocks while the queue
is full, which gives backpressure to a producer.
cdynmq An unbounded queue. A send never waits.
cchan Two circular queues, one in each direction;
the identity of the calling thread selects
the queue.
cselect() A one-shot wait until one of several queues
or file descriptors is ready; ready items
share the calls fairly.
ceventloop A persistent reactor that runs callbacks for
a set of queues and file descriptors that can
change.
Messages and ownership
Every queue carries values of type cmessage:
typedef struct cmessage {
void *data;
size_t size;
} cmessage;
The library copies no payload. A send that succeeds hands the
ownership of data to the queue and sets the data field of the sender
to NULL. A receive hands the ownership to the receiver, which must
free data. A send that fails leaves the message as it is, so the
caller keeps the ownership of data. A message with a NULL data and a
size of 0 is valid, and it is the usual end-of-stream sentinel. Every
send refuses, as ccol_invalid_args, a NULL data with a size above 0
and a data that is not NULL with a size of 0.
Blocking, try and timed calls
A plain send or receive blocks. A try_ call never blocks and returns ccol_container_full or ccol_container_empty instead. A timed_ call takes a uint64_t count of microseconds, measured on CLOCK_MONOTONIC, so changing the wall clock doesn't make the wait longer or shorter. With a timeout of 0 a timed call behaves exactly like its try_ variant and returns that variant's result. A timeout too large for a deadline (such as UINT64_MAX) gets the largest deadline there is, so the deadline never wraps around into the past.
Readiness only
cselect() and ceventloop only say that an item is ready; they
never receive or send anything. Right after that the caller makes its
own non-blocking call:
- For a queue: ccircq_try_recv(), cdynmq_try_recv(),
ccircq_try_send() or cdynmq_send().
- For a file descriptor: read(2), recv(2), write(2) or send(2).
As with POSIX select(2), another consumer or producer can take the
message or the space first, so that call must not block and you always
have to check its result.
An event loop registration can watch a queue. While the registration
has a wake pending, a send to that queue makes no system call, because
one dispatch serves every send that came before it started.
Handles
Queues and channels are plain pointers, and their destroy macros set the pointer to NULL. ceventloop and ceventloop_reg are opaque VALUE handles (uint64_t) and not pointers. Compare them with CEVENTLOOP_NULL and CEVENTLOOP_REG_NULL, which are both 0. Such a handle is resolved before the object behind it is used, so a stale handle is refused and never dereferenced.
Process exit
Event loop dispatch uses two thread-specific keys, created once per process; queues and channels need no key. If the process has no free key left, creating a loop fails with CEVENTLOOP_NULL for the rest of that process, while queues and channels keep working. The library's process-exit destructors delete the keys and free the library's bookkeeping for all loops and queues. When a queue or a loop is still live at that point, its own destroy does that release later, so an application destructor that runs after the library's destructors can still destroy the objects it owns. Once the release has happened, creating a queue, a channel or a loop fails cleanly.
fork(2)
There are two supported ways to use fork(2) with this module: - Call fork(2) before you create a queue, a channel or a loop. - Create the object, then call fork(2) followed at once by exec(3). A child that keeps running with no exec(3) while an object from before the fork is live is not supported. The child never inherits a queue's lock in the locked state, and a queue has no thread of its own, so a queue from the parent stays usable in the child. An event loop from the parent has no reactor threads in the child, though, so it dispatches nothing there.
SEE ALSO
ccircq_create(3), cdynmq_create(3), cchan_create(3), cselect(3), ceventloop_create(3), ceventloop_add(3), ceventloop_remove(3), ceventloop_destroy(3)