summaryrefslogtreecommitdiff
path: root/sources/examples/pilib/twoPlaceBuffer.scala
blob: 670c879ce63e3550ccfff1d5abe25c5686034bbd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import scala.concurrent.pilib._;

/** Two-place buffer specification and implementation. */
object twoPlaceBuffer with Application {

  /**
  * Specification.
  */
  def Spec[a](in: Chan[a], out: Chan[a]): unit =  {

    def B0: unit = choice (
      in * (x => B1(x))
    );

    def B1(x: a): unit = choice (
      out(x) * (B0),
      in * (y => B2(x, y))
    );

    def B2(x: a, y: a): unit = choice (
      out(x) * (B1(y))
    );

    B0
  }

  /**
  * Implementation using two one-place buffers.
  */
  def Impl[a](in: Chan[a], out: Chan[a]): unit =  {
    ///- ... complete here ...
    // one-place buffer
    def OnePlaceBuffer[a](in: Chan[a], out: Chan[a]): unit =  {
      def B0: unit = choice ( in * (x => B1(x)) );
      def B1(x: a): unit = choice ( out(x) * (B0));
      B0
    }
    val hidden = new Chan[a];
    spawn < OnePlaceBuffer(in, hidden) | OnePlaceBuffer(hidden, out) >
    ///+
  }

  val random = new java.util.Random();

  def Producer(n: Int, in: Chan[String]): unit = {
    Thread.sleep(random.nextInt(1000));
    val msg = "" + n;
    choice (in(msg) * {});
    Producer(n + 1, in)
  }

  def Consumer(out: Chan[String]): unit = {
    Thread.sleep(random.nextInt(1000));
    choice (out * { msg => () });
    Consumer(out)
  }

  val in = new Chan[String];
  in.attach(s => System.out.println("put " + s));
  val out = new Chan[String];
  out.attach(s => System.out.println("get " + s));
  //spawn < Producer(0, in) | Consumer(out) | Spec(in, out) >
  spawn < Producer(0, in) | Consumer(out) | Impl(in, out) >

}