Roman Elizarov | 44e3ba5 | 2017-08-01 22:01:31 -0700 | [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 | |
| 17 | /* |
| 18 | * Copyright 2016-2017 JetBrains s.r.o. |
| 19 | * |
| 20 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 21 | * you may not use this file except in compliance with the License. |
| 22 | * You may obtain a copy of the License at |
| 23 | * |
| 24 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 25 | * |
| 26 | * Unless required by applicable law or agreed to in writing, software |
| 27 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 28 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 29 | * See the License for the specific language governing permissions and |
| 30 | * limitations under the License. |
| 31 | */ |
| 32 | |
| 33 | package kotlinx.coroutines.experimental.quasar |
| 34 | |
| 35 | import co.paralleluniverse.fibers.Fiber |
| 36 | import co.paralleluniverse.fibers.FiberAsync |
| 37 | import co.paralleluniverse.fibers.SuspendExecution |
| 38 | import co.paralleluniverse.fibers.Suspendable |
| 39 | import co.paralleluniverse.strands.SuspendableCallable |
Vsevolod Tolstopyatov | 80a2947 | 2018-04-17 16:02:02 +0300 | [diff] [blame] | 40 | import kotlinx.coroutines.experimental.* |
Roman Elizarov | 44e3ba5 | 2017-08-01 22:01:31 -0700 | [diff] [blame] | 41 | import kotlin.coroutines.experimental.Continuation |
| 42 | import kotlin.coroutines.experimental.CoroutineContext |
| 43 | import kotlin.coroutines.experimental.startCoroutine |
| 44 | |
| 45 | /** |
| 46 | * Runs Quasar-instrumented suspendable code from Kotlin coroutine. |
| 47 | */ |
| 48 | suspend fun <T> runSuspendable(callable: SuspendableCallable<T>): T = suspendCancellableCoroutine { cont -> |
| 49 | val fiber = object : Fiber<Unit>() { |
| 50 | @Throws(SuspendExecution::class) |
| 51 | override fun run() { |
| 52 | val result = try { callable.run() } |
| 53 | catch (e: Throwable) { |
| 54 | cont.resumeWithException(e) |
| 55 | return |
| 56 | } |
| 57 | cont.resume(result) |
| 58 | } |
| 59 | } |
Vsevolod Tolstopyatov | 80a2947 | 2018-04-17 16:02:02 +0300 | [diff] [blame] | 60 | cont.cancelFutureOnCancellation(fiber) |
Roman Elizarov | 44e3ba5 | 2017-08-01 22:01:31 -0700 | [diff] [blame] | 61 | fiber.start() |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Runs Kotlin suspending function from Quasar-instrumented suspendable code. |
| 66 | */ |
| 67 | @Suspendable |
| 68 | fun <T> runFiberBlocking(block: suspend () -> T): T = |
| 69 | CoroutineAsync(block).run() |
| 70 | |
| 71 | private class CoroutineAsync<T>( |
| 72 | private val block: suspend () -> T |
| 73 | ) : FiberAsync<T, Throwable>(), Continuation<T> { |
| 74 | override val context: CoroutineContext = Fiber.currentFiber().scheduler.executor.asCoroutineDispatcher() |
| 75 | override fun resume(value: T) { asyncCompleted(value) } |
| 76 | override fun resumeWithException(exception: Throwable) { asyncFailed(exception) } |
| 77 | |
| 78 | override fun requestAsync() { |
| 79 | block.startCoroutine(completion = this) |
| 80 | } |
| 81 | } |