# Exercise: Ping-Pong
#
# Two strands communicating back and forth.
#
# This is a classic concurrent programming exercise:
#   Strand A sends "ping"
#   Strand B receives, sends "pong"
#   Strand A receives, sends "ping"
#   ...and so on
#
# Your task: Set up a simple ping-pong (just one exchange).
# When done, delete the marker line below.

# I AM NOT DONE

: test-ping-pong ( -- )
    chan.make   # ping channel (main -> worker)
    chan.make   # pong channel (worker -> main)
    # Stack: ( ping pong )

    # Spawn "pong" responder - captures both channels
    2dup [
        swap chan.receive drop drop  # Get ping, drop Bool and value
        1 swap chan.send drop        # Send pong (1) to pong channel
    ] strand.spawn drop
    drop drop   # clean up extra channels: ( ping pong )

    # Send ping (as 0)
    over 0 swap chan.send drop   # ( ping pong )

    # Receive pong
    # Your code here - receive from pong channel, clean up channels
    drop drop
    0
    # Do not edit below this line
    1 test.assert-eq
;
