LLAM v3.0.1

Lifecycle

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

Create, run, stop, and destroy runtimes.

On this page

Lifecycle

Embedding code should use the handle-scoped lifecycle: llam_runtime_create(), llam_runtime_spawn_ex(), llam_runtime_run_handle(), and llam_runtime_destroy(). The process-default APIs select the same required asynchronous policy for single-runtime programs. Use the explicitly named *_compat initializers for boundary/safepoint execution.

llam_runtime_opts_initFill runtime options with ABI-safe defaults.
int llam_runtime_opts_init(llam_runtime_opts_t *opts, size_t opts_size);

Initialize runtime options to the library defaults.

This is the ABI-stable option initializer preferred by dynamic loaders and FFI bindings. LLAM writes only the overlapping prefix known to the loaded library; caller-side tail bytes from newer struct definitions are left untouched.

opts
Destination options object. Must not be NULL.
opts_size
Size of the caller's llam_runtime_opts_t definition.

Returns 0 on success, -1 with errno set on invalid arguments.

llam_runtime_init_exInitialize the process-default asynchronous runtime with an explicit option size.
int llam_runtime_init_ex(const llam_runtime_opts_t *opts, size_t opts_size);

Initialize the process-default runtime with required asynchronous preemption.

This is the ABI-stable form preferred by dynamic loaders and FFI bindings. LLAM copies only the overlapping prefix of opts and treats missing tail fields as runtime defaults, so older bindings can run against newer libraries that appended fields to llam_runtime_opts_t. Embedders that own scheduler lifetime should prefer the explicit handle lifecycle: llam_runtime_create, llam_runtime_spawn_ex, llam_runtime_run_handle, and llam_runtime_destroy. The process-default lifecycle remains a convenience path for simple single-runtime programs.

opts
Optional runtime options; pass NULL for defaults.
opts_size
Size of the caller's llam_runtime_opts_t definition.

Returns 0 on success, -1 on failure with errno set.

llam_runtime_initConvenience default-runtime initializer.
int llam_runtime_init(const llam_runtime_opts_t *opts);

Initialize the process-default asynchronous runtime.

Convenience wrapper around llam_runtime_init_ex. New embedding code should prefer llam_runtime_create and drive the returned handle.

opts
Optional runtime options; pass NULL for defaults.

Returns 0 on success, -1 on failure with errno set.

llam_runtime_init_compat_exInitialize the process-default compatibility runtime with an explicit option size.
int llam_runtime_init_compat_ex(const llam_runtime_opts_t *opts,
                                size_t opts_size);

Initialize the process-default runtime for plain C tasks.

Explicit compatibility runtime: boundary/safepoint preemption, including AUTO/STRICT request policies. These functions do not require managed images.

opts
Optional runtime options; pass NULL for defaults.
opts_size
Size of opts when non-NULL; must then be nonzero.

Returns 0 on success, -1 with errno set.

llam_runtime_init_compatConvenience default-runtime compatibility initializer.
int llam_runtime_init_compat(const llam_runtime_opts_t *opts);

Initialize the process-default runtime for plain C tasks.

Explicit compatibility runtime: boundary/safepoint preemption, including AUTO/STRICT request policies. These functions do not require managed images.

opts
Optional runtime options; pass NULL for defaults.

Returns 0 on success, -1 with errno set.

llam_runtime_defaultReturn the default runtime handle.
llam_runtime_t *llam_runtime_default(void);

Return the process-default runtime handle.

This handle names the legacy process-default runtime used by llam_runtime_init, llam_spawn, llam_run, and llam_runtime_shutdown. It is also accepted by explicit-handle APIs when an embedder wants to drive the default runtime through the canonical handle surface.

Returns The default runtime handle.

llam_runtime_createExplicit runtime with required asynchronous admission.
int llam_runtime_create(const llam_runtime_opts_t *opts,
                        size_t opts_size,
                        llam_runtime_t **out);

Create an explicit runtime with required asynchronous preemption.

Canonical embedding entry point. Allocates and initializes an independent runtime instance. The returned handle owns scheduler state, caches, blocking pool, and backend resources. Public registries validate owner-stamped objects without exposing raw storage addresses. Multiple explicit runtimes may run concurrently on different host threads. Cross-runtime object use fails with EXDEV. Compiler-qualified task images and a supported native profile are required. Unqualified task entries fail admission with ENOTSUP; unsupported native profiles fail creation or startup with ENOTSUP. No cooperative fallback is selected. Use llam_runtime_create_compat for native C compatibility execution.

opts
Optional runtime options; pass NULL for defaults.
opts_size
Size of the caller's llam_runtime_opts_t definition.
out
Destination runtime handle. Must not be NULL.

Returns 0 on success, -1 with errno set.

llam_runtime_create_compatExplicit native-C compatibility runtime.
int llam_runtime_create_compat(const llam_runtime_opts_t *opts,
                               size_t opts_size,
                               llam_runtime_t **out);

Create an explicit compatibility runtime without asynchronous admission.

Ownership and handle lifetime rules match llam_runtime_create. It accepts every preemption mode, needs no native asynchronous profile, and does not refuse a call from a managed task.

opts
Optional runtime options; pass NULL for defaults.
opts_size
Size of opts when non-NULL; must then be nonzero.
outout
Receives the runtime handle; set to NULL on failure. Must not be NULL.

Returns 0 on success, -1 with errno set.

llam_runtime_run_handleCanonical embedding scheduler driver for one runtime handle.
int llam_runtime_run_handle(llam_runtime_t *runtime);

Run a runtime handle.

Canonical embedding scheduler driver. Drives the scheduler associated with runtime until all work drains, cooperative stop is requested, or a backend/runtime error is recorded. Passing NULL fails with EINVAL.

runtime
Runtime to drive; not NULL.

Returns 0 after the runtime drains or stops, -1 with errno set on failure.

llam_runtime_request_stop_handleStop one explicit runtime; peer managed tasks get EXDEV.
int llam_runtime_request_stop_handle(llam_runtime_t *runtime);

Request stop for this handle; a task cannot stop a peer runtime.

runtime
Runtime to stop; not NULL.

Returns 0 on success, -1 with errno set to EINVAL for an invalid handle or EXDEV from a task of another runtime.

llam_runtime_get_resources_exCaller-sized snapshot of pool sizes, live/retained work and closing state.
int llam_runtime_get_resources_ex(llam_runtime_t *runtime,
                                  llam_runtime_resources_t *out,
                                  size_t out_size);

Copy an advisory snapshot of pool sizes, live work, and closing state.

runtime
Live runtime; not NULL.
outout
Receives the snapshot. Must not be NULL.
out_size
Size of the caller's llam_runtime_resources_t; must be nonzero. LLAM writes only the compatible prefix.

Returns 0 on success, -1 with errno set.

llam_runtime_drain_untilHost bounded stop/drain; timeout preserves runtime and resources.
int llam_runtime_drain_until(llam_runtime_t *runtime,
                             uint64_t deadline_ns,
                             llam_runtime_resources_t *out,
                             size_t out_size);

Close admission, request stop and wait until the active driver returns. Host only. The absolute monotonic deadline bounds waiting, not native callback execution or OS scheduling. ETIMEDOUT preserves runtime, handles and buffers; retry after callbacks finish, then join/destroy normally. Success never consumes resources. With pending tasks and no driver, returns EBUSY/NEEDS_RUNNER before closing admission: start the driver before retrying. Output is optional.

runtime
Runtime to drain; not NULL.
deadline_ns
Absolute llam_now_ns deadline for this wait.
outout
Optional final snapshot, written on success and on failure after the runtime handle was accepted; pass NULL to skip it.
out_size
Size of out when non-NULL; must then be nonzero.

Returns 0 once quiescent, -1 with errno set.

llam_runDrive the process-default runtime.
int llam_run(void);

Run the scheduler until all runtime work completes or an error occurs.

A runtime stop requested by llam_runtime_request_stop is a clean scheduler outcome and returns 0. Backend or fatal runtime failures return -1 and set errno. This function drives the scheduler on the calling OS thread; joins from unmanaged OS threads do not drive scheduler progress by themselves.

Returns 0 on clean completion or cooperative stop, -1 on failure with errno set.

llam_runtime_request_stopRequest cooperative stop and wake workers.
int llam_runtime_request_stop(void);

Request cooperative runtime stop and wake all workers.

This requests clean scheduler stop without forcibly terminating live tasks. Waits may observe ECANCELED; unmanaged callers target the default runtime, while managed tasks target their owner runtime.

Returns 0 on success, -1 with errno set when the runtime is not initialized.

llam_runtime_destroyCanonical embedding teardown. Passing NULL aliases default shutdown.
void llam_runtime_destroy(llam_runtime_t *runtime);

Destroy a runtime handle.

Canonical embedding teardown. Requests cooperative stop, tears down runtime-owned resources, and invalidates the handle. Heap-backed handle storage returned by llam_runtime_create is retired for the process lifetime rather than immediately reused, so stale raw pointers cannot alias a later runtime. Passing NULL is a legacy default-runtime shutdown alias. Managed tasks may only stop their owner runtime through this API; foreign runtime handles and unknown handles are ignored because this function has no errno channel.

