| author | wenzelm |
| Mon, 04 Jan 2010 18:54:22 +0100 | |
| changeset 34240 | 3274571e45c1 |
| parent 34217 | 67e1ac2d3b2c |
| child 34262 | a6d2d9c07e46 |
| permissions | -rw-r--r-- |
| 34217 | 1 |
/* Title: Pure/Concurrent/future.scala |
2 |
Author: Makarius |
|
3 |
||
4 |
Future values. |
|
5 |
||
6 |
Notable differences to scala.actors.Future (as of Scala 2.7.7): |
|
7 |
||
8 |
* We capture/release exceptions as in the ML variant. |
|
9 |
||
10 |
* Future.join works for *any* actor, not just the one of the |
|
11 |
original fork. |
|
12 |
*/ |
|
13 |
||
14 |
package isabelle |
|
15 |
||
16 |
||
17 |
import scala.actors.Actor._ |
|
18 |
||
19 |
||
20 |
object Future |
|
21 |
{
|
|
22 |
def value[A](x: A): Future[A] = new Finished_Future(x) |
|
23 |
def fork[A](body: => A): Future[A] = new Pending_Future(body) |
|
|
34240
3274571e45c1
added Future.promise -- essentially a single-assignment variable with signalling, using the Future interface;
wenzelm
parents:
34217
diff
changeset
|
24 |
def promise[A]: Promise[A] = new Promise_Future |
| 34217 | 25 |
} |
26 |
||
27 |
abstract class Future[A] |
|
28 |
{
|
|
29 |
def peek: Option[Exn.Result[A]] |
|
30 |
def is_finished: Boolean = peek.isDefined |
|
31 |
def join: A |
|
32 |
def map[B](f: A => B): Future[B] = Future.fork { f(join) }
|
|
33 |
||
34 |
override def toString = |
|
35 |
peek match {
|
|
36 |
case None => "<future>" |
|
37 |
case Some(Exn.Exn(_)) => "<failed>" |
|
38 |
case Some(Exn.Res(x)) => x.toString |
|
39 |
} |
|
40 |
} |
|
41 |
||
|
34240
3274571e45c1
added Future.promise -- essentially a single-assignment variable with signalling, using the Future interface;
wenzelm
parents:
34217
diff
changeset
|
42 |
abstract class Promise[A] extends Future[A] |
|
3274571e45c1
added Future.promise -- essentially a single-assignment variable with signalling, using the Future interface;
wenzelm
parents:
34217
diff
changeset
|
43 |
{
|
|
3274571e45c1
added Future.promise -- essentially a single-assignment variable with signalling, using the Future interface;
wenzelm
parents:
34217
diff
changeset
|
44 |
def fulfill(x: A): Unit |
|
3274571e45c1
added Future.promise -- essentially a single-assignment variable with signalling, using the Future interface;
wenzelm
parents:
34217
diff
changeset
|
45 |
} |
|
3274571e45c1
added Future.promise -- essentially a single-assignment variable with signalling, using the Future interface;
wenzelm
parents:
34217
diff
changeset
|
46 |
|
| 34217 | 47 |
private class Finished_Future[A](x: A) extends Future[A] |
48 |
{
|
|
49 |
val peek: Option[Exn.Result[A]] = Some(Exn.Res(x)) |
|
50 |
val join: A = x |
|
51 |
} |
|
52 |
||
53 |
private class Pending_Future[A](body: => A) extends Future[A] |
|
54 |
{
|
|
55 |
@volatile private var result: Option[Exn.Result[A]] = None |
|
56 |
||
57 |
private val evaluator = actor {
|
|
58 |
result = Some(Exn.capture(body)) |
|
59 |
loop { react { case _ => reply(result.get) } }
|
|
60 |
} |
|
61 |
||
62 |
def peek: Option[Exn.Result[A]] = result |
|
63 |
||
64 |
def join: A = |
|
65 |
Exn.release {
|
|
66 |
peek match {
|
|
67 |
case Some(res) => res |
|
68 |
case None => (evaluator !? ()).asInstanceOf[Exn.Result[A]] |
|
69 |
} |
|
70 |
} |
|
71 |
} |
|
72 |
||
|
34240
3274571e45c1
added Future.promise -- essentially a single-assignment variable with signalling, using the Future interface;
wenzelm
parents:
34217
diff
changeset
|
73 |
private class Promise_Future[A] extends Promise[A] |
|
3274571e45c1
added Future.promise -- essentially a single-assignment variable with signalling, using the Future interface;
wenzelm
parents:
34217
diff
changeset
|
74 |
{
|
|
3274571e45c1
added Future.promise -- essentially a single-assignment variable with signalling, using the Future interface;
wenzelm
parents:
34217
diff
changeset
|
75 |
@volatile private var result: Option[A] = None |
| 34217 | 76 |
|
|
34240
3274571e45c1
added Future.promise -- essentially a single-assignment variable with signalling, using the Future interface;
wenzelm
parents:
34217
diff
changeset
|
77 |
private case object Read |
|
3274571e45c1
added Future.promise -- essentially a single-assignment variable with signalling, using the Future interface;
wenzelm
parents:
34217
diff
changeset
|
78 |
private case class Write(x: A) |
|
3274571e45c1
added Future.promise -- essentially a single-assignment variable with signalling, using the Future interface;
wenzelm
parents:
34217
diff
changeset
|
79 |
|
|
3274571e45c1
added Future.promise -- essentially a single-assignment variable with signalling, using the Future interface;
wenzelm
parents:
34217
diff
changeset
|
80 |
private val receiver = actor {
|
|
3274571e45c1
added Future.promise -- essentially a single-assignment variable with signalling, using the Future interface;
wenzelm
parents:
34217
diff
changeset
|
81 |
loop {
|
|
3274571e45c1
added Future.promise -- essentially a single-assignment variable with signalling, using the Future interface;
wenzelm
parents:
34217
diff
changeset
|
82 |
react {
|
|
3274571e45c1
added Future.promise -- essentially a single-assignment variable with signalling, using the Future interface;
wenzelm
parents:
34217
diff
changeset
|
83 |
case Read if result.isDefined => reply(result.get) |
|
3274571e45c1
added Future.promise -- essentially a single-assignment variable with signalling, using the Future interface;
wenzelm
parents:
34217
diff
changeset
|
84 |
case Write(x) => |
|
3274571e45c1
added Future.promise -- essentially a single-assignment variable with signalling, using the Future interface;
wenzelm
parents:
34217
diff
changeset
|
85 |
if (result.isDefined) reply(false) |
|
3274571e45c1
added Future.promise -- essentially a single-assignment variable with signalling, using the Future interface;
wenzelm
parents:
34217
diff
changeset
|
86 |
else { result = Some(x); reply(true) }
|
|
3274571e45c1
added Future.promise -- essentially a single-assignment variable with signalling, using the Future interface;
wenzelm
parents:
34217
diff
changeset
|
87 |
} |
|
3274571e45c1
added Future.promise -- essentially a single-assignment variable with signalling, using the Future interface;
wenzelm
parents:
34217
diff
changeset
|
88 |
} |
|
3274571e45c1
added Future.promise -- essentially a single-assignment variable with signalling, using the Future interface;
wenzelm
parents:
34217
diff
changeset
|
89 |
} |
|
3274571e45c1
added Future.promise -- essentially a single-assignment variable with signalling, using the Future interface;
wenzelm
parents:
34217
diff
changeset
|
90 |
|
|
3274571e45c1
added Future.promise -- essentially a single-assignment variable with signalling, using the Future interface;
wenzelm
parents:
34217
diff
changeset
|
91 |
def peek: Option[Exn.Result[A]] = result.map(Exn.Res(_)) |
|
3274571e45c1
added Future.promise -- essentially a single-assignment variable with signalling, using the Future interface;
wenzelm
parents:
34217
diff
changeset
|
92 |
|
|
3274571e45c1
added Future.promise -- essentially a single-assignment variable with signalling, using the Future interface;
wenzelm
parents:
34217
diff
changeset
|
93 |
def join: A = |
|
3274571e45c1
added Future.promise -- essentially a single-assignment variable with signalling, using the Future interface;
wenzelm
parents:
34217
diff
changeset
|
94 |
result match {
|
|
3274571e45c1
added Future.promise -- essentially a single-assignment variable with signalling, using the Future interface;
wenzelm
parents:
34217
diff
changeset
|
95 |
case Some(res) => res |
|
3274571e45c1
added Future.promise -- essentially a single-assignment variable with signalling, using the Future interface;
wenzelm
parents:
34217
diff
changeset
|
96 |
case None => (receiver !? Read).asInstanceOf[A] |
|
3274571e45c1
added Future.promise -- essentially a single-assignment variable with signalling, using the Future interface;
wenzelm
parents:
34217
diff
changeset
|
97 |
} |
|
3274571e45c1
added Future.promise -- essentially a single-assignment variable with signalling, using the Future interface;
wenzelm
parents:
34217
diff
changeset
|
98 |
|
|
3274571e45c1
added Future.promise -- essentially a single-assignment variable with signalling, using the Future interface;
wenzelm
parents:
34217
diff
changeset
|
99 |
def fulfill(x: A) {
|
|
3274571e45c1
added Future.promise -- essentially a single-assignment variable with signalling, using the Future interface;
wenzelm
parents:
34217
diff
changeset
|
100 |
receiver !? Write(x) match {
|
|
3274571e45c1
added Future.promise -- essentially a single-assignment variable with signalling, using the Future interface;
wenzelm
parents:
34217
diff
changeset
|
101 |
case false => error("Duplicate fulfillment of promise")
|
|
3274571e45c1
added Future.promise -- essentially a single-assignment variable with signalling, using the Future interface;
wenzelm
parents:
34217
diff
changeset
|
102 |
case _ => |
|
3274571e45c1
added Future.promise -- essentially a single-assignment variable with signalling, using the Future interface;
wenzelm
parents:
34217
diff
changeset
|
103 |
} |
|
3274571e45c1
added Future.promise -- essentially a single-assignment variable with signalling, using the Future interface;
wenzelm
parents:
34217
diff
changeset
|
104 |
} |
|
3274571e45c1
added Future.promise -- essentially a single-assignment variable with signalling, using the Future interface;
wenzelm
parents:
34217
diff
changeset
|
105 |
} |
|
3274571e45c1
added Future.promise -- essentially a single-assignment variable with signalling, using the Future interface;
wenzelm
parents:
34217
diff
changeset
|
106 |