LLAM v3.0.1

Sync and channels

Edit pageReport a problemLast modified: 30 November 2026Declared in <llam/runtime.h>

Coordinate tasks with mutexes, condition variables, channels, and select. These primitives park managed tasks instead of blocking worker threads.

On this page

Synchronization

llam_mutex_createCreate and destroy runtime-aware non-recursive mutexes.
llam_mutex_t *llam_mutex_create(void);

Create a runtime-aware mutex.

llam_mutex_destroyCreate and destroy runtime-aware non-recursive mutexes.
int llam_mutex_destroy(llam_mutex_t *mutex);

Destroy a runtime-aware mutex.

Destroy fails with EBUSY while a task owns or waits on the mutex, or while another public mutex operation is still pinned inside the handle registry. Managed cross-runtime destroy attempts fail with EXDEV.

llam_mutex_lockLock and park the task when contended.
int llam_mutex_lock(llam_mutex_t *mutex);

Lock a runtime-aware mutex, parking the task if needed.

Must be called from a managed LLAM task; outside task context fails with ENOTSUP. LLAM mutexes are non-recursive; locking a mutex already owned by the current task fails with EDEADLK. Contended locks apply a bounded priority-donation hint from latency-class waiters to the owner until unlock.

llam_mutex_lock_untilTimed lock with an absolute deadline.
int llam_mutex_lock_until(llam_mutex_t *mutex, uint64_t deadline_ns);

Lock a runtime-aware mutex until an absolute deadline.

Must be called from a managed LLAM task; outside task context fails with ENOTSUP. deadline_ns is an absolute llam_now_ns deadline; 0 is treated as an already-expired deadline. LLAM mutexes are non-recursive; locking a mutex already owned by the current task fails with EDEADLK. Contended locks apply the same bounded priority-donation hint as llam_mutex_lock.

llam_mutex_trylockNon-parking lock attempt.
int llam_mutex_trylock(llam_mutex_t *mutex);

Try to lock a runtime-aware mutex without parking.

Returns 0 on lock acquisition, or -1 with errno set to EBUSY when already locked, EINVAL for invalid arguments, or ENOTSUP outside a managed task.

llam_mutex_unlockUnlock and wake one waiter when present.
int llam_mutex_unlock(llam_mutex_t *mutex);

Unlock a runtime-aware mutex and wake a waiter if one exists.

Returns 0 on success, or -1 with errno set to EPERM when the current task does not own the mutex, EINVAL for invalid arguments, or ENOTSUP outside a managed task.

llam_cond_createCreate and destroy runtime-aware condition variables.
llam_cond_t *llam_cond_create(void);

Create a runtime-aware condition variable.

llam_cond_destroyCreate and destroy runtime-aware condition variables.
int llam_cond_destroy(llam_cond_t *cond);

Destroy a runtime-aware condition variable.

Destroy fails with EBUSY while a task is currently waiting on the condition, including the interval after signal/broadcast has selected the waiter but before that task has returned from llam_cond_wait(), or while another public condition operation is still pinned inside the handle registry. Managed cross-runtime destroy attempts fail with EXDEV.

llam_cond_waitWait while atomically releasing and reacquiring a LLAM mutex.
int llam_cond_wait(llam_cond_t *cond, llam_mutex_t *mutex);

Wait on a condition variable and atomically release/reacquire mutex.

Must be called from a managed LLAM task; outside task context fails with ENOTSUP. The caller must own mutex on entry. LLAM atomically releases it while waiting and reacquires it before returning, including signal, broadcast, timeout, cancellation, runtime stop, and spurious wake paths. The reacquire has no deadline and cannot be cancelled, so a stopping runtime cannot drain until the mutex's owner unlocks it. Callers must wait in a predicate loop.

llam_cond_wait_untilTimed condition wait.
int llam_cond_wait_until(llam_cond_t *cond,
                         llam_mutex_t *mutex,
                         uint64_t deadline_ns);

Wait on a condition variable until an absolute deadline.

Must be called from a managed LLAM task; outside task context fails with ENOTSUP. The caller must own mutex on entry. LLAM atomically releases it while waiting and reacquires it before returning, including signal, broadcast, timeout, cancellation, runtime stop, and spurious wake paths. The reacquire has no deadline and cannot be cancelled, so the call can return later than deadline_ns, and a stopping runtime cannot drain until the mutex's owner unlocks it. Callers must wait in a predicate loop. deadline_ns is an absolute llam_now_ns deadline; 0 is treated as an already-expired deadline.

