Time and cancellation
Read the runtime clock, sleep until a deadline, wait on timers and signals, and cancel waits.
On this page
Time, timers, and signals
llam_now_nsMonotonic nanosecond clock used by deadlines.
uint64_t llam_now_ns(void);Return a monotonic timestamp in nanoseconds.
llam_sleep_nsRelative sleep for the current managed task.
int llam_sleep_ns(uint64_t duration_ns);Sleep the current task for a relative duration.
Calls from managed LLAM tasks park cooperatively. Calls outside a managed task block the calling OS thread after runtime initialization; before initialization they fail with EINVAL.
duration_ns- Sleep duration in nanoseconds.
Returns 0 on wake, -1 on cancellation/failure with errno set.
llam_sleep_untilSleep until an absolute deadline.
int llam_sleep_until(uint64_t deadline_ns);Sleep the current task until an absolute deadline.
Calls from managed LLAM tasks park cooperatively. Calls outside a managed task block the calling OS thread after runtime initialization; before initialization they fail with EINVAL. deadline_ns is an absolute llam_now_ns deadline; 0 is treated as an already-expired deadline.
deadline_ns- Absolute deadline in llam_now_ns() units.
Returns 0 on wake, -1 on cancellation/failure with errno set.
llam_timer_create_exCreate a waitable interval timer from llam_timer_opts_t.
int llam_timer_create_ex(const llam_timer_opts_t *opts,
size_t opts_size,
llam_timer_t **out);Create a waitable interval timer bound to the current runtime owner.
The timer is not a callback source. Callers wait explicitly with llam_timer_wait or llam_timer_wait_until, which keeps callback lifetime and reentrancy under caller control. opts.interval_ns must be non-zero. A first_deadline_ns of 0 arms the first tick for the current llam_now_ns timestamp. Timers are runtime-aware objects; using a timer from another managed runtime fails with EXDEV.
opts- Timer options, with a non-zero
interval_ns. opts_size- Size of the caller's
llam_timer_opts_t. outout- Receives the timer; set to NULL on failure. Must not be NULL.
Returns 0 on success, -1 with errno set.
llam_timer_createCreate a timer with only an interval.
int llam_timer_create(uint64_t interval_ns, llam_timer_t **out);Create an interval timer with a first tick after interval_ns.
interval_ns- Repeat interval in nanoseconds. Must not be 0.
outout- Receives the timer; set to NULL on failure. Must not be NULL.
Returns 0 on success, -1 with errno set.
llam_timer_waitWait for timer ticks.
int llam_timer_wait(llam_timer_t *timer, uint64_t *ticks_out);Wait for the next timer tick.
On success, ticks_out receives the number of elapsed intervals since the previous successful wait, clamped to at least 1. This lets periodic tasks account for missed ticks without drift. Managed tasks park cooperatively; unmanaged callers block the calling OS thread.
timer- Timer to wait on.
ticks_outout- Receives the elapsed interval count; set to 0 before the wait. Must not be NULL.
Returns 0 on success, -1 with errno set.
llam_timer_wait_untilWait for timer ticks until a deadline.
int llam_timer_wait_until(llam_timer_t *timer,
uint64_t deadline_ns,
uint64_t *ticks_out);Wait for the next timer tick until an absolute deadline.
deadline_ns uses llam_now_ns units. 0 is already expired.
timer- Timer to wait on.
deadline_ns- Absolute deadline.
ticks_outout- Receives the elapsed interval count; set to 0 before the wait. Must not be NULL.
Returns 0 on success, -1 with errno set.
llam_timer_resetReset first deadline and interval.
int llam_timer_reset(llam_timer_t *timer,
uint64_t first_deadline_ns,
uint64_t interval_ns);Reset a timer to a new first deadline and interval.
timer- Timer to reset.
first_deadline_ns- Absolute first deadline; 0 means now.
interval_ns- Repeat interval in nanoseconds. Must not be 0.
Returns 0 on success, -1 with errno set.
llam_timer_cancelCancel pending timer waits.
int llam_timer_cancel(llam_timer_t *timer);Cancel a timer and wake waiters with ECANCELED.
llam_timer_destroyDestroy a timer.
int llam_timer_destroy(llam_timer_t *timer);Returns 0 on success, -1 with EBUSY if another public operation is active.
LLAM_TIMER_OPTS_CURRENT_SIZESize token for llam_timer_create_ex.
#define LLAM_TIMER_OPTS_CURRENT_SIZECurrent size to pass to llam_timer_create_ex.
llam_signal_set_create_exCreate a signal wait set. Linux-only; other platforms return ENOTSUP.
int llam_signal_set_create_ex(const int *signos,
size_t signo_count,
const llam_signal_opts_t *opts,
size_t opts_size,
llam_signal_set_t **out);Create a POSIX signal wait set bound to the current runtime owner.
This is an opt-in process-level subscription. On Linux, LLAM blocks the requested signals for the calling thread and waits with sigtimedwait on a blocking helper using bounded slices so runtime stop and task cancellation can be observed. Call this before starting worker threads when possible so they inherit the signal mask. Duplicate or unsupported signal sets may fail with EBUSY or ENOTSUP. Other platforms currently return ENOTSUP.
signos- Signal numbers to subscribe to.
signo_count- Number of entries in
signos. opts- Optional signal options; pass NULL for defaults.
opts_size- Size of
optswhen non-NULL; must then be nonzero. outout- Receives the set; set to NULL on failure. Must not be NULL.
Returns 0 on success, -1 with errno set.
llam_signal_waitWait for a signal event.
int llam_signal_wait(llam_signal_set_t *set, llam_signal_event_t *out);Wait indefinitely for a signal in the set.
set- Signal wait set.
outout- Receives the signal event; cleared before the wait. Must not be NULL.
Returns 0 on success, -1 with errno set.
llam_signal_wait_untilWait for a signal event until a deadline.
int llam_signal_wait_until(llam_signal_set_t *set,
uint64_t deadline_ns,
llam_signal_event_t *out);Wait for a signal in the set until an absolute deadline.
set- Signal wait set.
deadline_ns- Absolute
llam_now_nsdeadline. outout- Receives the signal event; cleared before the wait. Must not be NULL.
Returns 0 on success, -1 with errno set.
llam_signal_set_destroyDestroy a signal wait set.
int llam_signal_set_destroy(llam_signal_set_t *set);Destroy a signal wait set. On the creating thread, only signals newly blocked by creation are unblocked. Destruction on another thread leaves both threads' signal masks unchanged; the creating thread remains responsible for restoring its mask.
LLAM_SIGNAL_OPTS_CURRENT_SIZESize token for llam_signal_set_create_ex.
#define LLAM_SIGNAL_OPTS_CURRENT_SIZECurrent size to pass to llam_signal_set_create_ex.
Cancellation
llam_cancel_token_createCreate a cancellation token.
llam_cancel_token_t *llam_cancel_token_create(void);llam_cancel_token_cancelRequest cancellation for current and future observers.
int llam_cancel_token_cancel(llam_cancel_token_t *token);Request cancellation for all current and future observers of token.
Calls racing with a completed destroy fail with EINVAL instead of dereferencing reclaimed token storage. Cancellation also fails with EBUSY while the public handle is already being torn down, and managed cross-runtime cancellation fails with EXDEV. Unmanaged host calls that need to wake waiters require a live owner runtime and fail with ENOTSUP after the owner runtime can no longer service wakeups.
llam_cancel_token_is_cancelledQuery cancellation state.
int llam_cancel_token_is_cancelled(const llam_cancel_token_t *token);Return non-zero when token has been cancelled.
Calls racing with a completed destroy fail with EINVAL instead of dereferencing reclaimed token storage. Queries also fail with EBUSY while the public handle is already being torn down, and managed cross-runtime queries fail with EXDEV.
llam_cancel_token_destroyDestroy a token when it has no live waiters or observers.
int llam_cancel_token_destroy(llam_cancel_token_t *token);Destroy a cancellation token.
The token must not have live waiters or task/I/O observers. Destroying a token with active observers fails with EBUSY; it does not invalidate active observers behind the caller's back. Destroy also fails with EBUSY while another thread is cancelling, querying, or retaining the token for a new task. Calls that race with a completed destroy fail with EINVAL, and managed cross-runtime destroy attempts fail with EXDEV.