|
1 | 1 | package concurrent
|
| 2 | +import java.util.concurrent.atomic.AtomicBoolean |
| 3 | +import scala.collection.mutable |
| 4 | +import runtime.suspend |
| 5 | +import scala.util.boundary |
| 6 | + |
| 7 | +/** The underlying configuration of an async block */ |
| 8 | +trait AsyncConfig: |
| 9 | + |
| 10 | + /** The cancellable async source underlying this async computation */ |
| 11 | + def root: Cancellable |
| 12 | + |
| 13 | + /** The scheduler for runnables defined in this async computation */ |
| 14 | + def scheduler: Scheduler |
| 15 | + |
| 16 | +object AsyncConfig: |
| 17 | + |
| 18 | + /** A toplevel async group with given scheduler and a synthetic root that |
| 19 | + * ignores cancellation requests |
| 20 | + */ |
| 21 | + given fromScheduler(using s: Scheduler): AsyncConfig with |
| 22 | + def root = Cancellable.empty |
| 23 | + def scheduler = s |
| 24 | + |
| 25 | +end AsyncConfig |
2 | 26 |
|
3 | 27 | /** A context that allows to suspend waiting for asynchronous data sources */
|
4 |
| -trait Async: |
| 28 | +trait Async extends AsyncConfig: |
5 | 29 |
|
6 | 30 | /** Wait for completion of async source `src` and return the result */
|
7 | 31 | def await[T](src: Async.Source[T]): T
|
8 | 32 |
|
9 |
| - /** Wait for completion of the first of the sources `src1`, `src2` |
10 |
| - * @return `Left(r1)` if `src1` completed first with `r1` |
11 |
| - * `Right(r2)` if `src2` completed first with `r2` |
12 |
| - */ |
13 |
| - def awaitEither[T1, T2](src1: Async.Source[T1], src2: Async.Source[T2]): Either[T1, T2] |
| 33 | +object Async: |
14 | 34 |
|
15 |
| - /** The cancellable runner underlying this async computation. */ |
16 |
| - def runner: Cancellable |
| 35 | + abstract class AsyncImpl(val root: Cancellable, val scheduler: Scheduler) |
| 36 | + (using boundary.Label[Unit]) extends Async: |
17 | 37 |
|
18 |
| - /** The scheduler for runnables defined in this async computation */ |
19 |
| - def scheduler: Scheduler |
| 38 | + protected def checkCancellation(): Unit |
20 | 39 |
|
21 |
| -object Async: |
| 40 | + private var result: T |
| 41 | + |
| 42 | + def await[T](src: Async.Source[T]): T = |
| 43 | + checkCancellation() |
| 44 | + var resultOpt: Option[T] = None |
| 45 | + if src.poll: x => |
| 46 | + result = x |
| 47 | + true |
| 48 | + then result |
| 49 | + else |
| 50 | + try suspend[T, Unit]: k => |
| 51 | + src.onComplete: x => |
| 52 | + scheduler.schedule: () => |
| 53 | + k.resume(x) |
| 54 | + true // signals to `src` that result `x` was consumed |
| 55 | + finally checkCancellation() |
| 56 | + |
| 57 | + end AsyncImpl |
22 | 58 |
|
23 | 59 | /** The currently executing Async context */
|
24 | 60 | inline def current(using async: Async): Async = async
|
25 | 61 |
|
26 |
| - /** An asynchronous data source. Sources can be persistent or ephemeral. |
27 |
| - * A persistent source will always return the same data to calls to `poll` |
28 |
| - * and pass the same data to calls of `handle`. An ephemeral source might pass new |
29 |
| - * data in every call. An example of a persistent source is `Future`. An |
30 |
| - * example of an ephemeral source is `Channel`. |
| 62 | + /** Await source result in currently executing Async context */ |
| 63 | + inline def await[T](src: Source[T])(using async: Async): T = async.await(src) |
| 64 | + |
| 65 | + /** A function `T => Boolean` whose lineage is recorded by its implementing |
| 66 | + * classes. The Listener function accepts values of type `T` and returns |
| 67 | + * `true` iff the value was consumed by an async block. |
31 | 68 | */
|
32 |
| - trait Source[+T]: |
33 |
| - thisSource => |
| 69 | + trait Listener[-T] extends Function[T, Boolean] |
34 | 70 |
|
35 |
| - /** Poll whether data is available |
36 |
| - * @return The data or None in an option. Depending on the nature of the |
37 |
| - * source, data might be returned only once in a poll. E.g. if |
38 |
| - * the source is a channel, a Some result might skip to the next |
| 71 | + /** A listener for values that are processed by the given source `src` and |
| 72 | + * that are demanded by the continuation listener `continue`. |
| 73 | + */ |
| 74 | + abstract case class ForwardingListener[T](src: Source[?], continue: Listener[?]) extends Listener[T] |
| 75 | + |
| 76 | + /** A listener for values that are processed directly in an async block. |
| 77 | + * Closures of type `T => Boolean` can be SAM converted to this type. |
| 78 | + */ |
| 79 | + abstract case class FinalListener[T]() extends Listener[T] |
| 80 | + |
| 81 | + /** A source that cannot be mapped, filtered, or raced. In other words, |
| 82 | + * an item coming from a direct source must be immediately consumed in |
| 83 | + * another async computation; no rejection of this item is possible. |
| 84 | + */ |
| 85 | + trait DirectSource[+T]: |
| 86 | + |
| 87 | + /** If data is available at present, pass it to function `k` |
| 88 | + * and return the result if this call. |
| 89 | + * `k` returns true iff the data was consumed in an async block. |
| 90 | + * Calls to `poll` are always synchronous. |
| 91 | + */ |
| 92 | + def poll(k: Listener[T]): Boolean |
| 93 | + |
| 94 | + /** Once data is available, pass it to function `k`. |
| 95 | + * `k` returns true iff the data was consumed in an async block. |
| 96 | + * Calls to `onComplete` are usually asynchronous, meaning that |
| 97 | + * the passed continuation `k` is a suspension. |
39 | 98 | */
|
40 |
| - def poll: Option[T] |
| 99 | + def onComplete(k: Listener[T]): Unit |
41 | 100 |
|
42 |
| - /** When data is available, pass it to function `k`. |
| 101 | + /** Signal that listener `k` is dead (i.e. will always return `false` from now on). |
| 102 | + * This permits original, (i.e. non-derived) sources like futures or channels |
| 103 | + * to drop the listener from their `waiting` sets. |
43 | 104 | */
|
44 |
| - def handleWith(k: T => Unit): Unit |
| 105 | + def dropListener(k: Listener[T]): Unit |
45 | 106 |
|
46 |
| - def map[U](f: T => U): Source[U] = new Source: |
47 |
| - def poll = thisSource.poll.map(f) |
48 |
| - def handleWith(k: U => Unit): Unit = thisSource.handleWith(f.andThen(k)) |
| 107 | + end DirectSource |
| 108 | + |
| 109 | + /** An asynchronous data source. Sources can be persistent or ephemeral. |
| 110 | + * A persistent source will always pass same data to calls of `poll and `onComplete`. |
| 111 | + * An ephememral source can pass new data in every call. |
| 112 | + * An example of a persistent source is `Future`. |
| 113 | + * An example of an ephemeral source is `Channel`. |
| 114 | + */ |
| 115 | + trait Source[+T] extends DirectSource[T]: |
| 116 | + |
| 117 | + /** Pass on data transformed by `f` */ |
| 118 | + def map[U](f: T => U): Source[U] = |
| 119 | + new DerivedSource[T, U](this): |
| 120 | + def listen(x: T, k: Listener[U]) = k(f(x)) |
| 121 | + |
| 122 | + /** Pass on only data matching the predicate `p` */ |
| 123 | + def filter(p: T => Boolean): Source[T] = |
| 124 | + new DerivedSource[T, T](this): |
| 125 | + def listen(x: T, k: Listener[T]) = p(x) && k(x) |
49 | 126 |
|
50 | 127 | end Source
|
51 | 128 |
|
| 129 | + /** As source that transforms an original source in some way */ |
| 130 | + |
| 131 | + abstract class DerivedSource[T, U](src: Source[T]) extends Source[U]: |
| 132 | + |
| 133 | + /** Handle a value `x` passed to the original source by possibly |
| 134 | + * invokiong the continuation for this source. |
| 135 | + */ |
| 136 | + protected def listen(x: T, k: Listener[U]): Boolean |
| 137 | + |
| 138 | + private def transform(k: Listener[U]): Listener[T] = |
| 139 | + new ForwardingListener[T](this, k): |
| 140 | + def apply(x: T): Boolean = listen(x, k) |
| 141 | + |
| 142 | + def poll(k: Listener[U]): Boolean = |
| 143 | + src.poll(transform(k)) |
| 144 | + def onComplete(k: Listener[U]): Unit = |
| 145 | + src.onComplete(transform(k)) |
| 146 | + def dropListener(k: Listener[U]): Unit = |
| 147 | + src.dropListener(transform(k)) |
| 148 | + end DerivedSource |
| 149 | + |
| 150 | + /** Pass first result from any of `sources` to the continuation */ |
| 151 | + def race[T](sources: Source[T]*): Source[T] = new Source: |
| 152 | + |
| 153 | + def poll(k: Listener[T]): Boolean = |
| 154 | + val it = sources.iterator |
| 155 | + var found = false |
| 156 | + while it.hasNext && !found do |
| 157 | + it.next.poll: x => |
| 158 | + found = k(x) |
| 159 | + found |
| 160 | + found |
| 161 | + |
| 162 | + def onComplete(k: Listener[T]): Unit = |
| 163 | + val listener = new ForwardingListener[T](this, k): |
| 164 | + var foundBefore = false |
| 165 | + def continueIfFirst(x: T): Boolean = synchronized: |
| 166 | + if foundBefore then false else { foundBefore = k(x); foundBefore } |
| 167 | + def apply(x: T): Boolean = |
| 168 | + val found = continueIfFirst(x) |
| 169 | + if found then sources.foreach(_.dropListener(this)) |
| 170 | + found |
| 171 | + sources.foreach(_.onComplete(listener)) |
| 172 | + |
| 173 | + def dropListener(k: Listener[T]): Unit = |
| 174 | + val listener = new ForwardingListener[T](this, k): |
| 175 | + def apply(x: T): Boolean = ??? |
| 176 | + // not to be called, we need the listener only for its |
| 177 | + // hashcode and equality test. |
| 178 | + sources.foreach(_.dropListener(listener)) |
| 179 | + |
| 180 | + end race |
| 181 | + |
| 182 | + /** If left (respectively, right) source succeeds with `x`, pass `Left(x)`, |
| 183 | + * (respectively, Right(x)) on to the continuation. |
| 184 | + */ |
| 185 | + def either[T, U](src1: Source[T], src2: Source[U]): Source[Either[T, U]] = |
| 186 | + race[Either[T, U]](src1.map(Left(_)), src2.map(Right(_))) |
| 187 | + |
52 | 188 | end Async
|
53 | 189 |
|
0 commit comments