llam_cond_signalWake one condition waiter.
int llam_cond_signal(llam_cond_t *cond);

Wake one condition-variable waiter.

May be called with or without the associated mutex held. Calls outside a managed LLAM task are allowed while the owner runtime is live.

Returns 0 on success, or -1 with errno set to EINVAL for invalid arguments, EBUSY when the public handle is already being torn down, EXDEV for cross-runtime managed use or a destroyed explicit owner runtime, or ENOTSUP when an unmanaged host caller touches a condition after the default owner runtime has shut down.

llam_cond_broadcastWake all condition waiters.
int llam_cond_broadcast(llam_cond_t *cond);

Wake all condition-variable waiters.

May be called with or without the associated mutex held. Calls outside a managed LLAM task are allowed while the owner runtime is live.

Returns 0 on success, or -1 with errno set to EINVAL for invalid arguments, EBUSY when the public handle is already being torn down, EXDEV for cross-runtime managed use or a destroyed explicit owner runtime, or ENOTSUP when an unmanaged host caller touches a condition after the default owner runtime has shut down.

Reader/writer locks

llam_rwlock_createCreate a reader/writer lock for the current or specified runtime.
llam_rwlock_t *llam_rwlock_create(void);

Create a nonrecursive reader/writer lock owned by the current runtime. Concurrent readers can enter while no writer owns or waits for the lock. Acquisition and release require LLAM task context. Each task must unlock before exit. Recursive acquisition and read-to-write upgrade fail EDEADLK. Try calls fail EBUSY when acquisition would wait; unlock by a nonowner fails EPERM. Destruction requires no holders or in-flight operations.

llam_runtime_rwlock_createCreate a reader/writer lock for the current or specified runtime.
llam_rwlock_t *llam_runtime_rwlock_create(llam_runtime_t *runtime);

Create a reader/writer lock owned by the specified runtime.

llam_rwlock_destroyDestroy a lock after its holders and waiters have drained.
int llam_rwlock_destroy(llam_rwlock_t *lock);

Create a nonrecursive reader/writer lock owned by the current runtime. Concurrent readers can enter while no writer owns or waits for the lock. Acquisition and release require LLAM task context. Each task must unlock before exit. Recursive acquisition and read-to-write upgrade fail EDEADLK. Try calls fail EBUSY when acquisition would wait; unlock by a nonowner fails EPERM. Destruction requires no holders or in-flight operations.

llam_rwlock_read_lockAcquire shared access, with an optional absolute deadline.
int llam_rwlock_read_lock(llam_rwlock_t *lock);

Create a nonrecursive reader/writer lock owned by the current runtime. Concurrent readers can enter while no writer owns or waits for the lock. Acquisition and release require LLAM task context. Each task must unlock before exit. Recursive acquisition and read-to-write upgrade fail EDEADLK. Try calls fail EBUSY when acquisition would wait; unlock by a nonowner fails EPERM. Destruction requires no holders or in-flight operations.

llam_rwlock_read_untilAcquire shared access, with an optional absolute deadline.
int llam_rwlock_read_until(llam_rwlock_t *lock, uint64_t deadline_ns);

Create a nonrecursive reader/writer lock owned by the current runtime. Concurrent readers can enter while no writer owns or waits for the lock. Acquisition and release require LLAM task context. Each task must unlock before exit. Recursive acquisition and read-to-write upgrade fail EDEADLK. Try calls fail EBUSY when acquisition would wait; unlock by a nonowner fails EPERM. Destruction requires no holders or in-flight operations.

llam_rwlock_write_lockAcquire exclusive access, with an optional absolute deadline.
int llam_rwlock_write_lock(llam_rwlock_t *lock);

Create a nonrecursive reader/writer lock owned by the current runtime. Concurrent readers can enter while no writer owns or waits for the lock. Acquisition and release require LLAM task context. Each task must unlock before exit. Recursive acquisition and read-to-write upgrade fail EDEADLK. Try calls fail EBUSY when acquisition would wait; unlock by a nonowner fails EPERM. Destruction requires no holders or in-flight operations.

