Roman Elizarov | e22f14a | 2017-06-22 19:04:52 +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 | |
| 17 | package kotlinx.coroutines.experimental |
| 18 | |
| 19 | import kotlin.coroutines.experimental.Continuation |
| 20 | |
| 21 | @PublishedApi internal const val MODE_ATOMIC_DEFAULT = 0 // schedule non-cancellable dispatch for suspendCoroutine |
| 22 | @PublishedApi internal const val MODE_CANCELLABLE = 1 // schedule cancellable dispatch for suspendCancellableCoroutine |
| 23 | @PublishedApi internal const val MODE_DIRECT = 2 // when the context is right just invoke the delegate continuation direct |
| 24 | @PublishedApi internal const val MODE_UNDISPATCHED = 3 // when the thread is right, but need to mark it with current coroutine |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 25 | @PublishedApi internal const val MODE_IGNORE = 4 // don't do anything |
Roman Elizarov | e22f14a | 2017-06-22 19:04:52 +0300 | [diff] [blame] | 26 | |
Roman Elizarov | ac48070 | 2017-12-28 01:09:43 +0300 | [diff] [blame] | 27 | internal val Int.isCancellableMode get() = this == MODE_CANCELLABLE |
| 28 | internal val Int.isDispatchedMode get() = this == MODE_ATOMIC_DEFAULT || this == MODE_CANCELLABLE |
| 29 | |
Roman Elizarov | e1c0b65 | 2017-12-01 14:02:57 +0300 | [diff] [blame] | 30 | internal fun <T> Continuation<T>.resumeMode(value: T, mode: Int) { |
Roman Elizarov | e22f14a | 2017-06-22 19:04:52 +0300 | [diff] [blame] | 31 | when (mode) { |
| 32 | MODE_ATOMIC_DEFAULT -> resume(value) |
| 33 | MODE_CANCELLABLE -> resumeCancellable(value) |
| 34 | MODE_DIRECT -> resumeDirect(value) |
| 35 | MODE_UNDISPATCHED -> (this as DispatchedContinuation).resumeUndispatched(value) |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 36 | MODE_IGNORE -> {} |
Roman Elizarov | e22f14a | 2017-06-22 19:04:52 +0300 | [diff] [blame] | 37 | else -> error("Invalid mode $mode") |
| 38 | } |
| 39 | } |
| 40 | |
Roman Elizarov | e1c0b65 | 2017-12-01 14:02:57 +0300 | [diff] [blame] | 41 | internal fun <T> Continuation<T>.resumeWithExceptionMode(exception: Throwable, mode: Int) { |
Roman Elizarov | e22f14a | 2017-06-22 19:04:52 +0300 | [diff] [blame] | 42 | when (mode) { |
| 43 | MODE_ATOMIC_DEFAULT -> resumeWithException(exception) |
| 44 | MODE_CANCELLABLE -> resumeCancellableWithException(exception) |
| 45 | MODE_DIRECT -> resumeDirectWithException(exception) |
| 46 | MODE_UNDISPATCHED -> (this as DispatchedContinuation).resumeUndispatchedWithException(exception) |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 47 | MODE_IGNORE -> {} |
Roman Elizarov | e22f14a | 2017-06-22 19:04:52 +0300 | [diff] [blame] | 48 | else -> error("Invalid mode $mode") |
| 49 | } |
| 50 | } |