summaryrefslogtreecommitdiff
path: root/sources/examples/pilib/elasticBuffer.scala
diff options
context:
space:
mode:
authorcremet <cremet@epfl.ch>2003-08-26 15:28:51 +0000
committercremet <cremet@epfl.ch>2003-08-26 15:28:51 +0000
commit80d3a625a75ba054eef58eac94dc3a8066690c36 (patch)
treeccdc5d3d86415b7630666961d2741589cf59dede /sources/examples/pilib/elasticBuffer.scala
parent4c10e8515b2d1b7991f660b444f4b0a5d014286d (diff)
downloadscala-80d3a625a75ba054eef58eac94dc3a8066690c36.tar.gz
scala-80d3a625a75ba054eef58eac94dc3a8066690c36.tar.bz2
scala-80d3a625a75ba054eef58eac94dc3a8066690c36.zip
- Added "PiLib" library and associated examples.
- Fixed some files in the package "scala.concurrent".
Diffstat (limited to 'sources/examples/pilib/elasticBuffer.scala')
-rw-r--r--sources/examples/pilib/elasticBuffer.scala75
1 files changed, 75 insertions, 0 deletions
diff --git a/sources/examples/pilib/elasticBuffer.scala b/sources/examples/pilib/elasticBuffer.scala
new file mode 100644
index 0000000000..50e61c291a
--- /dev/null
+++ b/sources/examples/pilib/elasticBuffer.scala
@@ -0,0 +1,75 @@
+object elasticBuffer {
+
+ import scala.concurrent.pilib._;
+
+ /**
+ * Recursive type for channels that carry a "String" channel and
+ * an object of the type we define.
+ */
+ class MetaChan extends Chan[Pair[Chan[String], MetaChan]];
+
+ def Buffer(put: Chan[String], get: Chan[String]): unit = {
+
+ /**
+ * An empty buffer cell, ready to pass on (o,r) to the left.
+ */
+ def Bl(i:Chan[String], l: MetaChan,
+ o: Chan[String], r: MetaChan): unit =
+ choice (
+ l(Pair(o,r)) * (System.out.println("Removed one cell.")),
+ i * (inp => Cl(i, l, o, r, inp))
+ );
+
+ /**
+ * A buffer cell containing a value, ready to receive (o,r) from the right.
+ */
+ def Cl(i: Chan[String], l: MetaChan,
+ o: Chan[String], r: MetaChan, content: String): unit =
+ choice (
+ o(content) * (Bl(i,l,o,r)),
+ i * (inp => Dl(i,l,o,r,content, inp)),
+ r * ( { case Pair(newo, newr) => Cl(i,l,newo,newr,content) })
+ );
+
+ /**
+ * Two joined buffer cells, of type Cl.
+ */
+ def Dl(i: Chan[String], l: MetaChan,
+ o: Chan[String], r: MetaChan,
+ content: String, inp: String): unit = {
+ val newlr = new MetaChan;
+ val newio = new Chan[String];
+ spawn < Cl(i, l, newio, newlr, inp) | Cl(newio, newlr, o, r,content) >;
+ }
+
+ // l and r channels for the leftmost and rightmost cell, respectively.
+ val unused1 = new MetaChan;
+ val unused2 = new MetaChan;
+
+ Bl(put, unused1, get, unused2);
+ }
+
+ val random = new java.util.Random();
+
+ def Producer(n: int, put: Chan[String]): unit = {
+ Thread.sleep(1 + random.nextInt(1000));
+ val msg = "object " + n;
+ put.write(msg);
+ System.out.println("Producer gave " + msg);
+ Producer(n + 1, put)
+ }
+
+ def Consumer(get: Chan[String]): unit = {
+ Thread.sleep(1 + random.nextInt(1000));
+ val msg = get.read;
+ System.out.println("Consummer took " + msg);
+ Consumer(get)
+ }
+
+ def main(args: Array[String]): unit = {
+ val put = new Chan[String];
+ val get = new Chan[String];
+ spawn < Producer(0, put) | Consumer(get) | Buffer(put, get) >
+ }
+
+}