sync/Context

Handle for one spawned asynchronous context. A Context supports validity checks, completion checks, cancellation requests, waiting, and output retrieval.


Handle and completion model


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 (--) Requests cancellation of the referenced context and returns immediately.

Preconditions

  • The handle is valid.
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


Waiting and cancellation semantics


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