Handle for one spawned asynchronous context. A Context supports validity checks, completion checks, cancellation requests, waiting, and output retrieval.
Handle and completion model
Context exposes one handle per spawned context.
- Default construction and
INIT create an invalid handle.
spawn returns a valid handle.
done? becomes TRUE only after the referenced context stores its completion state.
cancel only requests cancellation and returns immediately.
wait and get observe the same completion point.
- 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.
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
cancel |
(--) |
Requests cancellation of the referenced context and returns immediately. |
Preconditions
wait |
(--) |
Waits until the referenced context finishes. |
Preconditions
- The handle is valid.
- The current context is not waiting on itself.
- No other unfinished
wait or get is already using the same unfinished target context.
get |
(-- output) |
Waits until the referenced context finishes and returns its stored output according to the declared output schema. |
Preconditions
- The handle is valid.
- The current context is not waiting on itself.
- No other unfinished
wait or get is already using the same unfinished target context.
Output mapping
- The declared output schema is selected by the
out argument of spawn.
- The callable output is required to match that declared output schema.
- When the declared output schema is
(), get produces no value.
- When the declared output schema is
Meta, get returns that stored Meta object directly.
- Otherwise
get returns one outputSchema Ref to the stored output.
get leaves the handle valid.
Waiting and cancellation semantics
- If the current context is already canceled when
wait or get begins, cancellation is first requested for the target context and waiting then continues until completion.
- If the current context is not canceled when
wait or get begins, later cancellation of the current context requests cancellation of the target context.
cancel only requests cancellation.
- After the target context becomes done, repeated
wait and get calls return without additional suspension.
Examples
Initial state
"sync/Context" use
"control" use
{} Int32 {} [
context: () Context;
context.valid? printStack _:;
0
] "main" exportFunction
Expected Output
FALSE
done? and get
"sync/Context" use
"sync/sync" use
"String" use
"control" 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
"sync/Context" use
"sync/sync" use
"control" use
{} Int32 {} [
context: [10.0r64 sleepFor canceled?] FALSE spawn;
@context.cancel
result: @context.get;
@result printStack _:;
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.