summaryrefslogtreecommitdiff
path: root/sources/examples/pilib/scheduler.scala
diff options
context:
space:
mode:
authorcremet <cremet@epfl.ch>2003-11-27 17:43:37 +0000
committercremet <cremet@epfl.ch>2003-11-27 17:43:37 +0000
commit0212d5e04ad20ddeee78020d0b57f0b8fc8a2746 (patch)
tree9203a9a53c01472ef7b0ddc7f25acaa6468bf827 /sources/examples/pilib/scheduler.scala
parente40307b850f3d600056700fd6757e795e7a591b1 (diff)
downloadscala-0212d5e04ad20ddeee78020d0b57f0b8fc8a2746.tar.gz
scala-0212d5e04ad20ddeee78020d0b57f0b8fc8a2746.tar.bz2
scala-0212d5e04ad20ddeee78020d0b57f0b8fc8a2746.zip
- Added a means of observing pi-calculus reduct...
- Added a means of observing pi-calculus reductions in Pilib. - Made examples "pilib/twoPlaceBuffer" and "pilib/scheduler" use these examples.
Diffstat (limited to 'sources/examples/pilib/scheduler.scala')
-rw-r--r--sources/examples/pilib/scheduler.scala76
1 files changed, 38 insertions, 38 deletions
diff --git a/sources/examples/pilib/scheduler.scala b/sources/examples/pilib/scheduler.scala
index ca377ddcf6..bc291d8f56 100644
--- a/sources/examples/pilib/scheduler.scala
+++ b/sources/examples/pilib/scheduler.scala
@@ -1,6 +1,6 @@
-object scheduler {
+import scala.concurrent.pilib._;
- import scala.concurrent.pilib._;
+object scheduler {
/**
* Random number generator.
@@ -13,60 +13,60 @@ object scheduler {
* A cell of the scheduler whose attached agent is allowed to start.
*/
def A(a: Chan[unit], b: Chan[unit])(d: Chan[unit], c: Chan[unit]): unit = {
- a.read;
- C(a,b)(d,c)
+ ///- ... complete here ...
+ choice ( a * { x => C(a, b)(d, c) })
+ ///+
}
/**
- * A cell of the scheduler in an intermediate state (incorrect)
+ * A cell of the scheduler in another intermediate state.
*/
-// def B(a: Chan[unit], b: Chan[unit])(d: Chan[unit], c: Chan[unit]): unit = {
-// b.read;
-// D(a,b)(d,c)
-// }
+ def C(a: Chan[unit], b: Chan[unit])(d: Chan[unit], c: Chan[unit]): unit = {
+ ///- ... complete here ...
+ choice (c * { x => B(a, b)(d, c) })
+ ///+
+ }
/**
- * A cell of the scheduler in an intermediate state (correct).
+ * A cell of the scheduler whose attached agent is allowed to finish.
*/
- def B(a: Chan[unit], b: Chan[unit])(d: Chan[unit], c: Chan[unit]): unit =
+ def B(a: Chan[unit], b: Chan[unit])(d: Chan[unit], c: Chan[unit]): unit = {
+ ///- ... complete here ...
+ // choice (b * { x => D(a, b)(d, c) }) // incorrect naive solution
choice (
- b * (x => D(a,b)(d,c)),
- d(()) * ({ b.read; A(a,b)(d,c)})
- );
-
- /**
- * A cell of the scheduler in another intermediate state.
- */
- def C(a: Chan[unit], b: Chan[unit])(d: Chan[unit], c: Chan[unit]): unit = {
- c.read;
- B(a,b)(d,c)
- }
+ b * { x => choice ( d(()) * A(a, b)(d, c) ) }, // b.'d.A
+ d(()) * (choice (b * { x => A(a, b)(d, c) })) // 'd.b.A
+ )
+ ///+
+ }
/**
* A cell of the scheduler whose attached agent is not yet allowed to start.
*/
def D(a: Chan[unit], b: Chan[unit])(d: Chan[unit], c: Chan[unit]): unit = {
- d.write(());
- A(a,b)(d,c)
+ ///- ... complete here ...
+ choice (d(()) * A(a, b)(d, c))
+ ///+
}
//***************** Agents ******************//
def agent(i: Int)(a: Chan[unit], b: Chan[unit]): unit = {
- Thread.sleep(random.nextInt(1000) + 1);
- a.write(());
- System.out.println("Starting agent " + i);
- Thread.sleep(random.nextInt(1000) + 1);
-
- // 10% chance that we sleep for a long while.
- if(random.nextInt(10) == 0) {
- System.out.println("Agent " + i + " sleeps");
- Thread.sleep(20000);
+ // 50% chance that we sleep forever
+ if (i == 0 && random.nextInt(10) < 5) {
+ a.attach(x => System.out.println("Start and sleeps ----> " + i));
+ Thread.sleep(random.nextInt(1000));
+ a.write(());
+ }
+ else {
+ a.attach(x => System.out.println("Start ----> " + i));
+ b.attach(x => System.out.println("Stop -> " + i));
+ Thread.sleep(random.nextInt(1000));
+ a.write(());
+ Thread.sleep(random.nextInt(1000));
+ b.write(());
+ agent(i)(a, b)
}
-
- b.write(());
- System.out.println("Ending agent " + i);
- agent(i)(a, b)
}
//***************** Entry function ******************//
@@ -77,7 +77,7 @@ object scheduler {
def main(args: Array[String]): unit = {
val agentNb = 5;
- val agents = for(val i <- List.range(0, agentNb)) yield agent(i);
+ val agents = List.range(0, agentNb) map agent;
scheduleAgents(agents);
}