summaryrefslogtreecommitdiff
path: root/src/actors
diff options
context:
space:
mode:
authorPhilipp Haller <hallerp@gmail.com>2010-02-02 12:40:38 +0000
committerPhilipp Haller <hallerp@gmail.com>2010-02-02 12:40:38 +0000
commit5df06dc8dafef7f76b6783e7a473f30678c55d58 (patch)
tree44236316be9220ac2ab04e958fca9a76654c9082 /src/actors
parent2b20a98b3fa7cbd952554c7d460bdaab9e25a933 (diff)
downloadscala-5df06dc8dafef7f76b6783e7a473f30678c55d58.tar.gz
scala-5df06dc8dafef7f76b6783e7a473f30678c55d58.tar.bz2
scala-5df06dc8dafef7f76b6783e7a473f30678c55d58.zip
Closes #3009.
Diffstat (limited to 'src/actors')
-rw-r--r--src/actors/scala/actors/Future.scala37
1 files changed, 26 insertions, 11 deletions
diff --git a/src/actors/scala/actors/Future.scala b/src/actors/scala/actors/Future.scala
index ebb0489d88..cbdba8010e 100644
--- a/src/actors/scala/actors/Future.scala
+++ b/src/actors/scala/actors/Future.scala
@@ -46,11 +46,15 @@ abstract class Future[+T](val inputChannel: InputChannel[T]) extends Responder[T
*/
object Futures {
+ import scala.concurrent.SyncVar
+
private case object Eval
- private class FutureActor[T](fun: () => T, channel: Channel[T])
+ private class FutureActor[T](fun: SyncVar[T] => Unit, channel: Channel[T])
extends Future[T](channel) with DaemonActor {
+ import Actor._
+
def isSet = !fvalue.isEmpty
def apply(): T = {
@@ -70,12 +74,17 @@ object Futures {
}
def act() {
- val res = fun()
- fvalue = Some(res)
- channel ! res
- Actor.loop {
- Actor.react {
- case Eval => Actor.reply()
+ val res = new SyncVar[T]
+
+ {
+ fun(res)
+ } andThen {
+ fvalue = Some(res.get)
+ channel ! res.get
+ loop {
+ react {
+ case Eval => reply()
+ }
}
}
}
@@ -90,7 +99,7 @@ object Futures {
*/
def future[T](body: => T): Future[T] = {
val c = new Channel[T](Actor.self(DaemonScheduler))
- val a = new FutureActor[T](() => body, c)
+ val a = new FutureActor[T](_.set(body), c)
a.start()
a
}
@@ -100,10 +109,16 @@ object Futures {
* @param timespan the time span in ms after which the future resolves
* @return the future
*/
- def alarm(timespan: Long) = future {
- Actor.reactWithin(timespan) {
- case TIMEOUT => {}
+ def alarm(timespan: Long): Future[Unit] = {
+ val c = new Channel[Unit](Actor.self(DaemonScheduler))
+ val fun = (res: SyncVar[Unit]) => {
+ Actor.reactWithin(timespan) {
+ case TIMEOUT => res.set({})
+ }
}
+ val a = new FutureActor[Unit](fun, c)
+ a.start()
+ a
}
/** Waits for the first result returned by one of two