diff options
Diffstat (limited to 'sources')
-rw-r--r-- | sources/examples/jolib/Ref.scala | 41 | ||||
-rw-r--r-- | sources/examples/jolib/parallelOr.scala | 46 |
2 files changed, 87 insertions, 0 deletions
diff --git a/sources/examples/jolib/Ref.scala b/sources/examples/jolib/Ref.scala new file mode 100644 index 0000000000..09846b4bd6 --- /dev/null +++ b/sources/examples/jolib/Ref.scala @@ -0,0 +1,41 @@ +import concurrent.jolib._; +import concurrent.SyncVar; + +class Ref(init: int) extends Join { + + object get extends Synchr[int](this) { case class C() extends SyncVar[int]; } + object set extends Synchr[unit](this) { case class C(x: int) extends SyncVar[unit]; } + object state extends Asynchr(this) { case class C(x: int); } + + rules ( + Pair(List(get, state), { case List(g @ get.C(), state.C(x) ) => { g.set(x); state.send(state.C(x)) } }), + Pair(List(set, state), { case List(s @ set.C(x), state.C(y) ) => { s.set(()); state.send(state.C(x)) } }) + ); + + state.send(state.C(init)); + + def Get: int = get.send(get.C()); + def Set(x: int): unit = set.send(set.C(x)); +} + +object RefTest { + + def main(args: Array[String]) = { + System.out.println("Started."); + concurrent.ops.spawn({ + val r1 = new Ref(0); + System.out.println("Reference r1 created."); + System.out.println("Value r1 (first time) = " + r1.Get); + r1.Set(42); + System.out.println("Value r1 (second time) = " + r1.Get); + }); + concurrent.ops.spawn({ + val r2 = new Ref(100); + System.out.println("Reference r2 created."); + System.out.println("Value r2 (first time) = " + r2.Get); + r2.Set(89); + System.out.println("Value r2 (second time) = " + r2.Get); + }); + } + +} diff --git a/sources/examples/jolib/parallelOr.scala b/sources/examples/jolib/parallelOr.scala new file mode 100644 index 0000000000..40abcc0a87 --- /dev/null +++ b/sources/examples/jolib/parallelOr.scala @@ -0,0 +1,46 @@ +import concurrent.jolib._; +import concurrent.SyncVar; + +/** Implementation in the join-calculus of a parallel OR. */ +object or extends Join { + + object res extends Synchr[boolean](this) { case class C() extends SyncVar[boolean] }; + object res1 extends Asynchr(this) { case class C(b: boolean); } + object res2 extends Asynchr(this) { case class C(b: boolean); } + object res1False extends Synchr[boolean](this) { case class C() extends SyncVar[boolean] }; + object res2False extends Synchr[boolean](this) { case class C() extends SyncVar[boolean] }; + + rules( + Pair(List(res, res1), { case List(r @ res.C(), res1.C(b)) => + if (b) r.set(b) else r.set(res1False.send(res1False.C())) }), + + Pair(List(res, res2), { case List(r @ res.C(), res2.C(b)) => + if (b) r.set(b) else r.set(res2False.send(res2False.C())) }), + + Pair(List(res1False, res2), { case List(r @ res1False.C(), res2.C(b)) => + r.set(b) }), + + Pair(List(res2False, res1), { case List(r @ res2False.C(), res1.C(b)) => + r.set(b) }) + ); + + def apply(def b1: boolean, def b2: boolean): boolean = { + concurrent.ops.spawn(res1.send(res1.C(b1))); + concurrent.ops.spawn(res2.send(res2.C(b2))); + res.send(res.C()) + } +} + +object parallelOr { + + def main(args: Array[String]): unit = { + def loop: boolean = { while (true) {}; true }; + System.out.println("true || true = " + or(true, true)); + System.out.println("false || false = " + or(false, false)); + System.out.println("false || true = " + or(false, true)); + System.out.println("true || false = " + or(true, false)); + System.out.println("true || loop = " + or(true, loop)); + System.out.println("loop || true = " + or(loop, true)); + } + +} |