cthreadcomm · Section 3
ceventloop_destroy
NAME
ceventloop_destroy - destroy a ceventloop and invalidate the handle
LIBRARY
C Collections (libccollections, -lccollections)
SYNOPSIS
#include <ccollections/cthreadcomm.h> ceventloop_destroy(loop)
DESCRIPTION
Waits until every resolved use of the handle that is in flight has finished, shuts every reactor thread down (unless that already happened) and frees every registration that is left. At the end it sets loop to CEVENTLOOP_NULL. The shutdown is the same graceful drain as ceventloop_shutdown(3). The callbacks that the loop has already collected still run, and loop stays a live handle for them until the drain ends, so a callback can call ceventloop_remove(3), ceventloop_pause(3), ceventloop_resume(3), ceventloop_modify(3) and ceventloop_add(3) on loop during the destroy, with the same results as during ceventloop_shutdown(3). That makes it safe to close a connection from its own callback (remove each of its registrations, close the fd, free arg) while another thread destroys the loop: Once the removals return, no callback of those registrations runs again. After the drain, loop stops resolving and every later call on it fails as it would for a destroyed handle. loop must be a handle that is live at the time of the call, or CEVENTLOOP_NULL, which does nothing. A call on a stale handle (one that an earlier call destroyed, or that another thread is destroying at the same time) is a FATAL ERROR (abort()/SIGABRT) and never a silent double free. The handle carries a generation tag that is checked before the loop is touched, so such a value is always caught and never dereferenced. Calling it on loop from a callback running on one of loop's reactor threads or dispatch threads is a FATAL ERROR (abort()/SIGABRT) too, rather than a deadlock or a silent no-op. Hand the destroy to another thread, or do it after the callback returns. A queue-backed registration (one built with ccircq_selectable(), cdynmq_selectable() or cchan_selectable()) is unlinked from its queue's waiter list as it is freed, so a queue that outlives this ceventloop is never left with a dangling waiter pointer. The reverse order is the caller's job: Do not destroy a queue while a registration for it is live. ccircq_destroy(3), cdynmq_destroy(3) and cchan_destroy(3) detect that case and assert against it. Each remaining registration has a handlers.on_removed field (see ceventloop_add(3)). When it is set, it fires exactly once as part of the free, the same guarantee that an ordinary ceventloop_remove() call gives a live registration.
PARAMETERS
loop An lvalue of type ceventloop.
NOTES
The destroy never closes a registered fd and never destroys a registered queue or cchan. Each one stays with its owner, just as ceventloop_remove(3) leaves it. loop 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.
SEE ALSO
ceventloop_create(3), ceventloop_shutdown(3), ceventloop_remove(3)