summaryrefslogtreecommitdiff
path: root/docs/examples/pilib/twoPlaceBuffer.scala
diff options
context:
space:
mode:
authorGilles Dubochet <gilles.dubochet@epfl.ch>2005-12-16 18:44:33 +0000
committerGilles Dubochet <gilles.dubochet@epfl.ch>2005-12-16 18:44:33 +0000
commit53a3cc7b17f4cf97075b7e71720777fd84109696 (patch)
tree0cc784e0b47ea49cc151a136d19f20bfa8ee2197 /docs/examples/pilib/twoPlaceBuffer.scala
parentdf50e05006b43b007c2587549030d24b5c154398 (diff)
downloadscala-53a3cc7b17f4cf97075b7e71720777fd84109696.tar.gz
scala-53a3cc7b17f4cf97075b7e71720777fd84109696.tar.bz2
scala-53a3cc7b17f4cf97075b7e71720777fd84109696.zip
Created proper 'docs' folder for new layout.
Diffstat (limited to 'docs/examples/pilib/twoPlaceBuffer.scala')
-rw-r--r--docs/examples/pilib/twoPlaceBuffer.scala67
1 files changed, 67 insertions, 0 deletions
diff --git a/docs/examples/pilib/twoPlaceBuffer.scala b/docs/examples/pilib/twoPlaceBuffer.scala
new file mode 100644
index 0000000000..686547b344
--- /dev/null
+++ b/docs/examples/pilib/twoPlaceBuffer.scala
@@ -0,0 +1,67 @@
+package examples.pilib;
+
+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) >
+
+}