llam_runtime_shutdownStop and release default-runtime resources.
void llam_runtime_shutdown(void);

Stop workers and release runtime-owned resources.

Shutdown is idempotent and is also valid after a failed or partial initialization. It requests cooperative stop, joins runtime-owned OS threads that were started, and releases backend resources. It is not a task-join API: callers that need graceful task completion should request stop and drive llam_run before shutdown. Public cleanup handles remain governed by their own contracts after shutdown: task handles are no longer joinable, owned I/O buffers remain valid until release, and channels may still be drained with llam_channel_try_recv_result before destroy. Calls from a managed LLAM task or scheduler frame do not tear the owning runtime down directly. They are treated as llam_runtime_request_stop; the host thread that owns the run call remains responsible for final shutdown.

Managed runtimes

For compiler-qualified C, use the managed runtime API. <llam/managed.h> defines caller-sized policy, resource and module structures; see their fields. The ordinary and advanced constructors default to ASYNC_REQUIRED, refusing unqualified task entries and unsupported native profiles. ASYNC_AUTO and COOPERATIVE are explicit advanced choices. The *_compat constructors and the expert Windows interface retain their compatibility contracts.

llam_preempt_opts_initFill managed options with their defaults.
int llam_preempt_opts_init(llam_preempt_opts_t *out, size_t out_size);

Fill managed options with their defaults: ASYNC_REQUIRED, 64 MiB, four slots.

A zeroed llam_preempt_opts_t differs: its policy is ASYNC_AUTO.

outout
Options to initialize. Must not be NULL.
out_size
Size of the caller's llam_preempt_opts_t; must be nonzero. LLAM writes only the prefix it knows.

Returns 0 on success, -1 with errno set.

llam_runtime_create_managedExplicit runtime with a chosen admission policy and capsule and native-affine limits.
int llam_runtime_create_managed(const llam_runtime_opts_t *runtime_opts,
                                size_t runtime_opts_size,
                                const llam_preempt_opts_t *preempt_opts,
                                size_t preempt_opts_size,
                                llam_runtime_t **out);

Create a runtime with an explicit managed policy and resource limits.

Advanced host-only policy constructor. The default policy is ASYNC_REQUIRED, as for the ordinary constructors. ASYNC_AUTO and COOPERATIVE are explicit alternatives. Creation may inspect and pin native loader images; calling from a LLAM task returns EBUSY.

runtime_opts
Optional runtime options; pass NULL for defaults.
runtime_opts_size
Size of runtime_opts when non-NULL; must then be nonzero.
preempt_opts
Optional managed options; pass NULL with size 0 for the defaults of llam_preempt_opts_init.
preempt_opts_size
Size of preempt_opts; must be nonzero when it is non-NULL and 0 when it is NULL.
outout
Receives the runtime handle; set to NULL on failure. Must not be NULL.

Returns 0 on success, -1 with errno set.

llam_runtime_preempt_info_exRequested and effective policy, the reason, and capsule storage accounting.
int llam_runtime_preempt_info_ex(llam_runtime_t *runtime,
                                 llam_preempt_info_t *out,
                                 size_t out_size);

Copy the runtime's requested and effective policy and storage accounting.

A compatibility runtime reports LLAM_PREEMPT_REASON_LEGACY_RUNTIME.

runtime
Live runtime; not NULL.
outout
Receives the snapshot. Must not be NULL.
out_size
Size of the caller's llam_preempt_info_t; must be nonzero. LLAM writes only the compatible prefix.

Returns 0 on success, -1 with errno set.

llam_runtime_module_register_exRegister one loaded image's managed metadata.
int llam_runtime_module_register_ex(llam_runtime_t *runtime,
                                    const llam_module_opts_t *opts,
                                    size_t opts_size,
                                    llam_module_t **out);

Register one loaded image's managed metadata with a stopped runtime.

Host only; a task gets EBUSY. Keep the caller's loader reference alive during this call. LLAM obtains its own reference and validates the image.

runtime
Runtime with stopped dispatch; not NULL.
opts
Module options, with nonzero metadata fields and zero flags.
opts_size
Size of opts; must include metadata_address and metadata_bytes, so at least 16 bytes.
outout
Receives the module handle; set to NULL on failure. Must not be NULL.

Returns 0 on success, -1 with errno set.

llam_runtime_module_unregisterUnregister a module; success consumes its handle.
int llam_runtime_module_unregister(llam_runtime_t *runtime,
                                   llam_module_t *module);

Unregister a module and release LLAM's loader reference.

Success consumes the handle. EBUSY preserves it; another runtime is EXDEV. Registration changes require host context, stopped dispatch, and no retained G, capsule, native operation or catalog reader.

runtime
Runtime that registered the module; not NULL.
module
Module handle; not NULL.

