@@ -9,6 +9,48 @@ import kotlin.coroutines.intrinsics.suspendCoroutineUninterceptedOrReturn
9
9
import kotlin.coroutines.intrinsics.intercepted
10
10
import kotlin.coroutines.intrinsics.COROUTINE_SUSPENDED
11
11
12
+ /* *
13
+ * Tuples [fa], [fb] in parallel on [ComputationPool].
14
+ * Cancelling this operation cancels both operations running in parallel.
15
+ *
16
+ * @see parTupledN for the same function that can race on any [CoroutineContext].
17
+ */
18
+ suspend fun <A , B > parTupledN (fa : suspend () -> A , fb : suspend () -> B ): Pair <A , B > =
19
+ parTupledN(ComputationPool , fa, fb)
20
+
21
+ /* *
22
+ * Tuples [fa], [fb], [fc] in parallel on [ComputationPool].
23
+ * Cancelling this operation cancels both tasks running in parallel.
24
+ *
25
+ * @see parTupledN for the same function that can race on any [CoroutineContext].
26
+ */
27
+ suspend fun <A , B , C > parTupledN (fa : suspend () -> A , fb : suspend () -> B , fc : suspend () -> C ): Triple <A , B , C > =
28
+ parTupledN(ComputationPool , fa, fb, fc)
29
+
30
+ /* *
31
+ * Tuples [fa], [fb] on the provided [CoroutineContext].
32
+ * Cancelling this operation cancels both tasks running in parallel.
33
+ *
34
+ * **WARNING** it runs in parallel depending on the capabilities of the provided [CoroutineContext].
35
+ * We ensure they start in sequence so it's guaranteed to finish on a single threaded context.
36
+ *
37
+ * @see parTupledN for a function that ensures it runs in parallel on the [ComputationPool].
38
+ */
39
+ suspend fun <A , B > parTupledN (ctx : CoroutineContext , fa : suspend () -> A , fb : suspend () -> B ): Pair <A , B > =
40
+ parMapN(ctx, fa, fb, ::Pair )
41
+
42
+ /* *
43
+ * Tuples [fa], [fb] & [fc] on the provided [CoroutineContext].
44
+ * Cancelling this operation cancels both tasks running in parallel.
45
+ *
46
+ * **WARNING** it runs in parallel depending on the capabilities of the provided [CoroutineContext].
47
+ * We ensure they start in sequence so it's guaranteed to finish on a single threaded context.
48
+ *
49
+ * @see parTupledN for a function that ensures it runs in parallel on the [ComputationPool].
50
+ */
51
+ suspend fun <A , B , C > parTupledN (ctx : CoroutineContext , fa : suspend () -> A , fb : suspend () -> B , fc : suspend () -> C ): Triple <A , B , C > =
52
+ parMapN(ctx, fa, fb, fc, ::Triple )
53
+
12
54
/* *
13
55
* Parallel maps [fa], [fb] in parallel on [ComputationPool].
14
56
* Cancelling this operation cancels both operations running in parallel.
@@ -21,7 +63,7 @@ import kotlin.coroutines.intrinsics.COROUTINE_SUSPENDED
21
63
* val result = parMapN(
22
64
* { "First one is on ${Thread.currentThread().name}" },
23
65
* { "Second one is on ${Thread.currentThread().name}" }
24
- * ) { ( a, b) ->
66
+ * ) { a, b ->
25
67
* "$a\n$b"
26
68
* }
27
69
* //sampleEnd
@@ -36,8 +78,8 @@ import kotlin.coroutines.intrinsics.COROUTINE_SUSPENDED
36
78
*
37
79
* @see parMapN for the same function that can race on any [CoroutineContext].
38
80
*/
39
- suspend fun <A , B , C > parMapN (fa : suspend () -> A , fb : suspend () -> B , f : (Pair < A , B > ) -> C ): C =
40
- f(parTupledN( ComputationPool , fa, fb) )
81
+ suspend fun <A , B , C > parMapN (fa : suspend () -> A , fb : suspend () -> B , f : (A , B ) -> C ): C =
82
+ parMapN( ComputationPool , fa, fb, f )
41
83
42
84
/* *
43
85
* Parallel maps [fa], [fb], [fc] in parallel on [ComputationPool].
@@ -49,9 +91,8 @@ suspend fun <A, B, C, D> parMapN(
49
91
fa : suspend () -> A ,
50
92
fb : suspend () -> B ,
51
93
fc : suspend () -> C ,
52
- f : (Triple <A , B , C >) -> D
53
- ): D =
54
- f(parTupledN(ComputationPool , fa, fb, fc))
94
+ f : (A , B , C ) -> D
95
+ ): D = parMapN(ComputationPool , fa, fb, fc, f)
55
96
56
97
/* *
57
98
* Parallel maps [fa], [fb] on the provided [CoroutineContext].
@@ -63,66 +104,13 @@ suspend fun <A, B, C, D> parMapN(
63
104
*
64
105
* @see parMapN for a function that ensures it runs in parallel on the [ComputationPool].
65
106
*/
107
+ @Suppress(" UNCHECKED_CAST" )
66
108
suspend fun <A , B , C > parMapN (
67
109
ctx : CoroutineContext ,
68
110
fa : suspend () -> A ,
69
111
fb : suspend () -> B ,
70
- f : (Pair < A , B > ) -> C
112
+ f : (A , B ) -> C
71
113
): C =
72
- f(parTupledN(ctx, fa, fb))
73
-
74
- /* *
75
- * Parallel maps [fa], [fb], [fc] on the provided [CoroutineContext].
76
- * Cancelling this operation cancels both tasks running in parallel.
77
- *
78
- * **WARNING** this function forks [fa], [fb] & [fc] but if it runs in parallel depends
79
- * on the capabilities of the provided [CoroutineContext].
80
- * We ensure they start in sequence so it's guaranteed to finish on a single threaded context.
81
- *
82
- * @see parMapN for a function that ensures it runs in parallel on the [ComputationPool].
83
- */
84
- suspend fun <A , B , C , D > parMapN (
85
- ctx : CoroutineContext ,
86
- fa : suspend () -> A ,
87
- fb : suspend () -> B ,
88
- fc : suspend () -> C ,
89
- f : (Triple <A , B , C >) -> D
90
- ): D =
91
- f(parTupledN(ctx, fa, fb, fc))
92
-
93
- /* *
94
- * Tuples [fa], [fb] in parallel on [ComputationPool].
95
- * Cancelling this operation cancels both operations running in parallel.
96
- *
97
- * @see parTupledN for the same function that can race on any [CoroutineContext].
98
- */
99
- suspend fun <A , B > parTupledN (fa : suspend () -> A , fb : suspend () -> B ): Pair <A , B > =
100
- parTupledN(ComputationPool , fa, fb)
101
-
102
- /* *
103
- * Tuples [fa], [fb], [fc] in parallel on [ComputationPool].
104
- * Cancelling this operation cancels both tasks running in parallel.
105
- *
106
- * @see parTupledN for the same function that can race on any [CoroutineContext].
107
- */
108
- suspend fun <A , B , C > parTupledN (fa : suspend () -> A , fb : suspend () -> B , fc : suspend () -> C ): Triple <A , B , C > =
109
- parTupledN(ComputationPool , fa, fb, fc)
110
-
111
- /* *
112
- * Tuples [fa], [fb] on the provided [CoroutineContext].
113
- * Cancelling this operation cancels both tasks running in parallel.
114
- *
115
- * **WARNING** it runs in parallel depending on the capabilities of the provided [CoroutineContext].
116
- * We ensure they start in sequence so it's guaranteed to finish on a single threaded context.
117
- *
118
- * @see parTupledN for a function that ensures it runs in parallel on the [ComputationPool].
119
- */
120
- @Suppress(" UNCHECKED_CAST" )
121
- suspend fun <A , B > parTupledN (
122
- ctx : CoroutineContext ,
123
- fa : suspend () -> A ,
124
- fb : suspend () -> B
125
- ): Pair <A , B > =
126
114
suspendCoroutineUninterceptedOrReturn { cont ->
127
115
val conn = cont.context.connection()
128
116
val cont = cont.intercepted()
@@ -140,9 +128,9 @@ suspend fun <A, B> parTupledN(
140
128
conn.pop()
141
129
cb(
142
130
try {
143
- Result .success(Pair (a, b))
131
+ Result .success(f (a, b))
144
132
} catch (e: Throwable ) {
145
- Result .failure<Pair < A , B > >(e.nonFatalOrThrow())
133
+ Result .failure<C >(e.nonFatalOrThrow())
146
134
}
147
135
)
148
136
}
@@ -151,7 +139,7 @@ suspend fun <A, B> parTupledN(
151
139
is Throwable -> Unit // Do nothing we already finished
152
140
else -> other.cancelToken().cancel.startCoroutine(Continuation (EmptyCoroutineContext ) { r ->
153
141
conn.pop()
154
- cb(Result .failure< Pair < A , B >> (r.fold({ e }, { e2 -> Platform .composeErrors(e, e2) })))
142
+ cb(Result .failure(r.fold({ e }, { e2 -> Platform .composeErrors(e, e2) })))
155
143
})
156
144
}
157
145
@@ -185,20 +173,22 @@ suspend fun <A, B> parTupledN(
185
173
}
186
174
187
175
/* *
188
- * Tuples [fa], [fb] & [fc] on the provided [CoroutineContext].
176
+ * Parallel maps [fa], [fb], [fc] on the provided [CoroutineContext].
189
177
* Cancelling this operation cancels both tasks running in parallel.
190
178
*
191
- * **WARNING** it runs in parallel depending on the capabilities of the provided [CoroutineContext].
179
+ * **WARNING** this function forks [fa], [fb] & [fc] but if it runs in parallel depends
180
+ * on the capabilities of the provided [CoroutineContext].
192
181
* We ensure they start in sequence so it's guaranteed to finish on a single threaded context.
193
182
*
194
- * @see parTupledN for a function that ensures it runs in parallel on the [ComputationPool].
183
+ * @see parMapN for a function that ensures it runs in parallel on the [ComputationPool].
195
184
*/
196
- suspend fun <A , B , C > parTupledN (
185
+ suspend fun <A , B , C , D > parMapN (
197
186
ctx : CoroutineContext ,
198
187
fa : suspend () -> A ,
199
188
fb : suspend () -> B ,
200
- fc : suspend () -> C
201
- ): Triple <A , B , C > =
189
+ fc : suspend () -> C ,
190
+ f : (A , B , C ) -> D
191
+ ): D =
202
192
suspendCoroutineUninterceptedOrReturn { cont ->
203
193
val conn = cont.context.connection()
204
194
val cont = cont.intercepted()
@@ -217,7 +207,7 @@ suspend fun <A, B, C> parTupledN(
217
207
218
208
fun complete (a : A , b : B , c : C ) {
219
209
conn.pop()
220
- cb(Result .success(Triple (a, b, c)))
210
+ cb(Result .success(f (a, b, c)))
221
211
}
222
212
223
213
fun tryComplete (result : Triple <A ?, B ?, C ?>? ): Unit {
0 commit comments