6.27.1.5 Message queues

Gforth’s message queues are a variant of the actor model.

An event is an xt; the sending task tells the receiving task to run an event, and the receiving task performs one event from its message queue when it is ready. Execute-once closures can be used for events that pass parameters.

send-event ( xt task –  ) gforth-experimental “send-event”

Task IPC: send xt to task. The xt is executed there. Use a one-shot closure to pass parameters with the xt.

When several tasks send event to one task, the events may arrive in any order. In order to receive events, you put one of the following words in the receiving task:

?events ( ) gforth-experimental “question-events”

Perform all event sequences in the current task’s message queue, one event sequence at a time.

event-loop ( ) gforth-experimental “event-loop”

Wait for event sequences, and execute any event sequences when they arrive. Return to waiting if no event sequences are in the queue. This word never returns.

Alternatively, when a task is stopped, it is also ready for receiving event, and receiving an event will wake it up.