summaryrefslogtreecommitdiff
path: root/docs/examples/pilib/semaphore.scala
diff options
context:
space:
mode:
authormichelou <michelou@epfl.ch>2006-02-22 17:54:31 +0000
committermichelou <michelou@epfl.ch>2006-02-22 17:54:31 +0000
commit96ae92e4f6f830a9a4e55768c3de0328a2a030ba (patch)
tree0b84d247c1693bf186787aaa8f0c75d89fea9be3 /docs/examples/pilib/semaphore.scala
parentc1e184a3657d970a8fba6e3c7049f20a2e466bf0 (diff)
downloadscala-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/semaphore.scala')
-rw-r--r--docs/examples/pilib/semaphore.scala48
1 files changed, 24 insertions, 24 deletions
diff --git a/docs/examples/pilib/semaphore.scala b/docs/examples/pilib/semaphore.scala
index cfb0c8e5a2..30e3c00975 100644
--- a/docs/examples/pilib/semaphore.scala
+++ b/docs/examples/pilib/semaphore.scala
@@ -1,70 +1,70 @@
-package examples.pilib;
+package examples.pilib
/** Solution of exercise session 6 (first question). */
object semaphore {
- import scala.concurrent.pilib._;
+ import scala.concurrent.pilib._
class Signal extends Chan[unit] {
- def send = write(());
- def receive = read;
+ def send = write(())
+ def receive = read
}
/** Interface. */
trait Semaphore {
- def get: unit;
- def release: unit;
+ def get: unit
+ def release: unit
}
/** First implementation. */
class Sem1 extends Semaphore {
- private val g = new Signal;
- private val r = new Signal;
+ private val g = new Signal
+ private val r = new Signal
- def get: unit = g.send;
- def release: unit = r.send;
+ def get: unit = g.send
+ def release: unit = r.send
private def Sched: unit = choice (
g * (x => { r.receive; Sched }),
r * (x => Sched)
- );
- spawn< Sched >;
+ )
+ spawn< Sched >
}
/** Second implementation. */
class Sem2 extends Semaphore {
- private val a = new Signal;
- private val na = new Signal;
+ private val a = new Signal
+ private val na = new Signal
def get: unit = { a.receive; spawn< na.send > }
def release: unit = choice (
a * (x => spawn< a.send >),
na * (x => spawn< a.send >)
- );
- spawn< a.send >;
+ )
+ spawn< a.send >
}
/** Test program. */
def main(args: Array[String]): unit = {
- val random = new java.util.Random();
- val sem = new Sem2;
+ val random = new java.util.Random()
+ val sem = new Sem2
def mutex(p: => unit): unit = { sem.get; p; sem.release }
spawn< {
Thread.sleep(1 + random.nextInt(100));
mutex( {
- System.out.println("a1");
- Thread.sleep(1 + random.nextInt(100));
- System.out.println("a2")
+ System.out.println("a1");
+ Thread.sleep(1 + random.nextInt(100));
+ System.out.println("a2")
} )
} | {
Thread.sleep(1 + random.nextInt(100));
mutex( {
- System.out.println("b1");
- Thread.sleep(1 + random.nextInt(100));
- System.out.println("b2")
+ System.out.println("b1");
+ Thread.sleep(1 + random.nextInt(100));
+ System.out.println("b2")
} )
} >;
}