diff options
author | Philipp Haller <hallerp@gmail.com> | 2013-06-25 02:54:40 +0200 |
---|---|---|
committer | Philipp Haller <hallerp@gmail.com> | 2013-06-25 02:55:26 +0200 |
commit | 295904a7aa53a5cfce96c2003d3f15eb36ba40c9 (patch) | |
tree | 444fcde60a1c388b2b41bb5a1dff8212602d3d46 /src/main/scala/scala/async/FutureSystem.scala | |
parent | 6f6851c68659eae0d5d04ac9713413a3e592bd90 (diff) | |
download | scala-async-topic/seq-debugging.tar.gz scala-async-topic/seq-debugging.tar.bz2 scala-async-topic/seq-debugging.zip |
Add BlockingAsync and BlockingFutureSystemtopic/seq-debugging
Diffstat (limited to 'src/main/scala/scala/async/FutureSystem.scala')
-rw-r--r-- | src/main/scala/scala/async/FutureSystem.scala | 61 |
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 } |