Scheduler-context wait state with explicit clearing, setting, waiting, and wake operations. The Event wrapper remembers one boolean state between wake operations.
Event model
- A newly constructed
Event starts with stored state FALSE.
INIT and clear set the stored state to FALSE.
set stores TRUE and wakes every currently waiting context.
- While the stored state is
TRUE, later wait calls return immediately until clear.
wake and wakeOne release only current waiters and do not change the stored state.
wait returns no status or result value; after resumption, get exposes the stored state separately.
- An
Event owns its stored state and waiter queue. Destroying an Event with waiting contexts is invalid.
Event
Methods
INIT |
(--) |
Sets the stored state to FALSE. |
get |
(-- state) |
Returns the current stored state. |
clear |
(--) |
Clears the stored state. |
set |
(--) |
Stores the state TRUE and wakes every currently waiting context. |
wait |
(--) |
Returns immediately when the stored state is TRUE. Otherwise waits until set, wake, or wakeOne releases the current context. |
wake |
(--) |
Wakes every currently waiting context without changing the stored state. |
wakeOne |
(--) |
Wakes the oldest currently waiting context without changing the stored state. |
Waiting and cancellation semantics
- If the current context is already canceled when
wait begins, wait returns immediately while the stored state remains unchanged.
- If cancellation happens while the context is already waiting, the context is resumed while the stored state remains unchanged.
wake and wakeOne affect only contexts that are already waiting at that moment.
wakeOne releases the oldest currently waiting context.
- No wake is remembered for later waiters.
Examples
set remembers the state
"sync/Event" use
"sync/sync" use
"String" use
"control" use
{} Int32 {} [
event: Event;
context: {event: @event; CALL: [
@event.wait
("released" LF) printList
];} () spawn;
@event.set
yield
context.done? [
("done" LF) printList
] [
("waiting" LF) printList
] if
@context.wait
event.get [
("TRUE" LF) printList
] [
("FALSE" LF) printList
] if
0
] "main" exportFunction
Expected Output
released
done
TRUE
wakeOne without setting the state
"sync/Event" use
"sync/sync" use
"String" use
"control" use
{} Int32 {} [
event: Event;
context: {event: @event; CALL: [
@event.wait
];} () spawn;
yield
@event.wakeOne
@context.wait
event.get [
("TRUE" LF) printList
] [
("FALSE" LF) printList
] if
0
] "main" exportFunction
Expected Output
FALSE
clear restores blocking
"sync/Event" use
"sync/sync" use
"String" use
"control" use
{} Int32 {} [
event: Event;
@event.set
@event.clear
context: {event: @event; CALL: [
@event.wait
("released" LF) printList
];} () spawn;
yield
context.done? [
("done" LF) printList
] [
("waiting" LF) printList
] if
@event.wakeOne
@context.wait
0
] "main" exportFunction
Expected Output
waiting
released
See also
- sync/Signal: Wait queue with wake and wakeOne operations.
- sync/Context: Spawned context handle with waiting, output retrieval, and cancellation.
- sync/sync: Cross-platform scheduling, sleep, time, IPv4 formatting, and TCP helpers.