Source

atomo / examples / ping-pong.atomo

The default branch has multiple heads

Full commit
-- starts a match of n volleys
volley: (n: Integer) :=
  { -- start up the ponger
    proc =
      { notify |
        pong: notify
      } spawn: [self]

    -- start up the pinger
    { ping: n ponger: proc } spawn
  } call

pong: notify :=
  receive match: {
    -- volley is done; finish
    @finished ->
      { "         | ." print
        "volley finished" print
        notify <- @done
      } call

    -- got a ping, send a pong
    @(ping: pid) ->
      { "       . |" print
        pid <- @pong
        pong: notify -- loop
      } call
  }

ping: 0 ponger: pid := pid <- @finished
ping: n ponger: pid :=
  { pid <- @(ping: self)

    -- got a pong send a ping
    receive match: {
      @pong ->
        { "|  .      " print
          ping: (n - 1) ponger: pid
        } call
    }
  } call

-- start up the match, doing 5 volleys
volley: 5

-- wait for the match to complete
receive print