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 | a74eb5f | 2017-05-11 20:15:18 +0300 | [diff] [blame^] | 19 | import kotlinx.coroutines.experimental.resumeCancellable |
Roman Elizarov | ee88fbe | 2017-02-22 09:53:49 +0300 | [diff] [blame] | 20 | import kotlin.coroutines.experimental.Continuation |
Roman Elizarov | a74eb5f | 2017-05-11 20:15:18 +0300 | [diff] [blame^] | 21 | import kotlin.coroutines.experimental.createCoroutine |
| 22 | import kotlin.coroutines.experimental.intrinsics.COROUTINE_SUSPENDED |
| 23 | import kotlin.coroutines.experimental.intrinsics.startCoroutineUninterceptedOrReturn |
Roman Elizarov | ee88fbe | 2017-02-22 09:53:49 +0300 | [diff] [blame] | 24 | import kotlin.coroutines.experimental.suspendCoroutine |
| 25 | |
| 26 | /** |
| 27 | * Use this function to restart coroutine directly from inside of [suspendCoroutine]. |
| 28 | * |
| 29 | * @suppress **This is unstable API and it is subject to change.** |
| 30 | */ |
| 31 | @Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN", "UNCHECKED_CAST") |
Roman Elizarov | a74eb5f | 2017-05-11 20:15:18 +0300 | [diff] [blame^] | 32 | internal fun <T> (suspend () -> T).startCoroutineUndispatched(completion: Continuation<T>) { |
Roman Elizarov | ee88fbe | 2017-02-22 09:53:49 +0300 | [diff] [blame] | 33 | val value = try { |
Roman Elizarov | 4638d79 | 2017-03-14 19:39:26 +0300 | [diff] [blame] | 34 | startCoroutineUninterceptedOrReturn(completion) |
Roman Elizarov | ee88fbe | 2017-02-22 09:53:49 +0300 | [diff] [blame] | 35 | } catch (e: Throwable) { |
| 36 | completion.resumeWithException(e) |
| 37 | return |
| 38 | } |
| 39 | if (value !== COROUTINE_SUSPENDED) |
Roman Elizarov | a74eb5f | 2017-05-11 20:15:18 +0300 | [diff] [blame^] | 40 | completion.resume(value as T) |
Roman Elizarov | ee88fbe | 2017-02-22 09:53:49 +0300 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Use this function to restart coroutine directly from inside of [suspendCoroutine]. |
| 45 | * |
| 46 | * @suppress **This is unstable API and it is subject to change.** |
| 47 | */ |
| 48 | @Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN", "UNCHECKED_CAST") |
Roman Elizarov | a74eb5f | 2017-05-11 20:15:18 +0300 | [diff] [blame^] | 49 | internal fun <R, T> (suspend (R) -> T).startCoroutineUndispatched(receiver: R, completion: Continuation<T>) { |
Roman Elizarov | ee88fbe | 2017-02-22 09:53:49 +0300 | [diff] [blame] | 50 | val value = try { |
Roman Elizarov | a74eb5f | 2017-05-11 20:15:18 +0300 | [diff] [blame^] | 51 | startCoroutineUninterceptedOrReturn(receiver, completion) |
Roman Elizarov | ee88fbe | 2017-02-22 09:53:49 +0300 | [diff] [blame] | 52 | } catch (e: Throwable) { |
| 53 | completion.resumeWithException(e) |
| 54 | return |
| 55 | } |
| 56 | if (value !== COROUTINE_SUSPENDED) |
Roman Elizarov | a74eb5f | 2017-05-11 20:15:18 +0300 | [diff] [blame^] | 57 | completion.resume(value as T) |
Roman Elizarov | ee88fbe | 2017-02-22 09:53:49 +0300 | [diff] [blame] | 58 | } |