Asyncronous Message Passing

Article from Comp Language Forth (Usenet)

Newsgroups: comp.lang.forth Subject: Re: asynchronous message passing From: foxaudioresearch@gmail.com

On Saturday, June 1, 2019 at 10:04:11 AM UTC-4, Dmitry Ponyatov wrote:

> How should I implement asynchronous message passing in FORTH, if I want to implement distributed computing with the actor model? > > I really like the Smalltalk model with messaging at the level of an elementary language operation, and I would like to implement something similar in Forth. > > I feel that it should be done at the level of multiple Forth machines, each of them has its own stack and message dispatch works over the vocabulary. In this model, e

This is very old and simple but it works. Obviously more complex protocols could be added on top of this layer. I have used it in cooperative multi-tasking systems.

As I look at it now it could be simplified with BEGIN WHILE REPEAT .

In this code the word LOCAL computes the task specific address for a user variable.

PAUSE is the task context switch word.

\ Based on article in Forth Dimensions Vol 7 #4 by R. W. Dobbins. Columbia ML.
\ It allows a transputer like com link between FORTH tasks

\ "SENDer" waits until the mailbox is cleared by "RECEIVER" before
\ sending the next message.
\ Message is 1 CELL.  But complex sender/receiver pairs can be created with
\ this simple pipe.

: SEND-MSG      ( msg rcvr-PID --)
                 TASK-MSG LOCAL   \ ( msg addr ) compute rcvr MSGbox address
                 BEGIN
                    PAUSE
                    DUP @          \ ( msg addr n )  n = contents of MSGbox
                 0= UNTIL         \ ( msg addr ) loop until MSGbox is empty
                 ! ;                     \ store the msg in addr

: READ-MSG     ( PID -- msg )     \ read MSG from any task
                 TASK-MSG LOCAL   \ ( -- addr )
                 BEGIN
                   PAUSE
                   DUP @            \ dup the addr and fetch content
                   ?DUP UNTIL    \ loop until content is not zero
                   0 ROT ! ;          \ put zero in the MSG box, msg on stack


: GET-MSG      ( -- n )  myself read-MSG ;