| author | haftmann | 
| Mon, 23 Dec 2013 14:24:21 +0100 | |
| changeset 54848 | a303daddebbf | 
| parent 45673 | cd41e3903fbf | 
| child 56663 | 2d09b437c168 | 
| permissions | -rw-r--r-- | 
| 34217 | 1  | 
/* Title: Pure/Concurrent/future.scala  | 
| 
45673
 
cd41e3903fbf
separate compilation of PIDE vs. Pure sources, which enables independent Scala library;
 
wenzelm 
parents: 
45667 
diff
changeset
 | 
2  | 
Module: PIDE  | 
| 34217 | 3  | 
Author: Makarius  | 
4  | 
||
5  | 
Future values.  | 
|
6  | 
||
7  | 
Notable differences to scala.actors.Future (as of Scala 2.7.7):  | 
|
8  | 
||
9  | 
* We capture/release exceptions as in the ML variant.  | 
|
10  | 
||
11  | 
* Future.join works for *any* actor, not just the one of the  | 
|
12  | 
original fork.  | 
|
13  | 
*/  | 
|
14  | 
||
15  | 
package isabelle  | 
|
16  | 
||
17  | 
||
18  | 
import scala.actors.Actor._  | 
|
19  | 
||
20  | 
||
21  | 
object Future  | 
|
22  | 
{
 | 
|
23  | 
def value[A](x: A): Future[A] = new Finished_Future(x)  | 
|
24  | 
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
 | 
25  | 
def promise[A]: Promise[A] = new Promise_Future  | 
| 34217 | 26  | 
}  | 
27  | 
||
28  | 
abstract class Future[A]  | 
|
29  | 
{
 | 
|
30  | 
def peek: Option[Exn.Result[A]]  | 
|
31  | 
def is_finished: Boolean = peek.isDefined  | 
|
| 
38848
 
9483bb678d96
use Future.get_finished where this is the intended meaning -- prefer immediate crash over deadlock due to promises that are never fulfilled;
 
wenzelm 
parents: 
34262 
diff
changeset
 | 
32  | 
  def get_finished: A = { require(is_finished); Exn.release(peek.get) }
 | 
| 34217 | 33  | 
def join: A  | 
34  | 
  def map[B](f: A => B): Future[B] = Future.fork { f(join) }
 | 
|
35  | 
||
36  | 
override def toString =  | 
|
37  | 
    peek match {
 | 
|
38  | 
case None => "<future>"  | 
|
39  | 
case Some(Exn.Exn(_)) => "<failed>"  | 
|
40  | 
case Some(Exn.Res(x)) => x.toString  | 
|
41  | 
}  | 
|
42  | 
}  | 
|
43  | 
||
| 
34240
 
3274571e45c1
added Future.promise -- essentially a single-assignment variable with signalling, using the Future interface;
 
wenzelm 
parents: 
34217 
diff
changeset
 | 
44  | 
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
 | 
45  | 
{
 | 
| 34262 | 46  | 
def fulfill_result(res: Exn.Result[A]): Unit  | 
47  | 
  def fulfill(x: A) { fulfill_result(Exn.Res(x)) }
 | 
|
| 
34240
 
3274571e45c1
added Future.promise -- essentially a single-assignment variable with signalling, using the Future interface;
 
wenzelm 
parents: 
34217 
diff
changeset
 | 
48  | 
}  | 
| 
 
3274571e45c1
added Future.promise -- essentially a single-assignment variable with signalling, using the Future interface;
 
wenzelm 
parents: 
34217 
diff
changeset
 | 
49  | 
|
| 34217 | 50  | 
private class Finished_Future[A](x: A) extends Future[A]  | 
51  | 
{
 | 
|
52  | 
val peek: Option[Exn.Result[A]] = Some(Exn.Res(x))  | 
|
53  | 
val join: A = x  | 
|
54  | 
}  | 
|
55  | 
||
56  | 
private class Pending_Future[A](body: => A) extends Future[A]  | 
|
57  | 
{
 | 
|
58  | 
@volatile private var result: Option[Exn.Result[A]] = None  | 
|
59  | 
||
60  | 
  private val evaluator = actor {
 | 
|
61  | 
result = Some(Exn.capture(body))  | 
|
62  | 
    loop { react { case _ => reply(result.get) } }
 | 
|
63  | 
}  | 
|
64  | 
||
65  | 
def peek: Option[Exn.Result[A]] = result  | 
|
66  | 
||
67  | 
def join: A =  | 
|
68  | 
    Exn.release {
 | 
|
69  | 
      peek match {
 | 
|
70  | 
case Some(res) => res  | 
|
71  | 
case None => (evaluator !? ()).asInstanceOf[Exn.Result[A]]  | 
|
72  | 
}  | 
|
73  | 
}  | 
|
74  | 
}  | 
|
75  | 
||
| 
34240
 
