C Collections

cthreadcomm · Section 3

ceventloop_pause

ceventloop_pause - temporarily stop delivering events for an fd registration, without destroying it

C Collections (libccollections, -lccollections)

#include <ccollections/cthreadcomm.h>
 
ccol_retval ceventloop_pause(ceventloop loop, ceventloop_reg reg);

Works only on fd registrations. A registration made from a queue or a
cchan selectable gives ccol_invalid_args, the same restriction
that ceventloop_modify has.
 
ceventloop_remove unregisters reg completely and queues it to be
freed later. ceventloop_pause leaves reg as it is: It keeps its
slot on the shared epoll entry of its fd, it still counts toward
ceventloop_reg_count, and its ceventloop_reg_generation
doesn't change.
 
The call recomputes the fd's combined epoll interest mask without reg.
When the fd has another direction that is live and not paused, that is
a single EPOLL_CTL_MOD call. When every direction on the fd is paused
after this call, the fd is taken out of the kernel's epoll interest set
completely, so it produces no wake-up of any kind while it stays fully
paused, not even for its own error and hangup conditions, which the
kernel reports whatever interest mask you ask for. No on_readable,
on_writable or on_error callback fires for reg while it is paused. The
other direction on the same fd, if there is one, is not affected.
 
This is the cheap alternative to a ceventloop_remove followed
later by a ceventloop_add, for a pattern where the same logical
registration keeps coming back: A connection that goes to a worker
thread for blocking body I/O and comes back to the reactor for its next
request, for example. A fresh add and remove pair costs one heap
allocation and one free, plus churn in the chmap of the fd registry,
and pause and resume avoid both.
 
With more than one reactor thread (num_reactor_threads > 1)
EPOLLONESHOT is in use, and the dispatch worker runs its own re-arm
once more after a paused callback returns. That is harmless: A re-arm
always builds the epoll mask from the current state, so the extra
re-arm applies the same mask without reg, and there is no race. Pause
and resume still avoid the allocation and the registry changes in this
case as well.
 
Pausing a reg that is already paused succeeds and does nothing.

loop  ceventloop the registration belongs to.
reg   Registration to pause.

ccol_success        reg is paused, by this call or an earlier one.
ccol_invalid_args   loop is CEVENTLOOP_NULL, a stale handle,
                    or a handle that has already been destroyed;
                    or reg is CEVENTLOOP_REG_NULL or fails to
                    resolve for another reason, such as a removal at
                    the same time; or reg is a registration for a
                    queue or a cchan.

Safe to call at the same time as ceventloop_remove(), and from a
callback running on one of the reactor threads.

/* Inside an on_readable callback, once a full set of request headers is
   parsed and just before the connection goes to a worker thread for its
   own direct, blocking body I/O. reg is the handle that the callback
   receives: */
conn->reg = reg;
ceventloop_pause(loop, reg);
if (cthrpool_submit(worker_pool, process_request, conn, NULL) != ccol_success)
    ceventloop_resume(loop, reg);  /* no worker: the reactor keeps it */
/* ... later, on the worker thread, after the server sends the
   response, when the connection is ready for its next request: */
ceventloop_resume(loop, conn->reg);

ceventloop_resume(3), ceventloop_add(3), ceventloop_remove(3), ceventloop_modify(3), ceventloop_reg_generation(3)