C Collections

cthreadpool · Section 3

cthrpool_timed_submit

cthrpool_timed_submit - submit a task with a timeout

C Collections (libccollections, -lccollections)

#include <ccollections/cthreadpool.h>
 
ccol_retval cthrpool_timed_submit(cthrpool pool,
                                    void (*fn)(void *),
                                    void *arg,
                                    void (*on_complete)(void *arg, bool ran),
                                    uint64_t timeout_us);

The same as cthrpool_submit, except that with a bounded queue it waits
at most timeout_us microseconds for a free slot, counted from the
call. The timeout is turned into an absolute deadline on
CLOCK_MONOTONIC, so a change to the system's wall clock (by an
administrator, by NTP, or when a virtual machine resumes) makes the
wait neither longer nor shorter.
 
A timeout_us of 0 makes the call the same as cthrpool_try_submit, which
doesn't wait.
 
A timeout too large to form a deadline, such as UINT64_MAX, waits as
long as the queue stays full: The deadline saturates at the latest
time that can be represented and never wraps into the past.
 
A call from a task running on this same pool, or from the on_complete
callback of that task, never waits while a bounded queue is at
capacity, because only the workers of this pool can free a slot and
the caller is one of them. It returns ccol_container_full at once,
whatever timeout_us says.

pool         Thread pool handle
fn           Task function (must not be NULL)
arg          The library gives it to fn and on_complete (may be NULL)
on_complete  Called once for the task: on_complete(arg, true) after
             fn returns, or on_complete(arg, false) when the pool
             discards the task unrun (may be NULL)
timeout_us   Longest time to wait, in microseconds (0 = do not wait)

ccol_success           - task enqueued.
ccol_invalid_args      - pool is CTHRPOOL_NULL or a stale/already-
                         destroyed handle, or fn is NULL.
ccol_not_enough_memory - task struct allocation failed.
ccol_not_permitted     - pool is shutting down.
ccol_container_full    - the queue is full and timeout_us is 0, or the
                         caller is a task of this pool (see above).
ccol_timed_out         - timeout elapsed before a slot became free.
ccol_unexpected_failure - clock_gettime or pthread_cond_timedwait
                          returned an unexpected error.

cthrpool_submit(3), cthrpool_try_submit(3)