3274571e45c1
added Future.promise -- essentially a single-assignment variable with signalling, using the Future interface;
 
wenzelm 
parents: 
34217 
diff
changeset
 | 
76  | 
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
 | 
77  | 
{
 | 
| 34262 | 78  | 
@volatile private var result: Option[Exn.Result[A]] = None  | 
| 34217 | 79  | 
|
| 
34240
 
3274571e45c1
added Future.promise -- essentially a single-assignment variable with signalling, using the Future interface;
 
wenzelm 
parents: 
34217 
diff
changeset
 | 
80  | 
private case object Read  | 
| 34262 | 81  | 
private case class Write(res: Exn.Result[A])  | 
| 
34240
 
3274571e45c1
added Future.promise -- essentially a single-assignment variable with signalling, using the Future interface;
 
wenzelm 
parents: 
34217 
diff
changeset
 | 
82  | 
|
| 
 
3274571e45c1
added Future.promise -- essentially a single-assignment variable with signalling, using the Future interface;
 
wenzelm 
parents: 
34217 
diff
changeset
 | 
83  | 
  private val receiver = actor {
 | 
| 
 
3274571e45c1
added Future.promise -- essentially a single-assignment variable with signalling, using the Future interface;
 
wenzelm 
parents: 
34217 
diff
changeset
 | 
84  | 
    loop {
 | 
| 
 
3274571e45c1
added Future.promise -- essentially a single-assignment variable with signalling, using the Future interface;
 
wenzelm 
parents: 
34217 
diff
changeset
 | 
85  | 
      react {
 | 
| 
 
3274571e45c1
added Future.promise -- essentially a single-assignment variable with signalling, using the Future interface;
 
wenzelm 
parents: 
34217 
diff
changeset
 | 
86  | 
case Read if result.isDefined => reply(result.get)  | 
| 34262 | 87  | 
case Write(res) =>  | 
| 
34240
 
3274571e45c1
added Future.promise -- essentially a single-assignment variable with signalling, using the Future interface;
 
wenzelm 
parents: 
34217 
diff
changeset
 | 
88  | 
if (result.isDefined) reply(false)  | 
| 34262 | 89  | 
          else { result = Some(res); reply(true) }
 | 
| 
34240
 
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  | 
}  | 
| 
 
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  | 
|
| 34262 | 94  | 
def peek: Option[Exn.Result[A]] = result  | 
| 
34240
 
3274571e45c1
added Future.promise -- essentially a single-assignment variable with signalling, using the Future interface;
 
wenzelm 
parents: 
34217 
diff
changeset
 | 
95  | 
|
| 
 
3274571e45c1
added Future.promise -- essentially a single-assignment variable with signalling, using the Future interface;
 
wenzelm 
parents: 
34217 
diff
changeset
 | 
96  | 
def join: A =  | 
| 34262 | 97  | 
    Exn.release {
 | 
98  | 
      result match {
 | 
|
99  | 
case Some(res) => res  | 
|
100  | 
case None => (receiver !? Read).asInstanceOf[Exn.Result[A]]  | 
|
101  | 
}  | 
|
| 
34240
 
3274571e45c1
added Future.promise -- essentially a single-assignment variable with signalling, using the Future interface;
 
wenzelm 
parents: 
34217 
diff
changeset
 | 
102  | 
}  | 
| 
 
3274571e45c1
added Future.promise -- essentially a single-assignment variable with signalling, using the Future interface;
 
wenzelm 
parents: 
34217 
diff
changeset
 | 
103  | 
|
| 34262 | 104  | 
  def fulfill_result(res: Exn.Result[A]) {
 | 
105  | 
    receiver !? Write(res) match {
 | 
|
| 
34240
 
3274571e45c1
added Future.promise -- essentially a single-assignment variable with signalling, using the Future interface;
 
wenzelm 
parents: 
34217 
diff
changeset
 | 
106  | 
      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
 | 
107  | 
case _ =>  | 
| 
 
3274571e45c1
added Future.promise -- essentially a single-assignment variable with signalling, using the Future interface;
 
wenzelm 
parents: 
34217 
diff
changeset
 | 
108  | 
}  | 
| 
 
3274571e45c1
added Future.promise -- essentially a single-assignment variable with signalling, using the Future interface;
 
wenzelm 
parents: 
34217 
diff
changeset
 | 
109  | 
}  | 
| 
 
3274571e45c1
added Future.promise -- essentially a single-assignment variable with signalling, using the Future interface;
 
wenzelm 
parents: 
34217 
diff
changeset
 | 
110  | 
}  | 
| 
 
3274571e45c1
added Future.promise -- essentially a single-assignment variable with signalling, using the Future interface;
 
wenzelm 
parents: 
34217 
diff
changeset
 | 
111  |