llam_rwlock_write_untilAcquire exclusive access, with an optional absolute deadline.
int llam_rwlock_write_until(llam_rwlock_t *lock, uint64_t deadline_ns);

Create a nonrecursive reader/writer lock owned by the current runtime. Concurrent readers can enter while no writer owns or waits for the lock. Acquisition and release require LLAM task context. Each task must unlock before exit. Recursive acquisition and read-to-write upgrade fail EDEADLK. Try calls fail EBUSY when acquisition would wait; unlock by a nonowner fails EPERM. Destruction requires no holders or in-flight operations.

llam_rwlock_try_readAttempt acquisition without parking.
int llam_rwlock_try_read(llam_rwlock_t *lock);

Create a nonrecursive reader/writer lock owned by the current runtime. Concurrent readers can enter while no writer owns or waits for the lock. Acquisition and release require LLAM task context. Each task must unlock before exit. Recursive acquisition and read-to-write upgrade fail EDEADLK. Try calls fail EBUSY when acquisition would wait; unlock by a nonowner fails EPERM. Destruction requires no holders or in-flight operations.

llam_rwlock_try_writeAttempt acquisition without parking.
int llam_rwlock_try_write(llam_rwlock_t *lock);

Create a nonrecursive reader/writer lock owned by the current runtime. Concurrent readers can enter while no writer owns or waits for the lock. Acquisition and release require LLAM task context. Each task must unlock before exit. Recursive acquisition and read-to-write upgrade fail EDEADLK. Try calls fail EBUSY when acquisition would wait; unlock by a nonowner fails EPERM. Destruction requires no holders or in-flight operations.

llam_rwlock_unlockRelease the calling task's acquisition.
int llam_rwlock_unlock(llam_rwlock_t *lock);

Create a nonrecursive reader/writer lock owned by the current runtime. Concurrent readers can enter while no writer owns or waits for the lock. Acquisition and release require LLAM task context. Each task must unlock before exit. Recursive acquisition and read-to-write upgrade fail EDEADLK. Try calls fail EBUSY when acquisition would wait; unlock by a nonowner fails EPERM. Destruction requires no holders or in-flight operations.

Channels and select

llam_channel_createCreate and destroy bounded pointer channels.
llam_channel_t *llam_channel_create(size_t capacity);

Create a pointer-valued channel.

capacity
Number of bounded buffer slots. Must be at least 1.

Returns Channel handle on success, NULL on failure with errno set.

llam_channel_destroyCreate and destroy bounded pointer channels.
int llam_channel_destroy(llam_channel_t *channel);

Destroy a channel after all users have stopped accessing it.

Destroy fails with EBUSY while buffered values, parked senders/receivers, close-woken waiters, or another public channel operation remain. Close the channel and drain buffered values before destroying when producers may have sent data.

llam_channel_sendSend and park when the channel is full.
int llam_channel_send(llam_channel_t *channel, void *value);

Send a pointer value, parking the task if the channel is full.

Must be called from a managed LLAM task; outside task context fails with ENOTSUP.

llam_channel_try_sendNon-parking send attempt.
int llam_channel_try_send(llam_channel_t *channel, void *value);

Try to send a pointer value without parking.

This operation never parks. Calls from managed tasks and unmanaged OS threads are both allowed after runtime initialization. Full channels fail with EAGAIN. Timed send APIs use ETIMEDOUT only when a real deadline expires.

llam_channel_send_untilTimed send.
int llam_channel_send_until(llam_channel_t *channel,
                            void *value,
                            uint64_t deadline_ns);

Send a pointer value until an absolute deadline.

Must be called from a managed LLAM task; outside task context fails with ENOTSUP. deadline_ns is an absolute llam_now_ns deadline; 0 is treated as an already-expired deadline.

llam_channel_recv_resultReceive with explicit out parameter. Use when NULL is a valid payload.
int llam_channel_recv_result(llam_channel_t *channel, void **out);

Receive a pointer value, parking the task if the channel is empty.

Must be called from a managed LLAM task; outside task context fails with ENOTSUP. out is cleared to NULL before validation/waiting so failure paths never expose a stale payload pointer.

channel
Channel to receive from.
out
Destination for the received pointer. Must not be NULL.

Returns 0 on receive, -1 on close/cancellation/failure with errno set.

