diff options
author | michelou <michelou@epfl.ch> | 2006-02-22 17:54:31 +0000 |
---|---|---|
committer | michelou <michelou@epfl.ch> | 2006-02-22 17:54:31 +0000 |
commit | 96ae92e4f6f830a9a4e55768c3de0328a2a030ba (patch) | |
tree | 0b84d247c1693bf186787aaa8f0c75d89fea9be3 /docs/examples/pilib/scheduler.scala | |
parent | c1e184a3657d970a8fba6e3c7049f20a2e466bf0 (diff) | |
download | scala-96ae92e4f6f830a9a4e55768c3de0328a2a030ba.tar.gz scala-96ae92e4f6f830a9a4e55768c3de0328a2a030ba.tar.bz2 scala-96ae92e4f6f830a9a4e55768c3de0328a2a030ba.zip |
adapted code to Scala 2 syntax in files src/exa...
adapted code to Scala 2 syntax in files src/examples/**/*.scala
Diffstat (limited to 'docs/examples/pilib/scheduler.scala')
-rw-r--r-- | docs/examples/pilib/scheduler.scala | 107 |
1 files changed, 54 insertions, 53 deletions
diff --git a/docs/examples/pilib/scheduler.scala b/docs/examples/pilib/scheduler.scala index 3b08a9df66..8946a5a0b2 100644 --- a/docs/examples/pilib/scheduler.scala +++ b/docs/examples/pilib/scheduler.scala @@ -1,19 +1,19 @@ -package examples.pilib; +package examples.pilib -import scala.concurrent.pilib._; +import scala.concurrent.pilib._ object scheduler { /** - * Random number generator. - */ - val random = new java.util.Random(); + * Random number generator. + */ + val random = new java.util.Random() //***************** Scheduler ******************// /** - * A cell of the scheduler whose attached agent is allowed to start. - */ + * 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 = { ///- ... complete here ... choice ( a * { x => C(a, b)(d, c) }) @@ -21,8 +21,8 @@ object scheduler { } /** - * A cell of the scheduler in another intermediate state. - */ + * A cell of the scheduler in another intermediate state. + */ 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) }) @@ -30,8 +30,8 @@ object scheduler { } /** - * A cell of the scheduler whose attached agent is allowed to finish. - */ + * 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 = { ///- ... complete here ... // choice (b * { x => D(a, b)(d, c) }) // incorrect naive solution @@ -43,8 +43,8 @@ object scheduler { } /** - * A cell of the scheduler whose attached agent is not yet allowed to start. - */ + * 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 = { ///- ... complete here ... choice (d(()) * A(a, b)(d, c)) @@ -56,44 +56,44 @@ object scheduler { def agent(i: Int)(a: Chan[unit], b: Chan[unit]): unit = { // 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(()); + 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) + 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) } } //***************** Entry function ******************// /** - * Creates a scheduler for five agents (programs). - */ + * Creates a scheduler for five agents (programs). + */ def main(args: Array[String]): unit = { - val agentNb = 5; - val agents = List.range(0, agentNb) map agent; - scheduleAgents(agents); + val agentNb = 5 + val agents = List.range(0, agentNb) map agent + scheduleAgents(agents) } //***************** Infrastructure *****************// /** - * A cell is modelled as a function that takes as parameters - * input and output channels and which returns nothing. - */ - type Cell = (Chan[unit], Chan[unit]) => unit; + * A cell is modelled as a function that takes as parameters + * input and output channels and which returns nothing. + */ + type Cell = (Chan[unit], Chan[unit]) => unit /** - * Creates a cell composed of two cells linked together. - */ + * Creates a cell composed of two cells linked together. + */ def join(cell1: Cell, cell2: Cell): Cell = (l: Chan[unit], r: Chan[unit]) => { val link = new Chan[unit]; @@ -101,49 +101,50 @@ object scheduler { }; /** - * Links the output of a cell to its input. - */ + * Links the output of a cell to its input. + */ def close(cell: Cell): unit = { - val a = new Chan[unit]; + val a = new Chan[unit] cell(a, a) } /** - * Creates a cell consisting of a chain of cells. - */ + * Creates a cell consisting of a chain of cells. + */ def chain(cells: List[Cell]): Cell = - cells reduceLeft join; + cells reduceLeft join /** - * Creates a cell consisting of a chain of cells. - */ + * Creates a cell consisting of a chain of cells. + */ def makeRing(cells: List[Cell]): unit = - close(chain(cells)); + close(chain(cells)) /** - * An agent is modelled as a function that takes as parameters channels to - * signal that it has started or finished. - */ - type Agent = (Chan[unit], Chan[unit]) => unit; + * An agent is modelled as a function that takes as parameters channels to + * signal that it has started or finished. + */ + type Agent = (Chan[unit], Chan[unit]) => unit /** - * Takes a list of agents and schedules them. - */ + * Takes a list of agents and schedules them. + */ def scheduleAgents(agents: List[Agent]): unit = { var firstAgent = true; val cells = agents map (ag => { val a = new Chan[unit]; val b = new Chan[unit]; spawn < ag(a, b) >; - if (firstAgent) { - firstAgent = false; - A(a, b) + (d: Chan[unit], c: Chan[unit]) => if (firstAgent) { + firstAgent = false; + A(a, b)(d, c) } else - D(a, b) + D(a, b)(d, c) }); makeRing(cells) } + } |