Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 1 | package kotlinx.coroutines.experimental |
| 2 | |
| 3 | import kotlin.coroutines.Continuation |
| 4 | import kotlin.coroutines.intrinsics.SUSPENDED_MARKER |
| 5 | import kotlin.coroutines.intrinsics.suspendCoroutineOrReturn |
| 6 | import kotlin.coroutines.suspendCoroutine |
| 7 | |
| 8 | // --------------- cancellable continuations --------------- |
| 9 | |
| 10 | /** |
| 11 | * Cancellable continuation. Its job is completed when it is resumed or cancelled. |
| 12 | * When [cancel] function is explicitly invoked, this continuation resumes with [CancellationException]. |
| 13 | * If the cancel reason was not a [CancellationException], then the original exception is added as cause of the |
| 14 | * [CancellationException] that this continuation resumes with. |
| 15 | */ |
Roman Elizarov | 834af46 | 2017-01-23 20:50:29 +0300 | [diff] [blame^] | 16 | public interface CancellableContinuation<in T> : Continuation<T>, Job { |
| 17 | /** |
| 18 | * Returns `true` if this continuation was cancelled. It implies that [isActive] is `false`. |
| 19 | */ |
| 20 | val isCancelled: Boolean |
| 21 | } |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 22 | |
| 23 | /** |
| 24 | * Suspend coroutine similar to [suspendCoroutine], but provide an implementation of [CancellableContinuation] to |
Roman Elizarov | 58a7add | 2017-01-20 12:19:52 +0300 | [diff] [blame] | 25 | * the [block]. This function throws [CancellationException] if the coroutine is cancelled while suspended. |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 26 | */ |
| 27 | public inline suspend fun <T> suspendCancellableCoroutine(crossinline block: (CancellableContinuation<T>) -> Unit): T = |
Roman Elizarov | 58a7add | 2017-01-20 12:19:52 +0300 | [diff] [blame] | 28 | suspendCoroutineOrReturn { cont -> |
| 29 | val safe = SafeCancellableContinuation(cont, getParentJobOrAbort(cont)) |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 30 | block(safe) |
| 31 | safe.getResult() |
| 32 | } |
| 33 | |
| 34 | // --------------- implementation details --------------- |
| 35 | |
| 36 | @PublishedApi |
Roman Elizarov | 58a7add | 2017-01-20 12:19:52 +0300 | [diff] [blame] | 37 | internal fun getParentJobOrAbort(cont: Continuation<*>): Job? { |
| 38 | val job = cont.context[Job] |
| 39 | // fast path when parent job is already complete (we don't even construct SafeCancellableContinuation object) |
| 40 | job?.isActive?.let { if (!it) throw CancellationException() } |
| 41 | return job |
| 42 | } |
| 43 | |
| 44 | @PublishedApi |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 45 | internal class SafeCancellableContinuation<in T>( |
Roman Elizarov | 58a7add | 2017-01-20 12:19:52 +0300 | [diff] [blame] | 46 | private val delegate: Continuation<T>, |
| 47 | parentJob: Job? |
Roman Elizarov | 53a0a40 | 2017-01-19 20:21:57 +0300 | [diff] [blame] | 48 | ) : AbstractCoroutine<T>(delegate.context), CancellableContinuation<T> { |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 49 | // only updated from the thread that invoked suspendCancellableCoroutine |
| 50 | private var suspendedThread: Thread? = Thread.currentThread() |
| 51 | |
Roman Elizarov | 58a7add | 2017-01-20 12:19:52 +0300 | [diff] [blame] | 52 | init { initParentJob(parentJob) } |
| 53 | |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 54 | fun getResult(): Any? { |
| 55 | if (suspendedThread != null) { |
| 56 | suspendedThread = null |
| 57 | return SUSPENDED_MARKER |
| 58 | } |
| 59 | val state = getState() |
| 60 | if (state is CompletedExceptionally) throw state.exception |
| 61 | return state |
| 62 | } |
| 63 | |
Roman Elizarov | 834af46 | 2017-01-23 20:50:29 +0300 | [diff] [blame^] | 64 | override val isCancelled: Boolean |
| 65 | get() = getState() is Cancelled |
| 66 | |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 67 | @Suppress("UNCHECKED_CAST") |
Roman Elizarov | 53a0a40 | 2017-01-19 20:21:57 +0300 | [diff] [blame] | 68 | override fun afterCompletion(state: Any?) { |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 69 | if (suspendedThread === Thread.currentThread()) { |
| 70 | // cancelled during suspendCancellableCoroutine in its thread |
| 71 | suspendedThread = null |
| 72 | } else { |
| 73 | // cancelled later or in other thread |
| 74 | if (state is CompletedExceptionally) |
| 75 | delegate.resumeWithException(state.exception) |
| 76 | else |
| 77 | delegate.resume(state as T) |
| 78 | } |
| 79 | } |
| 80 | } |