Roman Elizarov | fadf8c8 | 2017-03-02 23:36:06 +0300 | [diff] [blame] | 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 | |
Roman Elizarov | ee88fbe | 2017-02-22 09:53:49 +0300 | [diff] [blame] | 17 | package kotlinx.coroutines.experimental.intrinsics |
| 18 | |
Roman Elizarov | 2adf8bc | 2018-01-24 20:09:57 +0300 | [diff] [blame] | 19 | import kotlinx.coroutines.experimental.* |
| 20 | import kotlin.coroutines.experimental.* |
| 21 | import kotlin.coroutines.experimental.intrinsics.* |
Roman Elizarov | ee88fbe | 2017-02-22 09:53:49 +0300 | [diff] [blame] | 22 | |
| 23 | /** |
Roman Elizarov | 23fb728 | 2018-01-24 23:09:42 +0300 | [diff] [blame] | 24 | * Use this function to restart coroutine directly from inside of [suspendCoroutine] in the same context. |
Roman Elizarov | ee88fbe | 2017-02-22 09:53:49 +0300 | [diff] [blame] | 25 | */ |
| 26 | @Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN", "UNCHECKED_CAST") |
Roman Elizarov | 23fb728 | 2018-01-24 23:09:42 +0300 | [diff] [blame] | 27 | public fun <T> (suspend () -> T).startCoroutineUndispatched(completion: Continuation<T>) { |
Roman Elizarov | ee88fbe | 2017-02-22 09:53:49 +0300 | [diff] [blame] | 28 | val value = try { |
Roman Elizarov | 4638d79 | 2017-03-14 19:39:26 +0300 | [diff] [blame] | 29 | startCoroutineUninterceptedOrReturn(completion) |
Roman Elizarov | ee88fbe | 2017-02-22 09:53:49 +0300 | [diff] [blame] | 30 | } catch (e: Throwable) { |
| 31 | completion.resumeWithException(e) |
| 32 | return |
| 33 | } |
| 34 | if (value !== COROUTINE_SUSPENDED) |
Roman Elizarov | a74eb5f | 2017-05-11 20:15:18 +0300 | [diff] [blame] | 35 | completion.resume(value as T) |
Roman Elizarov | ee88fbe | 2017-02-22 09:53:49 +0300 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | /** |
Roman Elizarov | 23fb728 | 2018-01-24 23:09:42 +0300 | [diff] [blame] | 39 | * Use this function to restart coroutine directly from inside of [suspendCoroutine] in the same context. |
Roman Elizarov | ee88fbe | 2017-02-22 09:53:49 +0300 | [diff] [blame] | 40 | */ |
| 41 | @Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN", "UNCHECKED_CAST") |
Roman Elizarov | 23fb728 | 2018-01-24 23:09:42 +0300 | [diff] [blame] | 42 | public fun <R, T> (suspend (R) -> T).startCoroutineUndispatched(receiver: R, completion: Continuation<T>) { |
Roman Elizarov | ee88fbe | 2017-02-22 09:53:49 +0300 | [diff] [blame] | 43 | val value = try { |
Roman Elizarov | a74eb5f | 2017-05-11 20:15:18 +0300 | [diff] [blame] | 44 | startCoroutineUninterceptedOrReturn(receiver, completion) |
Roman Elizarov | ee88fbe | 2017-02-22 09:53:49 +0300 | [diff] [blame] | 45 | } catch (e: Throwable) { |
| 46 | completion.resumeWithException(e) |
| 47 | return |
| 48 | } |
| 49 | if (value !== COROUTINE_SUSPENDED) |
Roman Elizarov | a74eb5f | 2017-05-11 20:15:18 +0300 | [diff] [blame] | 50 | completion.resume(value as T) |
Roman Elizarov | ee88fbe | 2017-02-22 09:53:49 +0300 | [diff] [blame] | 51 | } |
Roman Elizarov | 2adf8bc | 2018-01-24 20:09:57 +0300 | [diff] [blame] | 52 | |
| 53 | /** |
Roman Elizarov | 23fb728 | 2018-01-24 23:09:42 +0300 | [diff] [blame] | 54 | * 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 Elizarov | 2adf8bc | 2018-01-24 20:09:57 +0300 | [diff] [blame] | 57 | * |
| 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 Elizarov | 2adf8bc | 2018-01-24 20:09:57 +0300 | [diff] [blame] | 60 | */ |
| 61 | public fun <T> AbstractCoroutine<T>.startUndispatchedOrReturn(block: suspend () -> T): Any? { |
| 62 | initParentJob() |
| 63 | return undispatchedResult { block.startCoroutineUninterceptedOrReturn(this) } |
| 64 | } |
| 65 | |
| 66 | /** |
Roman Elizarov | 23fb728 | 2018-01-24 23:09:42 +0300 | [diff] [blame] | 67 | * 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 Elizarov | 2adf8bc | 2018-01-24 20:09:57 +0300 | [diff] [blame] | 70 | * |
| 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 Elizarov | 2adf8bc | 2018-01-24 20:09:57 +0300 | [diff] [blame] | 73 | */ |
| 74 | public fun <T, R> AbstractCoroutine<T>.startUndispatchedOrReturn(receiver: R, block: suspend R.() -> T): Any? { |
| 75 | initParentJob() |
| 76 | return undispatchedResult { block.startCoroutineUninterceptedOrReturn(receiver, this) } |
| 77 | } |
| 78 | |
| 79 | private 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Ćski | ea4cd45 | 2018-02-01 17:18:14 +0100 | [diff] [blame] | 86 | result === COROUTINE_SUSPENDED -> COROUTINE_SUSPENDED |
Roman Elizarov | 2adf8bc | 2018-01-24 20:09:57 +0300 | [diff] [blame] | 87 | makeCompletingOnce(result, MODE_IGNORE) -> { |
Vsevolod Tolstopyatov | c1092d5 | 2018-04-12 20:22:25 +0300 | [diff] [blame] | 88 | if (result is CompletedExceptionally) throw result.cause else result |
Roman Elizarov | 2adf8bc | 2018-01-24 20:09:57 +0300 | [diff] [blame] | 89 | } |
| 90 | else -> COROUTINE_SUSPENDED |
| 91 | } |
| 92 | } |