cthreadcomm · Section 3
cchan_destroy
NAME
cchan_destroy - destroy a cchan
LIBRARY
C Collections (libccollections, -lccollections)
SYNOPSIS
#include <ccollections/cthreadcomm.h> cchan_destroy(ch);
DESCRIPTION
Frees everything the cchan holds, both of its queues included, and then sets the cchan pointer to NULL. ceventloop_remove() doesn't wait for a callback that it has already handed to a reactor thread, so such a callback can still be working inside one of the channel's queues when the removal returns. This call blocks until every one of those callbacks is done with the queue and only then frees it, which is what makes the documented teardown order (drain, remove, destroy) safe as written. A callback that the loop runs for a different registration can run that whole sequence for this channel, with any num_reactor_threads. A dispatch that the loop collected for the removed registration but did not start yet doesn't hold the destroy up, and it never runs its callback after the removal.
PARAMETERS
ch Channel to destroy. The macro sets it to NULL.
NOTES
You can call this with a NULL pointer. ch is evaluated exactly once and must be a modifiable lvalue, such as a variable or an array element. An expression with a side effect, such as handles[i++], therefore destroys and clears a single element.
WARNINGS
A message left in either direction causes an assertion failure, so drain both queues before you destroy the channel. A watcher on either direction causes an assertion failure too. A watcher is a cselect() call, a cselect_timed() call or a ceventloop registration (cchan_selectable). Without the assertion the waiter would stay linked and keep pointing at the mutex of a queue that the destroy is about to free. Call ceventloop_remove() first, or let every cselect() and cselect_timed() call that watches the channel return. Do not call this from inside a ceventloop callback that dispatches one of the directions of this cchan, because the destroy would wait for that very callback. It is a caller error, and the program stops through ccol__fatal_err(). Do not hold an application lock across this call when one of those callbacks takes the same lock: The destroy waits for the callback while the callback waits for the lock.