Returns 0 on success, -1 with errno set.

Explicit ownership

These host-side constructors stamp an object with the runtime you name. They retain the runtime through publication and preserve existing object lifetimes. Older constructors keep their current-task/default-runtime selection. The group forms that take options are under Task groups.

llam_runtime_channel_createCreate a channel owned by a runtime.
llam_channel_t *llam_runtime_channel_create(
    llam_runtime_t *runtime,
    size_t capacity);

Create objects for an explicit, initialized runtime from host code. These constructors pin the runtime until the new handle is published. A task of another runtime gets EXDEV; invalid/stale runtimes get EINVAL. Existing constructors retain their current-task/default-runtime behavior.

runtime
Initialized runtime that will own the object; not NULL.
capacity
Number of bounded buffer slots. Must be at least 1.

Returns New handle, or NULL with errno set.

llam_runtime_mutex_createCreate a mutex owned by a runtime.
llam_mutex_t *llam_runtime_mutex_create(llam_runtime_t *runtime);

Create objects for an explicit, initialized runtime from host code. These constructors pin the runtime until the new handle is published. A task of another runtime gets EXDEV; invalid/stale runtimes get EINVAL. Existing constructors retain their current-task/default-runtime behavior.

runtime
Initialized runtime that will own the object; not NULL.

Returns New handle, or NULL with errno set.

llam_runtime_cond_createCreate a condition variable owned by a runtime.
llam_cond_t *llam_runtime_cond_create(llam_runtime_t *runtime);

Create objects for an explicit, initialized runtime from host code. These constructors pin the runtime until the new handle is published. A task of another runtime gets EXDEV; invalid/stale runtimes get EINVAL. Existing constructors retain their current-task/default-runtime behavior.

runtime
Initialized runtime that will own the object; not NULL.

Returns New handle, or NULL with errno set.

llam_runtime_cancel_token_createCreate a cancellation token owned by a runtime.
llam_cancel_token_t *llam_runtime_cancel_token_create(
    llam_runtime_t *runtime);

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

llam_runtime_task_group_createCreate a task group owned by a runtime.
llam_task_group_t *llam_runtime_task_group_create(llam_runtime_t *runtime);

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

llam_runtime_timer_create_exCreate a timer owned by a runtime.
int llam_runtime_timer_create_ex(llam_runtime_t *runtime,
                                 const llam_timer_opts_t *opts,
                                 size_t opts_size,
                                 llam_timer_t **out);

Create a timer owned by an explicit runtime.

runtime
Initialized runtime that will own the timer; not NULL.
opts
Timer options; see llam_timer_create_ex.
opts_size
Size of opts.
outout
Receives the timer; set to NULL on failure. Must not be NULL.

Returns 0 on success, -1 with errno set.

llam_runtime_signal_set_create_exCreate a signal wait set owned by a runtime. Linux-only; other platforms return ENOTSUP.
int llam_runtime_signal_set_create_ex(llam_runtime_t *runtime,
                                      const int *signos,
                                      size_t signo_count,
                                      const llam_signal_opts_t *opts,
                                      size_t opts_size,
                                      llam_signal_set_t **out);

Create a signal wait set owned by an explicit runtime.

runtime
Initialized runtime that will own the set; not NULL.
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 opts when 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. Platforms other than Linux fail with ENOTSUP.

See Embed LLAM for cancellation composition, endpoint restrictions, drain timeout recovery and complete failure cleanup.

Reserved compiler ABI

These symbols are exported for code the managed compiler generates. They are not application API and are not task admission.

llam_managed_native_enterNative entry and exit guards the compiler emits in balanced pairs. Do not call them directly.
void llam_managed_native_enter(void);

Reserved for compiler-generated code. Application code must not call these.

Compiler ABI, not task admission. The qualified compiler emits balanced native entry/exit calls. They preserve errno/LastError and never allocate, resolve dynamic TLS or acquire a lock. A native callback has no managed G/P context. Unbalanced guards fail closed; they are not a public disable switch. Runtime managed creation and LLPM2 admission are separate contracts.

llam_managed_native_leaveNative entry and exit guards the compiler emits in balanced pairs. Do not call them directly.
void llam_managed_native_leave(void);

Reserved for compiler-generated code. Application code must not call these.

Compiler ABI, not task admission. The qualified compiler emits balanced native entry/exit calls. They preserve errno/LastError and never allocate, resolve dynamic TLS or acquire a lock. A native callback has no managed G/P context. Unbalanced guards fail closed; they are not a public disable switch. Runtime managed creation and LLPM2 admission are separate contracts.

↑↓ Navigate↵ Openesc CloseSearching the 3.0.1 docs