|
| 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 or cancellation 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 | + * If [parent] job is specified, then this supervisor job becomes a child job of its parent and is cancelled when its |
| 20 | + * parent fails or is cancelled. All this supervisor's children are cancelled in this case, too. The invocation of |
| 21 | + * of [cancel][Job.cancel] with exception (other than [CancellationException]) on this supervisor job also cancels parent. |
| 22 | + * |
| 23 | + * @param parent an optional parent job. |
| 24 | + */ |
| 25 | +@Suppress("FunctionName") |
| 26 | +public fun SupervisorJob(parent: Job? = null) : Job = SupervisorJobImpl(parent) |
| 27 | + |
| 28 | +/** |
| 29 | + * Creates new [CoroutineScope] with [SupervisorJob] and calls the specified suspend block with this scope. |
| 30 | + * The provided scope inherits its [coroutineContext][CoroutineScope.coroutineContext] from the outer scope, but overrides |
| 31 | + * context's [Job] with [SupervisorJob]. |
| 32 | + * |
| 33 | + * A failure of a child does not cause this scope to fail and does not affect its other children, |
| 34 | + * so a custom policy for handling failures of its children can be implemented. See [SupervisorJob] for details. |
| 35 | + */ |
| 36 | +public suspend fun <R> supervisorScope(block: suspend CoroutineScope.() -> R): R { |
| 37 | + // todo: optimize implementation to a single allocated object |
| 38 | + // todo: fix copy-and-paste with coroutineScope |
| 39 | + val owner = SupervisorCoroutine<R>(coroutineContext) |
| 40 | + owner.start(CoroutineStart.UNDISPATCHED, owner, block) |
| 41 | + owner.join() |
| 42 | + if (owner.isCancelled) { |
| 43 | + throw owner.getCancellationException().let { it.cause ?: it } |
| 44 | + } |
| 45 | + val state = owner.state |
| 46 | + if (state is CompletedExceptionally) { |
| 47 | + throw state.cause |
| 48 | + } |
| 49 | + @Suppress("UNCHECKED_CAST") |
| 50 | + return state as R |
| 51 | + |
| 52 | +} |
| 53 | + |
| 54 | +private class SupervisorJobImpl(parent: Job?) : JobSupport(true) { |
| 55 | + init { initParentJobInternal(parent) } |
| 56 | + override val cancelsParent: Boolean get() = true |
| 57 | + override val onCancelComplete get() = true |
| 58 | + override val handlesException: Boolean get() = false |
| 59 | + override fun childCancelled(cause: Throwable): Boolean = false |
| 60 | +} |
| 61 | + |
| 62 | +private class SupervisorCoroutine<R>( |
| 63 | + parentContext: CoroutineContext |
| 64 | +) : AbstractCoroutine<R>(parentContext, true) { |
| 65 | + override val cancelsParent: Boolean get() = true |
| 66 | + override fun childCancelled(cause: Throwable): Boolean = false |
| 67 | +} |
0 commit comments