-- The Great Computer Language Shootout
-- http://shootout.alioth.debian.org/
--
-- contributed by Christian L. Jacobsen

#INCLUDE "course.module"

VAL INT t IS 3000: -- FIXME: From cmd line args

PROC thread(CHAN INT in?, out!)
  INT num:
  SEQ
    in ? num
    out ! (num + 1)
:

PROC startthread(CHAN INT out!)
  out ! 0
:

PROC endthread(CHAN INT in?, CHAN BYTE out!)
  INT num:
  SEQ
    in ? num
    out.int(num, 0, out!)
:
  
PROC threads(CHAN BYTE kyb?, scr!, err!)
  [t + 1]CHAN INT channels:
  PAR
    startthread(channels[0]!)
    PAR i = 0 FOR t
      thread(channels[i]?, channels[i + 1]!)
    endthread(channels[t]?, scr!)
:

