blob: e5b18c0b77fb7fa45c7ad0198710603ac8377ec6 [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
19import kotlin.coroutines.experimental.Continuation
Roman Elizarov4638d792017-03-14 19:39:26 +030020import kotlin.coroutines.experimental.intrinsics.*
Roman Elizarovee88fbe2017-02-22 09:53:49 +030021import kotlin.coroutines.experimental.suspendCoroutine
22
23/**
24 * Use this function to restart coroutine directly from inside of [suspendCoroutine].
25 *
26 * @suppress **This is unstable API and it is subject to change.**
27 */
28@Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN", "UNCHECKED_CAST")
Roman Elizarov4638d792017-03-14 19:39:26 +030029internal fun <R> (suspend () -> R).startCoroutineUndispatched(completion: Continuation<R>) {
Roman Elizarovee88fbe2017-02-22 09:53:49 +030030 val value = try {
Roman Elizarov4638d792017-03-14 19:39:26 +030031 startCoroutineUninterceptedOrReturn(completion)
Roman Elizarovee88fbe2017-02-22 09:53:49 +030032 } catch (e: Throwable) {
33 completion.resumeWithException(e)
34 return
35 }
36 if (value !== COROUTINE_SUSPENDED)
37 completion.resume(value as R)
38}
39
40/**
41 * Use this function to restart coroutine directly from inside of [suspendCoroutine].
42 *
43 * @suppress **This is unstable API and it is subject to change.**
44 */
45@Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN", "UNCHECKED_CAST")
Roman Elizarov4638d792017-03-14 19:39:26 +030046internal fun <E, R> (suspend (E) -> R).startCoroutineUndispatched(element: E, completion: Continuation<R>) {
Roman Elizarovee88fbe2017-02-22 09:53:49 +030047 val value = try {
Roman Elizarov4638d792017-03-14 19:39:26 +030048 startCoroutineUninterceptedOrReturn(element, completion)
Roman Elizarovee88fbe2017-02-22 09:53:49 +030049 } catch (e: Throwable) {
50 completion.resumeWithException(e)
51 return
52 }
53 if (value !== COROUTINE_SUSPENDED)
54 completion.resume(value as R)
55}