aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/scala/async/FutureSystem.scala
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/scala/scala/async/FutureSystem.scala')
-rw-r--r--src/main/scala/scala/async/FutureSystem.scala61
1 files changed, 58 insertions, 3 deletions
diff --git a/src/main/scala/scala/async/FutureSystem.scala b/src/main/scala/scala/async/FutureSystem.scala
index a050bec..250d6ed 100644
--- a/src/main/scala/scala/async/FutureSystem.scala
+++ b/src/main/scala/scala/async/FutureSystem.scala
@@ -47,7 +47,7 @@ trait FutureSystem {
/** Register an call back to run on completion of the given future */
def onComplete[A, U](future: Expr[Fut[A]], fun: Expr[scala.util.Try[A] => U],
- execContext: Expr[ExecContext]): Expr[Unit]
+ execContext: Expr[ExecContext], stateMachine: Expr[StateMachine[Prom[A], ExecContext]]): Expr[Unit]
/** Complete a promise with a value */
def completeProm[A](prom: Expr[Prom[A]], value: Expr[scala.util.Try[A]]): Expr[Unit]
@@ -95,7 +95,7 @@ object ScalaConcurrentFutureSystem extends FutureSystem {
}
def onComplete[A, U](future: Expr[Fut[A]], fun: Expr[scala.util.Try[A] => U],
- execContext: Expr[ExecContext]): Expr[Unit] = reify {
+ execContext: Expr[ExecContext], stateMachine: Expr[StateMachine[Prom[A], ExecContext]]): Expr[Unit] = reify {
future.splice.onComplete(fun.splice)(execContext.splice)
}
@@ -110,6 +110,61 @@ object ScalaConcurrentFutureSystem extends FutureSystem {
}
}
+/** This future system implements `await` using blocking. Note that this
+ * future system should only be used for the purpose of debugging.
+ */
+object BlockingFutureSystem extends FutureSystem {
+
+ import scala.concurrent._
+ import scala.concurrent.duration._
+
+ type Prom[A] = Promise[A]
+ type Fut[A] = Future[A]
+ type ExecContext = ExecutionContext
+
+ def mkOps(c: Context): Ops {val context: c.type} = new Ops {
+ val context: c.type = c
+
+ import context.universe._
+
+ def execContext: Expr[ExecContext] = c.Expr(c.inferImplicitValue(c.weakTypeOf[ExecutionContext]) match {
+ case EmptyTree => c.abort(c.macroApplication.pos, "Unable to resolve implicit ExecutionContext")
+ case context => context
+ })
+
+ def promType[A: WeakTypeTag]: Type = c.weakTypeOf[Promise[A]]
+ def execContextType: Type = c.weakTypeOf[ExecutionContext]
+
+ def createProm[A: WeakTypeTag]: Expr[Prom[A]] = reify {
+ Promise[A]()
+ }
+
+ def promiseToFuture[A: WeakTypeTag](prom: Expr[Prom[A]]) = reify {
+ prom.splice.future
+ }
+
+ def future[A: WeakTypeTag](a: Expr[A])(execContext: Expr[ExecContext]) = reify {
+ Future(a.splice)(execContext.splice)
+ }
+
+ def onComplete[A, U](future: Expr[Fut[A]], fun: Expr[scala.util.Try[A] => U],
+ execContext: Expr[ExecContext], stateMachine: Expr[StateMachine[Prom[A], ExecContext]]): Expr[Unit] = reify {
+ Await.ready(future.splice, Duration.Inf)
+ val tr = future.splice.value.get
+ stateMachine.splice.task$async = Some(() => fun.splice(tr))
+ }
+
+ def completeProm[A](prom: Expr[Prom[A]], value: Expr[scala.util.Try[A]]): Expr[Unit] = reify {
+ prom.splice.complete(value.splice)
+ context.literalUnit.splice
+ }
+
+ def castTo[A: WeakTypeTag](future: Expr[Fut[Any]]): Expr[Fut[A]] = reify {
+ future.splice.asInstanceOf[Fut[A]]
+ }
+ }
+}
+
/**
* A trivial implementation of [[scala.async.FutureSystem]] that performs computations
* on the current thread. Useful for testing.
@@ -142,7 +197,7 @@ object IdentityFutureSystem extends FutureSystem {
def future[A: WeakTypeTag](t: Expr[A])(execContext: Expr[ExecContext]) = t
def onComplete[A, U](future: Expr[Fut[A]], fun: Expr[scala.util.Try[A] => U],
- execContext: Expr[ExecContext]): Expr[Unit] = reify {
+ execContext: Expr[ExecContext], stateMachine: Expr[StateMachine[Prom[A], ExecContext]]): Expr[Unit] = reify {
fun.splice.apply(util.Success(future.splice))
context.literalUnit.splice
}