llam_channel_try_recv_resultNon-parking receive attempt with explicit out parameter.
int llam_channel_try_recv_result(llam_channel_t *channel, void **out);

Try to receive a pointer value without parking.

This operation never parks. Calls from managed tasks and unmanaged OS threads are both allowed after runtime initialization. Already-buffered values may also be drained after runtime shutdown so host cleanup code can destroy channels after stopping the scheduler. Empty open channels fail with EAGAIN while the runtime is alive and ENOTSUP after shutdown. Timed receive APIs use ETIMEDOUT only when a real deadline expires. out is cleared to NULL before validation so failure paths never expose a stale payload pointer.

channel
Channel to receive from.
out
Destination for the received pointer. Must not be NULL.

Returns 0 on receive, -1 on empty/close/cancellation/failure with errno set.

llam_channel_recv_until_resultTimed receive with explicit out parameter.
int llam_channel_recv_until_result(llam_channel_t *channel,
                                   uint64_t deadline_ns,
                                   void **out);

Receive a pointer value until an absolute deadline.

Must be called from a managed LLAM task; outside task context fails with ENOTSUP. deadline_ns is an absolute llam_now_ns deadline; 0 is treated as an already-expired deadline. out is cleared to NULL before validation/waiting so failure paths never expose a stale payload pointer.

channel
Channel to receive from.
deadline_ns
Absolute deadline in llam_now_ns() units.
out
Destination for the received pointer. Must not be NULL.

Returns 0 on receive, -1 on close/timeout/cancellation/failure with errno set.

llam_channel_recvConvenience receive wrapper.
void *llam_channel_recv(llam_channel_t *channel);

Convenience wrapper around llam_channel_recv_result.

Use the result API when NULL is a valid payload; outside task context returns NULL with errno set to ENOTSUP.

llam_channel_recv_untilConvenience timed receive wrapper.
void *llam_channel_recv_until(llam_channel_t *channel,
                              uint64_t deadline_ns);

Receive a pointer value until an absolute deadline.

Convenience wrapper around llam_channel_recv_until_result. If NULL is a valid payload for your channel, use llam_channel_recv_until_result. Outside task context returns NULL with errno set to ENOTSUP. deadline_ns is an absolute llam_now_ns deadline; 0 is treated as an already-expired deadline.

llam_channel_closeClose the channel and wake blocked senders/receivers.
int llam_channel_close(llam_channel_t *channel);

Close a channel and wake blocked senders/receivers.

Closing is idempotent. Sends after close fail with EPIPE. Buffered values sent before close remain drainable; receives fail with EPIPE only after the buffer is empty and no parked sender can hand off a value. NULL is a valid payload; use result-style receive APIs to distinguish it from failure. Closing an idle channel is cleanup-safe after explicit runtime destruction; closing a channel with parked waiters still requires a live owner runtime.

llam_channel_selectSelect one ready send/receive operation.
int llam_channel_select(llam_select_op_t *ops,
                        size_t op_count,
                        uint64_t deadline_ns,
                        size_t *selected_index);

Select one ready channel send/receive operation.

The current managed task is parked on all requested channel queues when no operation is immediately ready. The first channel operation to complete wins; the selected operation index is returned and remaining queued wait nodes are removed before this call returns. A selected receive on a closed, drained channel sets the operation's result_errno to EPIPE and stores NULL in recv_out; a selected send on a closed channel sets result_errno to EPIPE without enqueueing send_value. deadline_ns is an absolute llam_now_ns deadline; 0 performs a single non-blocking scan. op_count must be in the range 1..LLAM_CHANNEL_SELECT_MAX_OPS.

ops
Operation array.
op_count
Number of operations.
deadline_ns
Absolute deadline, or UINT64_MAX for no deadline.
selected_index
Receives selected operation index. Must not be NULL.

Returns 0 when an operation is selected, -1 on timeout/failure with errno set.

LLAM_CHANNEL_SELECT_MAX_OPSMaximum operation count accepted by llam_channel_select.
#define LLAM_CHANNEL_SELECT_MAX_OPS

The bound keeps malformed FFI calls from forcing unbounded array walks or allocation attempts before LLAM can validate each operation.

↑↓ Navigate↵ Openesc CloseSearching the 3.0.1 docs