@@ -6,7 +6,6 @@ import scala.compiletime.uninitialized
6
6
import scala .util .{Try , Success , Failure }
7
7
import scala .annotation .unchecked .uncheckedVariance
8
8
import java .util .concurrent .CancellationException
9
- import runtime .suspend
10
9
11
10
/** A cancellable future that can suspend waiting for other asynchronous sources
12
11
*/
@@ -22,14 +21,8 @@ trait Future[+T] extends Async.Source[Try[T]], Cancellable:
22
21
*/
23
22
def force (): T
24
23
25
- /** Links the future as a child to the current async root.
26
- * This means the future will be cancelled if the async root
27
- * is canceled.
28
- */
29
- def linked (using async : Async ): this .type
30
-
31
24
/** Eventually stop computation of this future and fail with
32
- * a `Cancellation` exception. Also cancel all linked children.
25
+ * a `Cancellation` exception. Also cancel all children.
33
26
*/
34
27
def cancel (): Unit
35
28
@@ -40,8 +33,8 @@ trait Future[+T] extends Async.Source[Try[T]], Cancellable:
40
33
41
34
object Future :
42
35
43
- /** The core part of a future that is completed explicitly by calling its
44
- * `complete` method. There are two implementations
36
+ /** A future that is completed explicitly by calling its
37
+ * `complete` method. There are two public implementations
45
38
*
46
39
* - RunnableFuture: Completion is done by running a block of code
47
40
* - Promise.future: Completion is done by external request.
@@ -50,20 +43,27 @@ object Future:
50
43
51
44
@ volatile protected var hasCompleted : Boolean = false
52
45
private var result : Try [T ] = uninitialized // guaranteed to be set if hasCompleted = true
53
- private var waiting : mutable.Set [Try [T ] => Boolean ] = mutable.Set ()
54
- private var children : mutable.Set [Cancellable ] = mutable.Set ()
46
+ private val waiting : mutable.Set [Try [T ] => Boolean ] = mutable.Set ()
47
+ private val children : mutable.Set [Cancellable ] = mutable.Set ()
55
48
56
49
private def extract [T ](s : mutable.Set [T ]): List [T ] = synchronized :
57
50
val xs = s.toList
58
51
s.clear()
59
52
xs
60
53
54
+ // Async.Source method implementations
55
+
61
56
def poll (k : Async .Listener [Try [T ]]): Boolean =
62
57
hasCompleted && k(result)
63
58
64
59
def onComplete (k : Async .Listener [Try [T ]]): Unit = synchronized :
65
60
if ! poll(k) then waiting += k
66
61
62
+ def dropListener (k : Async .Listener [Try [T ]]): Unit =
63
+ waiting -= k
64
+
65
+ // Cancellable method implementations
66
+
67
67
def cancel (): Unit =
68
68
val othersToCancel = synchronized :
69
69
if hasCompleted then Nil
@@ -76,17 +76,12 @@ object Future:
76
76
def addChild (child : Cancellable ): Unit = synchronized :
77
77
if ! hasCompleted then children += this
78
78
79
- def linked (using async : Async ): this .type =
80
- if ! hasCompleted then async.root.addChild(this )
81
- this
82
-
83
- def dropListener (k : Async .Listener [Try [T ]]): Unit =
84
- waiting -= k
79
+ // Future method implementations
85
80
86
81
def value (using async : Async ): T =
87
82
async.await(this ).get
88
83
89
- def force (): T =
84
+ def force (): T = synchronized :
90
85
while ! hasCompleted do wait()
91
86
result.get
92
87
@@ -104,10 +99,14 @@ object Future:
104
99
this .result = result
105
100
hasCompleted = true
106
101
for listener <- extract(waiting) do listener(result)
107
- notifyAll()
102
+ synchronized :
103
+ notifyAll()
108
104
109
105
end CoreFuture
110
106
107
+ /** A future that is completed by evaluating `body` as a separate
108
+ * asynchronous operation in the given `scheduler`
109
+ */
111
110
private class RunnableFuture [+ T ](body : Async ?=> T )(using scheduler : Scheduler )
112
111
extends CoreFuture [T ]:
113
112
@@ -191,7 +190,7 @@ class Task[+T](val body: Async ?=> T):
191
190
192
191
end Task
193
192
194
- def Test (x : Future [Int ], xs : List [Future [Int ]])(using Scheduler ): Future [Int ] =
193
+ def add (x : Future [Int ], xs : List [Future [Int ]])(using Scheduler ): Future [Int ] =
195
194
val b = x.zip:
196
195
Future :
197
196
xs.headOption.toString
@@ -208,8 +207,8 @@ def Test(x: Future[Int], xs: List[Future[Int]])(using Scheduler): Future[Int] =
208
207
x.value * 2
209
208
x.value + xs.map(_.value).sum
210
209
211
- end Test
210
+ end add
212
211
213
212
def Main (x : Future [Int ], xs : List [Future [Int ]])(using Scheduler ): Int =
214
- Test (x, xs).force()
213
+ add (x, xs).force()
215
214
0 commit comments