Execution controls
!!! note "Available in LLAM 3.0.1" These interfaces require headers and runtime with ABI 2.6 or newer. The 3.0.1 SDK provides ABI 2.7.
On this page
Include <llam/executor.h> for execution controls. They preserve the runtime's
requested asynchronous policy and apply to the current logical task and native
execution binding.
Short no-preemption regions
llam_preempt_region_enterEnter a nested, bounded region owned by the current native M/G execution.
int llam_preempt_region_enter(void);Enter a short, nested no-preemption region on the current managed task. Requires a live native M/G binding. The caller must keep the region bounded, nonblocking and balanced, and must not suspend, allocate, spawn, unwind or call unknown foreign code. Stateful LLAM calls are refused with ENOTSUP inside it. This does not stop other OS threads or provide memory synchronization. Host/guarded native callback entry returns ENOTSUP; exhausted nesting returns EOVERFLOW. Pending cancellation/preemption is preserved. A compiler must validate bounded control flow and transitive effects; this API does not certify an annotation.
llam_preempt_region_leaveLeave one matching region; final leave restores ordinary preemption.
int llam_preempt_region_leave(void);Leave one region owned by this exact native M/G execution. Underflow returns EINVAL; guarded native callbacks cannot leave a caller's region. Final leave restores ordinary execution and services a pending preemption request. A task returning with an open region is a process-fatal contract violation (exit status 125).
A successful enter prevents LLAM native capture and cooperative task handoff
until the matching leave. Nested calls preserve the outer guard. Host callers,
guarded foreign callbacks and announced blocking frames are rejected with ENOTSUP.
A foreign native-entry guard cannot enter or leave a caller's region. Underflow
in a valid managed task returns EINVAL; nesting exhaustion returns EOVERFLOW.
Failed transitions preserve the prior state. Returning from the task with an open
region is a process-fatal contract violation, reported with exit status 125.
Keep protected work short, bounded, nonblocking and balanced. Do not allocate,
spawn, park, unwind, deliver cancellation or call unknown foreign code inside it.
Stateful LLAM calls are refused with ENOTSUP; native allocation or foreign effects
must also be excluded by the caller/compiler. A language compiler must check all
control-flow exits and transitive effects, including indirect calls. Calling this
API does not certify a @nopreempt annotation or an arbitrary language frontend.
Bracket the complete call of a no-preemption function, including its epilogue. Restore protected invariants before leave: final leave may service pending preemption immediately. Cancellation requests remain pending and can be observed at the next permitted checkpoint. An expired sleep can still succeed immediately under its existing deadline semantics.
This is not a lock or an allocation-lifetime proof. The OS can deschedule the native thread, and other OS threads can continue accessing shared state. Continue to enforce the appropriate synchronization, borrow and lifetime rules.
The native regression uses one processor and a no-poll rendezvous to establish asynchronous progress before entry. A separate host probe checks that preemption counters stay stable while the region is held, and a peer detects any execution inside that interval. It also requests cancellation and verifies delivery after balanced leave. Core tests cover nesting, foreign entry refusal, overflow, suspension refusal and fatal unbalanced task exit.
Shared local executors
llam_local_executor_opts_initInitialize the reserved 8-byte options record; both fields must be zero.
int llam_local_executor_opts_init(llam_local_executor_opts_t *out,
size_t out_size);llam_runtime_local_executor_create_exReserve one native-affine thread slot on an explicit managed runtime; publish an opaque executor handle on success.
int llam_runtime_local_executor_create_ex(
llam_runtime_t *runtime,
const llam_local_executor_opts_t *opts,
size_t opts_size,
llam_local_executor_t **out);Reserve one LLAM-owned native thread slot in a managed runtime. Async policies require qualified task entries; explicit COOPERATIVE policy permits plain C. Use spawn_opts.local_executor for the root and every local child. Cross-thread submissions must transfer only data valid on the destination thread.
llam_local_executor_closeSeal admission without waiting or cancelling existing members. Idempotent.
int llam_local_executor_close(llam_local_executor_t *executor);Seal admission; existing members continue running. Idempotent, nonblocking.
llam_local_executor_drainSeal and wait for all member execution and actual native-thread/TLS retirement, deferring caller cancellation and deadlines.
int llam_local_executor_drain(llam_local_executor_t *executor);Seal and wait for all member execution and native-thread/TLS retirement. Caller cancellation/deadlines are deferred, not cleared. A member cannot drain its own executor (EDEADLK). Success alone authorizes releasing borrowed domain storage; fatal failure does not. Never call from a native TLS destructor. Native TLS is shared by the domain: do not retain pointers to a member's stack after that member returns. Local tasks still interleave; use short regions to protect local multi-instruction invariants. Runtime stop seals the executor.
llam_local_executor_destroyConsume a closed, quiescent executor with no concurrent public operation.
int llam_local_executor_destroy(llam_local_executor_t *executor);Consume a drained executor with no concurrent public operation.
LLAM_LOCAL_EXECUTOR_OPTS_CURRENT_SIZECurrent size for the options initializer and executor creation.
#define LLAM_LOCAL_EXECUTOR_OPTS_CURRENT_SIZECreation accepts NULL, 0 options and leaves the output untouched on failure.
flags and reserved0 are reserved zero fields. The runtime must have managed
execution pools; a compatibility-only runtime returns ENOTSUP. One executor
uses one native_affine_capacity slot, shared by all its members. Exhausted
capacity returns EAGAIN; task admission limits still apply separately.
Set llam_spawn_opts_t.local_executor on the root and every local child and submit
through llam_runtime_spawn_ex, llam_spawn_ex or llam_task_group_spawn_ex, with
the complete current option size. Unsized legacy spawn wrappers read only the old
prefix and ignore this new field. An
executor is not implicitly inherited: leaving that field NULL requests
ordinary scheduling. All selected members run on the same LLAM-owned OS thread
through yield, parking, asynchronous capture/restore and task-local exit cleanup.
The creating host thread is not that thread. Submit only data valid on the
executor thread; create thread-affine values inside its root before lending them
to local children. ASYNC_REQUIRED and ASYNC_AUTO still require qualified local
entries. Plain C local entries need an explicitly COOPERATIVE managed policy.
Closing rejects later submissions with EPIPE; accepted spawn reservations are
included in drain. A native slot already entering retirement also rejects later
admission. When the run driver finishes, its native threads retire and open local
domains close; create a new executor for a later run. Draining from any member
returns EDEADLK. Concurrent drains
or premature destroy return EBUSY; stale/wrong-family handles return EINVAL
and managed cross-runtime use returns EXDEV. A host drain needs a running driver
for outstanding members and never runs the scheduler itself. Runtime stop seals
the domain and requests ordinary member cancellation. Drain keeps waiting through
that request; fatal failure does not authorize release of borrowed storage.
An executor drain does not consume member task handles. Join or detach ordinary handles separately; a group drain consumes its borrowed child handles. Public member handles may outlive a destroyed executor after execution has retired. A normal group drain establishes member retirement; executor drain additionally establishes native TLS/FLS teardown. Never drain the executor from its own TLS callback. Domain TLS must not retain a pointer to a member's stack after that member returns. Storage intentionally borrowed until domain exit must instead remain valid through successful executor drain.
Local tasks can interleave between instructions even on one OS thread. Protect multi-instruction local invariants, such as a non-atomic reference-count update, with the short regions above. This also requires language-level effect and borrow checks. Owned-scope/future value destructors keep their thread-neutral contract; place thread-affine cleanup in the local task wrapper or task-local exit cleanup.
The installed SDK fixture uses a root and three local children, parent cancellation, borrowed group drain, no-poll asynchronous progress and same-thread cleanup. The native runtime suite also delays a native TLS destructor to verify that executor drain waits for it.