|
| 1 | +/* |
| 2 | + * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. |
| 3 | + */ |
| 4 | + |
| 5 | +package kotlinx.coroutines.experimental |
| 6 | + |
| 7 | +import kotlin.coroutines.experimental.* |
| 8 | + |
| 9 | +/** |
| 10 | + * Creates a new _supervisor_ job object in an active state. |
| 11 | + * Children of a supervisor job can fail independently of each other. |
| 12 | + * |
| 13 | + * A failure of a child does not cause the supervisor job to fail and does not affect its other children, |
| 14 | + * so a supervisor can implement a custom policy for handling failures of its children: |
| 15 | + * |
| 16 | + * * A failure of a child job that was created using [launch][CoroutineScope.launch] can be handled via [CoroutineExceptionHandler] in the context. |
| 17 | + * * A failure of a child job that was created using [async][CoroutineScope.async] can be handled via [Deferred.await] on the resulting deferred value. |
| 18 | + * |
| 19 | + * @param parent an optional parent job. If specified, this supervisor job becomes a child job of its parent and |
| 20 | + * is cancelled when its parent fails or is cancelled. |
| 21 | + */ |
| 22 | +@Suppress("FunctionName") |
| 23 | +public fun SupervisorJob(parent: Job? = null) : Job = SupervisorJobImpl(parent) |
| 24 | + |
| 25 | +/** |
| 26 | + * Creates new [CoroutineScope] with [SupervisorJob] and calls the specified suspend block with this scope. |
| 27 | + * The provided scope inherits its [coroutineContext][CoroutineScope.coroutineContext] from the outer scope, but overrides |
| 28 | + * context's [Job] with [SupervisorJob]. |
| 29 | + * |
| 30 | + * A failure of a child does not cause this scope to fail and does not affect its other children, |
| 31 | + * so a custom policy for handling failures of its children can be implemented. See [SupervisorJob] for details. |
| 32 | + */ |
| 33 | +public suspend fun <R> supervisorScope(block: suspend CoroutineScope.() -> R): R { |
| 34 | + // todo: optimize implementation to a single allocated object |
| 35 | + // todo: fix copy-and-paste with coroutineScope |
| 36 | + val owner = SupervisorCoroutine<R>(coroutineContext) |
| 37 | + owner.start(CoroutineStart.UNDISPATCHED, owner, block) |
| 38 | + owner.join() |
| 39 | + if (owner.isCancelled) { |
| 40 | + throw owner.getCancellationException().let { it.cause ?: it } |
| 41 | + } |
| 42 | + val state = owner.state |
| 43 | + if (state is CompletedExceptionally) { |
| 44 | + throw state.cause |
| 45 | + } |
| 46 | + @Suppress("UNCHECKED_CAST") |
| 47 | + return state as R |
| 48 | + |
| 49 | +} |
| 50 | + |
| 51 | +private class SupervisorJobImpl(parent: Job?) : JobSupport(true) { |
| 52 | + init { initParentJobInternal(parent) } |
| 53 | + override val onFailComplete get() = true |
| 54 | + override val handlesException: Boolean get() = false |
| 55 | + override fun childFailed(cause: Throwable): Boolean = false |
| 56 | +} |
| 57 | + |
| 58 | +private class SupervisorCoroutine<R>( |
| 59 | + parentContext: CoroutineContext |
| 60 | +) : AbstractCoroutine<R>(parentContext, true) { |
| 61 | + override fun childFailed(cause: Throwable): Boolean = false |
| 62 | +} |
0 commit comments