|
| 1 | +/* |
| 2 | + * Copyright 2016-2017 JetBrains s.r.o. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package kotlinx.coroutines.experimental |
| 18 | + |
| 19 | +import kotlinx.atomicfu.atomic |
| 20 | + |
| 21 | +/** |
| 22 | + * Awaits for completion of given deferred values without blocking a thread and resumes normally with the list of values |
| 23 | + * when all deferred computations are complete or resumes with the first thrown exception if any of computations |
| 24 | + * complete exceptionally including cancellation. |
| 25 | + * |
| 26 | + * This function is **not** equivalent to `deferreds.map { it.await() }` which fails only when when it sequentially |
| 27 | + * gets to wait the failing deferred, while this `awaitAll` fails immediately as soon as any of the deferreds fail. |
| 28 | + * |
| 29 | + * This suspending function is cancellable. |
| 30 | + * If the [Job] of the current coroutine is cancelled or completed while this suspending function is waiting, |
| 31 | + * this function immediately resumes with [CancellationException]. |
| 32 | + */ |
| 33 | +public suspend fun <T> awaitAll(vararg deferreds: Deferred<T>): List<T> = |
| 34 | + if (deferreds.isEmpty()) emptyList() else AwaitAll(deferreds).await() |
| 35 | + |
| 36 | +/** |
| 37 | + * Awaits for completion of given deferred values without blocking a thread and resumes normally with the list of values |
| 38 | + * when all deferred computations are complete or resumes with the first thrown exception if any of computations |
| 39 | + * complete exceptionally including cancellation. |
| 40 | + * |
| 41 | + * This function is **not** equivalent to `this.map { it.await() }` which fails only when when it sequentially |
| 42 | + * gets to wait the failing deferred, while this `awaitAll` fails immediately as soon as any of the deferreds fail. |
| 43 | + * |
| 44 | + * This suspending function is cancellable. |
| 45 | + * If the [Job] of the current coroutine is cancelled or completed while this suspending function is waiting, |
| 46 | + * this function immediately resumes with [CancellationException]. |
| 47 | + */ |
| 48 | +public suspend fun <T> Collection<Deferred<T>>.awaitAll(): List<T> = |
| 49 | + if (isEmpty()) emptyList() else AwaitAll(toTypedArray()).await() |
| 50 | + |
| 51 | +/** |
| 52 | + * Suspends current coroutine until all given jobs are complete. |
| 53 | + * This method is semantically equivalent to joining all given jobs one by one with `jobs.forEach { it.join() }`. |
| 54 | + * |
| 55 | + * This suspending function is cancellable. If the [Job] of the current coroutine is cancelled or completed while this suspending function is waiting, |
| 56 | + * this function immediately resumes with [CancellationException]. |
| 57 | + */ |
| 58 | +public suspend fun joinAll(vararg jobs: Job): Unit = jobs.forEach { it.join() } |
| 59 | + |
| 60 | +/** |
| 61 | + * Suspends current coroutine until all given jobs are complete. |
| 62 | + * This method is semantically equivalent to joining all given jobs one by one with `forEach { it.join() }`. |
| 63 | + * |
| 64 | + * This suspending function is cancellable. If the [Job] of the current coroutine is cancelled or completed while this suspending function is waiting, |
| 65 | + * this function immediately resumes with [CancellationException]. |
| 66 | + */ |
| 67 | +public suspend fun Collection<Job>.joinAll(): Unit = forEach { it.join() } |
| 68 | + |
| 69 | +private class AwaitAll<T>(private val deferreds: Array<out Deferred<T>>) { |
| 70 | + private val notCompletedCount = atomic(deferreds.size) |
| 71 | + |
| 72 | + suspend fun await(): List<T> = suspendCancellableCoroutine { cont -> |
| 73 | + val handlers = deferreds.map { |
| 74 | + it.start() // To properly await lazily started deferreds |
| 75 | + it.invokeOnCompletion(AwaitAllNode(cont, it).asHandler) |
| 76 | + } |
| 77 | + cont.invokeOnCancellation { |
| 78 | + handlers.forEach { it.dispose() } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + inner class AwaitAllNode(private val continuation: CancellableContinuation<List<T>>, job: Job) : JobNode<Job>(job) { |
| 83 | + override fun invoke(cause: Throwable?) { |
| 84 | + if (cause != null) { |
| 85 | + val token = continuation.tryResumeWithException(cause) |
| 86 | + if (token != null) { |
| 87 | + continuation.completeResume(token) |
| 88 | + } |
| 89 | + } else if (notCompletedCount.decrementAndGet() == 0) { |
| 90 | + continuation.resume(deferreds.map { it.getCompleted() }) |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | +} |
0 commit comments