Blocking and diagnostics
Call foreign blocking code without pinning a scheduler worker, and inspect a running runtime with stats and dumps.
On this page
Blocking integration
llam_call_blocking_resultRun blocking work and return through an explicit out parameter.
int llam_call_blocking_result(llam_blocking_fn fn, void *arg, void **out);Execute a blocking callback through the runtime blocking path.
This is the unambiguous FFI-safe blocking API. A callback that legitimately returns NULL still succeeds and stores NULL in out. If cancellation reaches a queued callback before a worker starts it, the callback is skipped. If cancellation reaches a callback that is already running, LLAM reports ECANCELED only after the callback returns, so arg remains owned by the callback for its full execution. out is cleared to NULL before validation/submission so failure paths never expose a stale callback result.
fn- Blocking callback. Must not be NULL.
arg- User pointer passed to fn.
out- Destination for the callback result. Must not be NULL.
Returns 0 on callback completion, -1 on submission/cancellation failure with errno set.
llam_call_blockingConvenience blocking callback API.
void *llam_call_blocking(llam_blocking_fn fn, void *arg);Convenience wrapper around llam_call_blocking_result.
Cannot distinguish a successful NULL result from API failure; production/FFI callers should use llam_call_blocking_result unless NULL is outside the callback's valid result domain.
Returns Callback result, or NULL if the callback returned NULL or submission failed.
llam_call_native_affine_resultRun a callback on a native-affine task's own OS thread. See Managed C.
int llam_call_native_affine_result(llam_blocking_fn fn,
void *arg,
void **out);Execute one native callback on the current task's dedicated native thread.
The compiler classifies callers as NATIVE_AFFINE. A qualified affine task releases its logical processor before the callback and reacquires a fresh execute/drain lease on the same native thread before returning. The callback may block and use native TLS; it cannot call stateful LLAM APIs or leave by longjmp/exception. Its result, errno and Windows LastError are preserved. Returns 0 after callback completion, including a NULL callback result. A stop or cancellation after callback entry does not skip reacquisition or discard the result. Before callback entry, invalid arguments return EINVAL, a missing affine execution context returns ENOTSUP, stop/cancellation returns ECANCELED, and a racing processor-ownership change returns EBUSY. On those failures *out is NULL when out is valid. Native callback reentry is rejected before touching caller arguments, as for other LLAM APIs.
fn- Callback to run. Must not be NULL.
arg- User pointer passed to
fn. outout- Receives the callback result; NULL is a valid result. Must not be NULL.
Returns 0 after callback completion, -1 with errno set before callback entry.
llam_enter_blockingMark the current task as entering an opaque blocking region.
int llam_enter_blocking(void);Calls outside a managed LLAM task are no-ops and return 0.
Returns 0 on success, -1 on failure with errno set.
llam_leave_blockingMark the current task as leaving an opaque blocking region.
int llam_leave_blocking(void);Calls outside a managed LLAM task are no-ops and return 0.
Returns 0 on success, -1 on failure with errno set.
Diagnostics and introspection
llam_runtime_collect_stats_exSize-aware stats snapshot for the process-default runtime.
int llam_runtime_collect_stats_ex(llam_runtime_stats_t *stats,
size_t stats_size);Collect a best-effort snapshot of runtime counters with explicit size.
This is the ABI-stable form preferred by dynamic loaders and FFI bindings. LLAM writes only the overlapping prefix known to the loaded library; caller tail bytes from newer struct definitions are left untouched. If stats and stats_size are valid, failure paths clear the known caller-visible prefix before returning so stale counters are not mistaken for a fresh snapshot.
stats- Destination statistics object. Must not be NULL.
stats_size- Size of the caller's
llam_runtime_stats_tdefinition.
Returns 0 on success, -1 on failure with errno set.
llam_runtime_collect_stats_ex_handleSize-aware stats snapshot for an explicit runtime handle.
int llam_runtime_collect_stats_ex_handle(
llam_runtime_t *runtime,
llam_runtime_stats_t *stats,
size_t stats_size);Collect a best-effort snapshot from an explicit runtime handle.
This is the handle-scoped form for embedders that use llam_runtime_create/llam_runtime_run_handle instead of the legacy default runtime wrappers. Unknown runtime handles fail with EINVAL. If stats and stats_size are valid, failure paths clear the caller-visible prefix.
runtime- Runtime returned by
llam_runtime_createorllam_runtime_default; not NULL. stats- Destination statistics object. Must not be NULL.
stats_size- Size of the caller's
llam_runtime_stats_tdefinition.
Returns 0 on success, -1 on failure with errno set.
llam_runtime_collect_statsConvenience stats snapshot for the process-default runtime.
int llam_runtime_collect_stats(llam_runtime_stats_t *stats);Collect a best-effort snapshot of runtime counters.
Convenience wrapper around llam_runtime_collect_stats_ex.
stats- Destination statistics object. Must not be NULL.
Returns 0 on success, -1 on failure with errno set.
llam_runtime_write_stats_jsonWrite JSON statistics for the process-default or an explicit runtime.
int llam_runtime_write_stats_json(int fd);Write runtime counters as one JSON object to an fd.
The JSON field set is additive and intended for dashboards, stress logs, and automation that should not parse llam_dump_runtime_state. The call writes a single newline-terminated object. Managed and unmanaged callers are both allowed.
fd- Destination file descriptor.
Returns 0 on success, -1 on invalid fd/write failure with errno set.
llam_runtime_write_stats_json_handleWrite JSON statistics for the process-default or an explicit runtime.
int llam_runtime_write_stats_json_handle(llam_runtime_t *runtime, int fd);Write JSON statistics for an explicit runtime.
runtime- Runtime to report; not NULL.
fd- Destination file descriptor.
Returns 0 on success, -1 with errno set.
llam_dump_runtime_stateBest-effort dump for the process-default or an explicit runtime.
void llam_dump_runtime_state(int fd);Write a human-readable runtime dump to an fd.
The dump is intended for rare hang and crash diagnostics. It includes lifecycle/stop state, active I/O waiter counts, node submit/watch queues, shard wake and I/O ownership state, and per-task wait ownership including cancellation, select, blocking-job, and I/O request details.
fd- Destination file descriptor.
llam_dump_runtime_state_handleBest-effort dump for the process-default or an explicit runtime.
void llam_dump_runtime_state_handle(llam_runtime_t *runtime, int fd);Write the same best-effort diagnostic dump for an explicit runtime.
runtime- Runtime to dump; a NULL handle sets
errnotoEINVAL. fd- Destination file descriptor; a negative value sets
errnotoEINVAL.
LLAM_RUNTIME_STATS_CURRENT_SIZESize token for llam_runtime_collect_stats_ex and handle-scoped stats.
#define LLAM_RUNTIME_STATS_CURRENT_SIZECurrent size to pass to llam_runtime_collect_stats_ex.
llam_task_flagsReturn task spawn/runtime flags.
uint32_t llam_task_flags(const llam_task_t *task);task- Task handle.
Returns Bitwise OR of LLAM_SPAWN_F_* values, or 0 for invalid, foreign-runtime, or NULL handles.
llam_task_idReturn the runtime-assigned task id.
uint64_t llam_task_id(const llam_task_t *task);Return the runtime-assigned task id, or 0 for invalid or foreign handles.
llam_task_state_nameReturn a stable task state string.
const char *llam_task_state_name(const llam_task_t *task);Return a stable task state string, or "UNKNOWN" for invalid or foreign handles.
llam_task_classReturn the scheduler class for a task.
uint32_t llam_task_class(const llam_task_t *task);Return the scheduler class, or default class for invalid or foreign handles.
llam_current_taskReturn the current managed task, or NULL outside LLAM.
llam_task_t *llam_current_task(void);Return the currently running task, or NULL outside a LLAM task.