Handle for one spawned asynchronous context. A Context supports validity checks, completion checks, cancellation requests, waiting, and output retrieval. Contexts are cooperatively scheduled: a spawned body runs only while the current context yields, waits, sleeps or blocks, so done? right after spawn is FALSE.
Handle and completion model
Contextexposes one handle per spawned context.- Construction with the output schema on the stack (
() Context,Int32 Context) andINITcreate an invalid handle. spawnreturns a valid handle.done?becomesTRUEonly after the referenced context stores its completion state.waitandgetobserve the same completion point.cancel,wait, andgetneed a mutable handle view, as in@context.cancel;valid?anddone?accept an immutable view. A handle can be moved with@context new, not copied.- When the declared output schema is not
(), the handle retains the stored completion value until destruction. - Destroying a valid context waits for completion, releases the spawned context state, and destroys any retained non-Meta output storage.
makeContext (in Out -- context)
Constructs and schedules one Context from a callable and its declared output schema.
Methods
INIT
(--) Creates an invalid handle.valid?
(-- valid) Reports whether the handle currently refers to one context.done?
(-- done) Reports whether the referenced context has finished and stored its completion state.Preconditions
- The handle is valid.
cancel
(--) Marks an uncanceled target as canceled and invokes its current cancellation callback synchronously; it does not wait for completion.Preconditions
- The handle is valid and is reached through a mutable view, as in
@context.cancel.
On Linux, canceling a target suspended in yield, including a sleep whose duration truncates to zero whole nanoseconds, terminates with invalid cancelation function, exit 2. Cancellation propagated through wait or get has the same restriction. Use a timed wait with a nonzero converted duration when cancellation is required.
wait
(--) Waits until the referenced context finishes.Preconditions
get
(-- output) Waits until the referenced context finishes and returns its stored output according to the declared output schema.Preconditions
Output mapping
- The declared output schema is selected by the
outargument ofspawn. - The callable output is required to match that declared output schema.
- When the declared output schema is
(),getproduces no object. - When the declared output schema is Meta,
getreturns that stored Meta object directly. - For a non-Meta In-place output schema,
getreturns a mutable borrowedRefto the context-owned output, valid until the handle is destroyed (Int32 Reffor anInt32output). Text and Code outputs retain their schema kinds. - A direct Text output comes back empty (known issue); return an owned
Stringwhen the contents must be retained. getleaves the handle valid.
Known issue: A Context that stores a borrowed Ref retains its target address without copying and destroys and reinitializes that target on destruction, even with an In-place output descriptor; return an owned value, such as target new through an immutable view, instead.
Waiting and cancellation semantics
- If the target is already done,
waitandgetreturn without propagating cancellation. Otherwise, an already-canceled current context requests cancellation of the target before waiting. - While the current context is suspended in
waitorget, canceling it requests cancellation of the target; waiting still continues until the target completes. - After the target context becomes done, repeated
waitandgetcalls return without additional suspension.
Examples
Initial state
"control" use
"sync/Context" use
{} Int32 {} [
context: () Context;
context.valid? printStack _:;
0
] "main" exportFunction
Expected Output During Compilation
FALSE
done? and get
"String" use
"control" use
"sync/Context" use
"sync/sync" use
{} Int32 {} [
context: [2] Int32 spawn;
context.done? [
("done" LF) printList
] [
("waiting" LF) printList
] if
yield
context.done? [
("done" LF) printList
] [
("waiting" LF) printList
] if
value: @context.get;
value isRef [
("ref" LF) printList
] [
("not-ref" LF) printList
] if
drop
("value=" @value new LF) printList
0
] "main" exportFunction
Expected Output
waiting
done
ref
value=2
Cancellation request and get
"String" use
"control" use
"sync/Context" use
"sync/sync" use
{} Int32 {} [
context: [10.0r64 sleepFor canceled?] FALSE spawn;
@context.cancel
result: @context.get;
(result LF) printList
0
] "main" exportFunction
Expected Output
TRUE
See also
- sync/sync: Cross-platform scheduling, sleep, time, IPv4 formatting, and TCP helpers.
- sync/ContextGroup: Group of spawned contexts with shared waiting and cancellation.
- sync/Event: Persistent event state with clear, set, wait, wake, and wakeOne.
- windows/dispatcher: Windows completion-port dispatcher and callback posting helpers.