Roman Elizarov | f16fd27 | 2017-02-07 11:26:00 +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 | |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 17 | package kotlinx.coroutines.experimental |
| 18 | |
Roman Elizarov | ea4a51b | 2017-01-31 12:01:25 +0300 | [diff] [blame] | 19 | import kotlin.coroutines.experimental.Continuation |
| 20 | import kotlin.coroutines.experimental.CoroutineContext |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 21 | |
Roman Elizarov | 53a0a40 | 2017-01-19 20:21:57 +0300 | [diff] [blame] | 22 | /** |
Roman Elizarov | d528e3e | 2017-01-23 15:40:05 +0300 | [diff] [blame] | 23 | * Receiver interface for generic coroutine builders, so that the code inside coroutine has a convenient access |
| 24 | * to its [context] and cancellation status via [isActive]. |
| 25 | */ |
| 26 | public interface CoroutineScope { |
| 27 | /** |
| 28 | * Returns `true` when this coroutine is still active (was not cancelled). |
| 29 | */ |
| 30 | public val isActive: Boolean |
| 31 | |
| 32 | /** |
| 33 | * Returns the context of this coroutine. |
| 34 | */ |
| 35 | public val context: CoroutineContext |
| 36 | } |
| 37 | |
| 38 | /** |
Roman Elizarov | 53a0a40 | 2017-01-19 20:21:57 +0300 | [diff] [blame] | 39 | * Abstract class to simplify writing of coroutine completion objects that |
| 40 | * implements [Continuation] and [Job] interfaces. |
| 41 | * It stores the result of continuation in the state of the job. |
| 42 | */ |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 43 | @Suppress("LeakingThis") |
Roman Elizarov | 55888f2 | 2017-01-26 11:48:37 +0300 | [diff] [blame] | 44 | internal abstract class AbstractCoroutine<in T>(context: CoroutineContext) : JobSupport(), Continuation<T>, CoroutineScope { |
Roman Elizarov | 41c5c8b | 2017-01-25 13:37:15 +0300 | [diff] [blame] | 45 | override val context: CoroutineContext = context + this // merges this job into this context |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 46 | |
Roman Elizarov | 53a0a40 | 2017-01-19 20:21:57 +0300 | [diff] [blame] | 47 | final override fun resume(value: T) { |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 48 | while (true) { // lock-free loop on state |
| 49 | val state = getState() // atomic read |
| 50 | when (state) { |
| 51 | is Active -> if (updateState(state, value)) return |
| 52 | is Cancelled -> return // ignore resumes on cancelled continuation |
| 53 | else -> throw IllegalStateException("Already resumed, but got value $value") |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
Roman Elizarov | 53a0a40 | 2017-01-19 20:21:57 +0300 | [diff] [blame] | 58 | final override fun resumeWithException(exception: Throwable) { |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 59 | while (true) { // lock-free loop on state |
| 60 | val state = getState() // atomic read |
| 61 | when (state) { |
Roman Elizarov | b7c46de | 2017-02-08 12:35:24 +0300 | [diff] [blame^] | 62 | is Active -> if (updateState(state, CompletedExceptionally(exception))) return |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 63 | is Cancelled -> { |
| 64 | // ignore resumes on cancelled continuation, but handle exception if a different one is here |
| 65 | if (exception != state.exception) handleCoroutineException(context, exception) |
| 66 | return |
| 67 | } |
| 68 | else -> throw IllegalStateException("Already resumed, but got exception $exception", exception) |
| 69 | } |
| 70 | } |
| 71 | } |
Roman Elizarov | c581454 | 2017-01-19 10:19:06 +0300 | [diff] [blame] | 72 | |
Roman Elizarov | 53a0a40 | 2017-01-19 20:21:57 +0300 | [diff] [blame] | 73 | final override fun handleCompletionException(closeException: Throwable) { |
| 74 | handleCoroutineException(context, closeException) |
Roman Elizarov | c581454 | 2017-01-19 10:19:06 +0300 | [diff] [blame] | 75 | } |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 76 | } |