@@ -14,15 +14,14 @@ package scala.concurrent
14
14
15
15
import java .util .concurrent .atomic .AtomicReference
16
16
import java .util .concurrent .locks .LockSupport
17
-
18
- import scala .util .control .{NonFatal , NoStackTrace }
17
+ import scala .util .control .{NoStackTrace , NonFatal }
19
18
import scala .util .{Failure , Success , Try }
20
19
import scala .concurrent .duration ._
21
20
import scala .collection .BuildFrom
22
- import scala .collection .mutable .{Builder , ArrayBuffer }
21
+ import scala .collection .mutable .{ArrayBuffer , Builder }
23
22
import scala .reflect .ClassTag
24
-
25
23
import scala .concurrent .ExecutionContext .parasitic
24
+ import scala .concurrent .impl .Promise .DefaultPromise
26
25
27
26
/** A `Future` represents a value which may or may not be currently available,
28
27
* but will be available at some point, or an exception if that value could not be made available.
@@ -126,12 +125,6 @@ trait Future[+T] extends Awaitable[T] {
126
125
*/
127
126
def onComplete [U ](f : Try [T ] => U )(implicit executor : ExecutionContext ): Unit
128
127
129
- /** The same as [[onComplete ]], but additionally returns a function which can be
130
- * invoked to unregister the callback function. Removing a callback from a long-lived
131
- * future can enable garbage collection of objects referenced by the closure.
132
- */
133
- private [concurrent] def onCompleteWithUnregister [U ](f : Try [T ] => U )(implicit executor : ExecutionContext ): () => Unit
134
-
135
128
/* Miscellaneous */
136
129
137
130
/** Returns whether the future had already been completed with
@@ -621,7 +614,6 @@ object Future {
621
614
}
622
615
623
616
override final def onComplete [U ](f : Try [Nothing ] => U )(implicit executor : ExecutionContext ): Unit = ()
624
- override private [concurrent] final def onCompleteWithUnregister [U ](f : Try [Nothing ] => U )(implicit executor : ExecutionContext ): () => Unit = () => ()
625
617
override final def isCompleted : Boolean = false
626
618
override final def value : Option [Try [Nothing ]] = None
627
619
override final def failed : Future [Throwable ] = this
@@ -751,10 +743,13 @@ object Future {
751
743
while (i.hasNext && ! completed) {
752
744
val deregs = firstCompleteHandler.get
753
745
if (deregs == null ) completed = true
754
- else {
755
- val d = i.next().onCompleteWithUnregister(firstCompleteHandler)
756
- if (! firstCompleteHandler.compareAndSet(deregs, d :: deregs))
757
- d.apply()
746
+ else i.next() match {
747
+ case dp : DefaultPromise [T @ unchecked] =>
748
+ val d = dp.onCompleteWithUnregister(firstCompleteHandler)
749
+ if (! firstCompleteHandler.compareAndSet(deregs, d :: deregs))
750
+ d.apply()
751
+ case f =>
752
+ f.onComplete(firstCompleteHandler)
758
753
}
759
754
}
760
755
p.future
0 commit comments