C Collections

cthreadcomm · Section 3

cselect_va, cselect_timed_va

cselect_va, cselect_timed_va - wait for readiness on an inline list of selectables (convenience macro)

C Collections (libccollections, -lccollections)

#include <ccollections/cthreadcomm.h>
 
cselect_va(ready_index, ...);
cselect_timed_va(ready_index, timeout_us, ...);

cselect_va() builds a cselectable array from its variadic
arguments, counts the elements at compile time and passes everything
to cselect().
 
cselect_timed_va() does the same but calls cselect_timed() with
the timeout_us you give, in microseconds: UINT64_MAX waits forever, 0
polls, and any other value sets a deadline.
 
Each variadic argument must be a cselectable value, normally made
with ccircq_selectable(), cdynmq_selectable(),
cchan_selectable() or cselectable_fd(), with a dir of
cselect_read or cselect_write. Both macros evaluate each
argument exactly once.
 
Neither macro does the receive or the send itself, so after
ccol_success the caller has to make that call immediately:
ccircq_try_recv(), cdynmq_try_recv(),
ccircq_try_send() or cdynmq_send() when a queue
selectable wins, and read(2), recv(2), write(2) or send(2) on its
descriptor when an fd selectable wins.
 
When several selectables are ready, the one reported comes from the
fair rotation described in cselect(3), not always the first one in
the argument list.
 
Each macro expands to a GCC and Clang statement expression with a value
of type ccol_retval, so you can use it in an assignment or in a
condition.

ready_index  A size_t * that receives the 0-based index of the ready
             selectable. It is only valid when the macro returns
             ccol_success.
timeout_us   Microseconds to wait; UINT64_MAX waits forever. Only
             cselect_timed_va() takes this parameter.
...          One or more cselectable values; at least one is
             required.

ccol_success            A selectable is ready and *ready_index is set;
                        the caller then makes its own receive or send.
ccol_timed_out          The deadline passed with no selectable ready
                        (cselect_timed_va() only).
ccol_invalid_args       An argument is NULL or zero, or a selectable
                        holds a NULL queue pointer, a negative fd, a
                        type tag unknown to the library, or a dir that
                        is neither cselect_read nor
                        cselect_write.
ccol_not_enough_memory  An internal allocation failed: The waiter nodes
                        (one per selectable) or the fd state.
ccol_unexpected_failure A system call failed: poll(2), or the setup of
                        epoll or eventfd. An fd selectable that is not
                        open, or that names a regular file or a
                        directory, always gives this code, because
                        epoll(7) cannot watch such a file.

The selectables array is an ordinary local array on the stack, so the
only heap allocation is the one that cselect_timed() makes itself.

Both macros use a GCC and Clang statement expression,
__extension__({ ... }), which is not valid strict ISO C (-pedantic).
Without -pedantic they compile cleanly with -Wall -Wextra -Werror on
both GCC and Clang.

size_t idx;
 
/* Two circular queues and a cchan, all read-wait: */
ccol_retval r = cselect_va(&idx,
    ccircq_selectable(q0, cselect_read),
    ccircq_selectable(q1, cselect_read),
    cchan_selectable(ch, cselect_read));
if (r == ccol_success) {
    printf("ready selectable: %zu\n", idx);
    /* claim the message from whichever selectable won, for example: */
    cmessage msg;
    if (idx == 0 && ccircq_try_recv(q0, &msg) == ccol_success)
        free(msg.data);
}
 
/* Mixed: write-wait on q0, read-wait on ch: */
cselect_va(&idx,
    ccircq_selectable(q0, cselect_write),
    cchan_selectable(ch, cselect_read));
 
/* Timed wait with 200 ms deadline: */
r = cselect_timed_va(&idx, 200000,
    ccircq_selectable(q0, cselect_read));
if (r == ccol_timed_out) {
    puts("no source ready within 200 ms");
}

cselect(3), cselect_timed(3), ccircq_selectable(3), cdynmq_selectable(3), cchan_selectable(3), cselectable_fd(3)