Permalink
Browse files

Removing a lot of residue and superfluous method declarations. Moving…

… out newPromise to the implementations instead of having it inside impl.Promise
  • Loading branch information...
viktorklang committed Apr 15, 2012
1 parent 578a62f commit 768748b79a93740fe2da2f166b71340446f6bd09
Showing with 8 additions and 83 deletions.
  1. +3 −35 src/library/scala/concurrent/impl/Future.scala
  2. +5 −48 src/library/scala/concurrent/impl/Promise.scala
@@ -19,29 +19,9 @@ private[concurrent] trait Future[+T] extends scala.concurrent.Future[T] with Awa
implicit def executor: ExecutionContext
/** For use only within a Future.flow block or another compatible Delimited Continuations reset block.
*
* Returns the result of this Future without blocking, by suspending execution and storing it as a
* continuation until the result is available.
*/
//def apply(): T @cps[Future[Any]] = shift(this flatMap (_: T => Future[Any]))
/** Tests whether this Future has been completed.
*/
def isCompleted: Boolean
/** The contained value of this Future. Before this Future is completed
* the value will be None. After completion the value will be Some(Right(t))
* if it contains a valid result, or Some(Left(error)) if it contains
* an exception.
*/
def value: Option[Either[Throwable, T]]
def onComplete[U](func: Either[Throwable, T] => U): this.type
}
object Future {
private[concurrent] object Future {
import java.{ lang => jl }
private val toBoxed = Map[Class[_], Class[_]](
@@ -65,24 +45,12 @@ object Future {
def result(atMost: Duration)(implicit permit: CanAwait) = body
}
def boxedType(c: Class[_]): Class[_] = {
if (c.isPrimitive) toBoxed(c) else c
}
def boxedType(c: Class[_]): Class[_] = if (c.isPrimitive) toBoxed(c) else c
def apply[T](body: =>T)(implicit executor: ExecutionContext): Future[T] = {
val promise = new Promise.DefaultPromise[T]()
executor.execute(new Runnable {
def run = {
promise complete {
try {
Right(body)
} catch {
case NonFatal(e) =>
executor.reportFailure(e)
scala.concurrent.resolver(e)
}
}
}
def run = promise complete { try Right(body) catch { case NonFatal(e) => Left(e) } }
})
promise.future
}
@@ -22,54 +22,7 @@ import scala.annotation.tailrec
private[concurrent] trait Promise[T] extends scala.concurrent.Promise[T] with Future[T] {
def future = this
def newPromise[S]: scala.concurrent.Promise[S] = new Promise.DefaultPromise()
// TODO refine answer and return types here from Any to type parameters
// then move this up in the hierarchy
/*
final def <<(value: T): Future[T] @cps[Future[Any]] = shift {
cont: (Future[T] => Future[Any]) =>
cont(complete(Right(value)))
}
final def <<(other: Future[T]): Future[T] @cps[Future[Any]] = shift {
cont: (Future[T] => Future[Any]) =>
val p = executor.promise[Any]
val thisPromise = this
thisPromise completeWith other
thisPromise onComplete { v =>
try {
p completeWith cont(thisPromise)
} catch {
case e => p complete resolver(e)
}
}
p.future
}
*/
// TODO finish this once we introduce something like dataflow streams
/*
final def <<(stream: PromiseStreamOut[T]): Future[T] @cps[Future[Any]] = shift { cont: (Future[T] => Future[Any]) =>
val fr = executor.promise[Any]
val f = stream.dequeue(this)
f.onComplete { _ =>
try {
fr completeWith cont(f)
} catch {
case e =>
fr failure e
}
}
fr
}
*/
def future: scala.concurrent.Future[T] = this
}
@@ -79,6 +32,8 @@ object Promise {
class DefaultPromise[T](implicit val executor: ExecutionContext) extends AbstractPromise with Promise[T] { self =>
updater.set(this, Nil) // Start at "No callbacks" //FIXME switch to Unsafe instead of ARFU
def newPromise[S]: scala.concurrent.Promise[S] = new Promise.DefaultPromise()
protected final def tryAwait(atMost: Duration): Boolean = {
@tailrec
def awaitUnsafe(waitTimeNanos: Long): Boolean = {
@@ -195,6 +150,8 @@ object Promise {
override def isCompleted(): Boolean = true
def newPromise[S]: scala.concurrent.Promise[S] = new Promise.DefaultPromise()
def tryComplete(value: Either[Throwable, T]): Boolean = false
def onComplete[U](func: Either[Throwable, T] => U): this.type = {

0 comments on commit 768748b

Please sign in to comment.