blob: f6cf3b92731cb95d051567269e277d24e9e6de0e [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 Elizarova74eb5f2017-05-11 20:15:18 +030019import kotlinx.coroutines.experimental.resumeCancellable
Roman Elizarovee88fbe2017-02-22 09:53:49 +030020import kotlin.coroutines.experimental.Continuation
Roman Elizarova74eb5f2017-05-11 20:15:18 +030021import kotlin.coroutines.experimental.createCoroutine
22import kotlin.coroutines.experimental.intrinsics.COROUTINE_SUSPENDED
23import kotlin.coroutines.experimental.intrinsics.startCoroutineUninterceptedOrReturn
Roman Elizarovee88fbe2017-02-22 09:53:49 +030024import 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 Elizarova74eb5f2017-05-11 20:15:18 +030032internal fun <T> (suspend () -> T).startCoroutineUndispatched(completion: Continuation<T>) {
Roman Elizarovee88fbe2017-02-22 09:53:49 +030033 val value = try {
Roman Elizarov4638d792017-03-14 19:39:26 +030034 startCoroutineUninterceptedOrReturn(completion)
Roman Elizarovee88fbe2017-02-22 09:53:49 +030035 } catch (e: Throwable) {
36 completion.resumeWithException(e)
37 return
38 }
39 if (value !== COROUTINE_SUSPENDED)
Roman Elizarova74eb5f2017-05-11 20:15:18 +030040 completion.resume(value as T)
Roman Elizarovee88fbe2017-02-22 09:53:49 +030041}
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 Elizarova74eb5f2017-05-11 20:15:18 +030049internal fun <R, T> (suspend (R) -> T).startCoroutineUndispatched(receiver: R, completion: Continuation<T>) {
Roman Elizarovee88fbe2017-02-22 09:53:49 +030050 val value = try {
Roman Elizarova74eb5f2017-05-11 20:15:18 +030051 startCoroutineUninterceptedOrReturn(receiver, completion)
Roman Elizarovee88fbe2017-02-22 09:53:49 +030052 } catch (e: Throwable) {
53 completion.resumeWithException(e)
54 return
55 }
56 if (value !== COROUTINE_SUSPENDED)
Roman Elizarova74eb5f2017-05-11 20:15:18 +030057 completion.resume(value as T)
Roman Elizarovee88fbe2017-02-22 09:53:49 +030058}