|
| 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 |
| 40 | +import kotlinx.coroutines.experimental.asCoroutineDispatcher |
| 41 | +import kotlinx.coroutines.experimental.cancelFutureOnCompletion |
| 42 | +import kotlinx.coroutines.experimental.suspendCancellableCoroutine |
| 43 | +import kotlin.coroutines.experimental.Continuation |
| 44 | +import kotlin.coroutines.experimental.CoroutineContext |
| 45 | +import kotlin.coroutines.experimental.startCoroutine |
| 46 | + |
| 47 | +/** |
| 48 | + * Runs Quasar-instrumented suspendable code from Kotlin coroutine. |
| 49 | + */ |
| 50 | +suspend fun <T> runSuspendable(callable: SuspendableCallable<T>): T = suspendCancellableCoroutine { cont -> |
| 51 | + val fiber = object : Fiber<Unit>() { |
| 52 | + @Throws(SuspendExecution::class) |
| 53 | + override fun run() { |
| 54 | + val result = try { callable.run() } |
| 55 | + catch (e: Throwable) { |
| 56 | + cont.resumeWithException(e) |
| 57 | + return |
| 58 | + } |
| 59 | + cont.resume(result) |
| 60 | + } |
| 61 | + } |
| 62 | + cont.cancelFutureOnCompletion(fiber) |
| 63 | + fiber.start() |
| 64 | +} |
| 65 | + |
| 66 | +/** |
| 67 | + * Runs Kotlin suspending function from Quasar-instrumented suspendable code. |
| 68 | + */ |
| 69 | +@Suspendable |
| 70 | +fun <T> runFiberBlocking(block: suspend () -> T): T = |
| 71 | + CoroutineAsync(block).run() |
| 72 | + |
| 73 | +private class CoroutineAsync<T>( |
| 74 | + private val block: suspend () -> T |
| 75 | +) : FiberAsync<T, Throwable>(), Continuation<T> { |
| 76 | + override val context: CoroutineContext = Fiber.currentFiber().scheduler.executor.asCoroutineDispatcher() |
| 77 | + override fun resume(value: T) { asyncCompleted(value) } |
| 78 | + override fun resumeWithException(exception: Throwable) { asyncFailed(exception) } |
| 79 | + |
| 80 | + override fun requestAsync() { |
| 81 | + block.startCoroutine(completion = this) |
| 82 | + } |
| 83 | +} |
0 commit comments