blob: 8178d8733e6d2bd7a56471af8de5299ce43441f0 [file] [log] [blame]
Roman Elizarovfadf8c82017-03-02 23:36:06 +03001/*
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
Roman Elizarovee88fbe2017-02-22 09:53:49 +030017package kotlinx.coroutines.experimental.intrinsics
18
Roman Elizarov2adf8bc2018-01-24 20:09:57 +030019import kotlinx.coroutines.experimental.*
20import kotlin.coroutines.experimental.*
21import kotlin.coroutines.experimental.intrinsics.*
Roman Elizarovee88fbe2017-02-22 09:53:49 +030022
23/**
Roman Elizarov23fb7282018-01-24 23:09:42 +030024 * Use this function to restart coroutine directly from inside of [suspendCoroutine] in the same context.
Roman Elizarovee88fbe2017-02-22 09:53:49 +030025 */
26@Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN", "UNCHECKED_CAST")
Roman Elizarov23fb7282018-01-24 23:09:42 +030027public fun <T> (suspend () -> T).startCoroutineUndispatched(completion: Continuation<T>) {
Roman Elizarovee88fbe2017-02-22 09:53:49 +030028 val value = try {
Roman Elizarov4638d792017-03-14 19:39:26 +030029 startCoroutineUninterceptedOrReturn(completion)
Roman Elizarovee88fbe2017-02-22 09:53:49 +030030 } catch (e: Throwable) {
31 completion.resumeWithException(e)
32 return
33 }
34 if (value !== COROUTINE_SUSPENDED)
Roman Elizarova74eb5f2017-05-11 20:15:18 +030035 completion.resume(value as T)
Roman Elizarovee88fbe2017-02-22 09:53:49 +030036}
37
38/**
Roman Elizarov23fb7282018-01-24 23:09:42 +030039 * Use this function to restart coroutine directly from inside of [suspendCoroutine] in the same context.
Roman Elizarovee88fbe2017-02-22 09:53:49 +030040 */
41@Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN", "UNCHECKED_CAST")
Roman Elizarov23fb7282018-01-24 23:09:42 +030042public fun <R, T> (suspend (R) -> T).startCoroutineUndispatched(receiver: R, completion: Continuation<T>) {
Roman Elizarovee88fbe2017-02-22 09:53:49 +030043 val value = try {
Roman Elizarova74eb5f2017-05-11 20:15:18 +030044 startCoroutineUninterceptedOrReturn(receiver, completion)
Roman Elizarovee88fbe2017-02-22 09:53:49 +030045 } catch (e: Throwable) {
46 completion.resumeWithException(e)
47 return
48 }
49 if (value !== COROUTINE_SUSPENDED)
Roman Elizarova74eb5f2017-05-11 20:15:18 +030050 completion.resume(value as T)
Roman Elizarovee88fbe2017-02-22 09:53:49 +030051}
Roman Elizarov2adf8bc2018-01-24 20:09:57 +030052
53/**
Roman Elizarov23fb7282018-01-24 23:09:42 +030054 * Starts this coroutine with the given code [block] in the same context and returns result when it
55 * completes without suspnesion.
56 * This function shall be invoked at most once on this coroutine.
Roman Elizarov2adf8bc2018-01-24 20:09:57 +030057 *
58 * First, this function initializes parent job from the `parentContext` of this coroutine that was passed to it
59 * during construction. Second, it starts the coroutine using [startCoroutineUninterceptedOrReturn].
Roman Elizarov2adf8bc2018-01-24 20:09:57 +030060 */
61public fun <T> AbstractCoroutine<T>.startUndispatchedOrReturn(block: suspend () -> T): Any? {
62 initParentJob()
63 return undispatchedResult { block.startCoroutineUninterceptedOrReturn(this) }
64}
65
66/**
Roman Elizarov23fb7282018-01-24 23:09:42 +030067 * Starts this coroutine with the given code [block] in the same context and returns result when it
68 * completes without suspnesion.
69 * This function shall be invoked at most once on this coroutine.
Roman Elizarov2adf8bc2018-01-24 20:09:57 +030070 *
71 * First, this function initializes parent job from the `parentContext` of this coroutine that was passed to it
72 * during construction. Second, it starts the coroutine using [startCoroutineUninterceptedOrReturn].
Roman Elizarov2adf8bc2018-01-24 20:09:57 +030073 */
74public fun <T, R> AbstractCoroutine<T>.startUndispatchedOrReturn(receiver: R, block: suspend R.() -> T): Any? {
75 initParentJob()
76 return undispatchedResult { block.startCoroutineUninterceptedOrReturn(receiver, this) }
77}
78
79private inline fun <T> AbstractCoroutine<T>.undispatchedResult(startBlock: () -> Any?): Any? {
80 val result = try {
81 startBlock()
82 } catch (e: Throwable) {
83 CompletedExceptionally(e)
84 }
85 return when {
Konrad KamiƄskiea4cd452018-02-01 17:18:14 +010086 result === COROUTINE_SUSPENDED -> COROUTINE_SUSPENDED
Roman Elizarov2adf8bc2018-01-24 20:09:57 +030087 makeCompletingOnce(result, MODE_IGNORE) -> {
Vsevolod Tolstopyatovc1092d52018-04-12 20:22:25 +030088 if (result is CompletedExceptionally) throw result.cause else result
Roman Elizarov2adf8bc2018-01-24 20:09:57 +030089 }
90 else -> COROUTINE_SUSPENDED
91 }
92}