include/sig/runtime/sig_loop.h
typedef struct sig_loop sig_loop;
typedef struct sig_loop_watch sig_loop_watch;
typedef struct sig_loop_timer sig_loop_timer;
sig_status_t sig_loop_create(sig_loop ** out)
| Parameter | Type |
|---|---|
out | sig_loop ** |
void sig_loop_destroy(sig_loop * loop)
| Parameter | Type |
|---|---|
loop | sig_loop * |
Frees the loop and any watches and timers still registered (their fds are not closed). Not while sig_loop_run() is running. NULL is a no-op.
typedef void (*sig_loop_fd_fn)(int fd, unsigned events, void *ctx);
Called when fd is ready; `events` is a SIG_POLL_* mask.
sig_status_t sig_loop_watch_fd(sig_loop * loop, int fd, unsigned events, sig_loop_fd_fn cb, void * ctx, sig_loop_watch ** out)
| Parameter | Type |
|---|---|
loop | sig_loop * |
fd | int |
events | unsigned |
cb | sig_loop_fd_fn |
ctx | void * |
out | sig_loop_watch ** |
Watches fd for `events` (SIG_POLL_READ / SIG_POLL_WRITE; errors are always reported). *out is the handle for modify/unwatch. The caller keeps ownership of the fd and must sig_loop_unwatch() before closing it. SIG_ERR_EXISTS if fd is already watched.
sig_status_t sig_loop_watch_modify(sig_loop_watch * watch, unsigned events)
| Parameter | Type |
|---|---|
watch | sig_loop_watch * |
events | unsigned |
Watches fd for `events` (SIG_POLL_READ / SIG_POLL_WRITE; errors are always reported). *out is the handle for modify/unwatch. The caller keeps ownership of the fd and must sig_loop_unwatch() before closing it. SIG_ERR_EXISTS if fd is already watched.
void sig_loop_unwatch(sig_loop_watch * watch)
| Parameter | Type |
|---|---|
watch | sig_loop_watch * |
Watches fd for `events` (SIG_POLL_READ / SIG_POLL_WRITE; errors are always reported). *out is the handle for modify/unwatch. The caller keeps ownership of the fd and must sig_loop_unwatch() before closing it. SIG_ERR_EXISTS if fd is already watched.
typedef void (*sig_loop_timer_fn)(void *ctx);
One-shot timer: cb runs once, no earlier than `deadline` (absolute CLOCK_MONOTONIC, as from sig_time_now()). The handle is valid until the callback is entered or sig_loop_timer_cancel() is called; a holder must forget it in the callback.
sig_status_t sig_loop_timer_start(sig_loop * loop, const struct timespec * deadline, sig_loop_timer_fn cb, void * ctx, sig_loop_timer ** out)
| Parameter | Type |
|---|---|
loop | sig_loop * |
deadline | const struct timespec * |
cb | sig_loop_timer_fn |
ctx | void * |
out | sig_loop_timer ** |
One-shot timer: cb runs once, no earlier than `deadline` (absolute CLOCK_MONOTONIC, as from sig_time_now()). The handle is valid until the callback is entered or sig_loop_timer_cancel() is called; a holder must forget it in the callback.
void sig_loop_timer_cancel(sig_loop_timer * timer)
| Parameter | Type |
|---|---|
timer | sig_loop_timer * |
One-shot timer: cb runs once, no earlier than `deadline` (absolute CLOCK_MONOTONIC, as from sig_time_now()). The handle is valid until the callback is entered or sig_loop_timer_cancel() is called; a holder must forget it in the callback.
typedef void (*sig_loop_step_fn)(void *ctx, bool *out_has_deadline, struct timespec *out_deadline);
The step hook runs at the top of every iteration and reports when it next needs to run (*out_has_deadline false: only when woken). The host points it at sig_runtime_step().
void sig_loop_set_step(sig_loop * loop, sig_loop_step_fn fn, void * ctx)
| Parameter | Type |
|---|---|
loop | sig_loop * |
fn | sig_loop_step_fn |
ctx | void * |
The step hook runs at the top of every iteration and reports when it next needs to run (*out_has_deadline false: only when woken). The host points it at sig_runtime_step().
sig_status_t sig_loop_run(sig_loop * loop)
| Parameter | Type |
|---|---|
loop | sig_loop * |
Runs until sig_loop_stop(): step, fire due timers, sleep until the earliest of the step deadline and the next timer or fd event, dispatch, repeat. A stop requested before the loop starts makes it return at once.
void sig_loop_wake(sig_loop * loop)
| Parameter | Type |
|---|---|
loop | sig_loop * |
Any thread. Makes the loop run its step hook now.
void sig_loop_stop(sig_loop * loop)
| Parameter | Type |
|---|---|
loop | sig_loop * |
Any thread. Makes sig_loop_run() return after the current iteration.