Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 1 | /* |
Aurimas Liutikas | 79e555e | 2021-05-17 17:41:41 +0000 | [diff] [blame] | 2 | * Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 3 | */ |
| 4 | |
Roman Elizarov | 0950dfa | 2018-07-13 10:33:25 +0300 | [diff] [blame] | 5 | package kotlinx.coroutines |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 6 | |
Roman Elizarov | 0950dfa | 2018-07-13 10:33:25 +0300 | [diff] [blame] | 7 | import kotlin.coroutines.* |
Roman Elizarov | 8f1f252 | 2019-11-23 18:21:22 +0300 | [diff] [blame] | 8 | import kotlin.jvm.* |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 9 | |
| 10 | /** |
| 11 | * A coroutine dispatcher that is not confined to any specific thread. |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 12 | */ |
Vsevolod Tolstopyatov | 1f7b2d8 | 2018-10-09 15:57:51 +0300 | [diff] [blame] | 13 | internal object Unconfined : CoroutineDispatcher() { |
Steve Elliott | ca095be | 2022-07-25 14:26:10 +0000 | [diff] [blame] | 14 | |
| 15 | @ExperimentalCoroutinesApi |
| 16 | override fun limitedParallelism(parallelism: Int): CoroutineDispatcher { |
| 17 | throw UnsupportedOperationException("limitedParallelism is not supported for Dispatchers.Unconfined") |
| 18 | } |
| 19 | |
Vsevolod Tolstopyatov | fd54bc4 | 2018-10-17 18:37:36 +0300 | [diff] [blame] | 20 | override fun isDispatchNeeded(context: CoroutineContext): Boolean = false |
Roman Elizarov | 8f1f252 | 2019-11-23 18:21:22 +0300 | [diff] [blame] | 21 | |
| 22 | override fun dispatch(context: CoroutineContext, block: Runnable) { |
Steve Elliott | ca095be | 2022-07-25 14:26:10 +0000 | [diff] [blame] | 23 | /** It can only be called by the [yield] function. See also code of [yield] function. */ |
Roman Elizarov | 8f1f252 | 2019-11-23 18:21:22 +0300 | [diff] [blame] | 24 | val yieldContext = context[YieldContext] |
| 25 | if (yieldContext != null) { |
| 26 | // report to "yield" that it is an unconfined dispatcher and don't call "block.run()" |
| 27 | yieldContext.dispatcherWasUnconfined = true |
| 28 | return |
| 29 | } |
Roman Elizarov | 69d1d41 | 2019-11-26 11:33:08 +0300 | [diff] [blame] | 30 | throw UnsupportedOperationException("Dispatchers.Unconfined.dispatch function can only be used by the yield function. " + |
| 31 | "If you wrap Unconfined dispatcher in your code, make sure you properly delegate " + |
| 32 | "isDispatchNeeded and dispatch calls.") |
Roman Elizarov | 8f1f252 | 2019-11-23 18:21:22 +0300 | [diff] [blame] | 33 | } |
| 34 | |
Vsevolod Tolstopyatov | ad542c4 | 2020-06-17 04:43:28 -0700 | [diff] [blame] | 35 | override fun toString(): String = "Dispatchers.Unconfined" |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 36 | } |
Roman Elizarov | 8f1f252 | 2019-11-23 18:21:22 +0300 | [diff] [blame] | 37 | |
| 38 | /** |
| 39 | * Used to detect calls to [Unconfined.dispatch] from [yield] function. |
| 40 | */ |
Steve Elliott | ca095be | 2022-07-25 14:26:10 +0000 | [diff] [blame] | 41 | @PublishedApi |
Roman Elizarov | 8f1f252 | 2019-11-23 18:21:22 +0300 | [diff] [blame] | 42 | internal class YieldContext : AbstractCoroutineContextElement(Key) { |
| 43 | companion object Key : CoroutineContext.Key<YieldContext> |
| 44 | |
| 45 | @JvmField |
| 46 | var dispatcherWasUnconfined = false |
Vsevolod Tolstopyatov | 6d1a6e3 | 2020-02-18 15:28:00 +0300 | [diff] [blame] | 47 | } |