Roman Elizarov | 04d11ca | 2017-02-27 12:47:32 +0300 | [diff] [blame] | 1 | /* |
Roman Elizarov | 1f74a2d | 2018-06-29 19:19:45 +0300 | [diff] [blame^] | 2 | * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. |
Roman Elizarov | 04d11ca | 2017-02-27 12:47:32 +0300 | [diff] [blame] | 3 | */ |
| 4 | |
| 5 | package kotlinx.coroutines.experimental.selects |
| 6 | |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 7 | import kotlinx.coroutines.experimental.timeunit.* |
| 8 | import kotlin.coroutines.experimental.* |
| 9 | import kotlin.coroutines.experimental.intrinsics.* |
Roman Elizarov | 04d11ca | 2017-02-27 12:47:32 +0300 | [diff] [blame] | 10 | |
| 11 | /** |
| 12 | * Waits for the result of multiple suspending functions simultaneously like [select], but in an _unbiased_ |
| 13 | * way when multiple clauses are selectable at the same time. |
| 14 | * |
| 15 | * This unbiased implementation of `select` expression randomly shuffles the clauses before checking |
| 16 | * if they are selectable, thus ensuring that there is no statistical bias to the selection of the first |
| 17 | * clauses. |
| 18 | * |
| 19 | * See [select] function description for all the other details. |
| 20 | */ |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 21 | public suspend inline fun <R> selectUnbiased(crossinline builder: SelectBuilder<R>.() -> Unit): R = |
Roman Elizarov | 04d11ca | 2017-02-27 12:47:32 +0300 | [diff] [blame] | 22 | suspendCoroutineOrReturn { cont -> |
| 23 | val scope = UnbiasedSelectBuilderImpl(cont) |
| 24 | try { |
| 25 | builder(scope) |
| 26 | } catch (e: Throwable) { |
| 27 | scope.handleBuilderException(e) |
| 28 | } |
| 29 | scope.initSelectResult() |
| 30 | } |
| 31 | |
| 32 | |
| 33 | @PublishedApi |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 34 | internal class UnbiasedSelectBuilderImpl<in R>(cont: Continuation<R>) : |
| 35 | SelectBuilder<R> { |
Roman Elizarov | 04d11ca | 2017-02-27 12:47:32 +0300 | [diff] [blame] | 36 | val instance = SelectBuilderImpl(cont) |
| 37 | val clauses = arrayListOf<() -> Unit>() |
| 38 | |
| 39 | @PublishedApi |
| 40 | internal fun handleBuilderException(e: Throwable) = instance.handleBuilderException(e) |
| 41 | |
| 42 | @PublishedApi |
| 43 | internal fun initSelectResult(): Any? { |
| 44 | if (!instance.isSelected) { |
| 45 | try { |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 46 | clauses.shuffle() |
Roman Elizarov | 04d11ca | 2017-02-27 12:47:32 +0300 | [diff] [blame] | 47 | clauses.forEach { it.invoke() } |
| 48 | } catch (e: Throwable) { |
| 49 | instance.handleBuilderException(e) |
| 50 | } |
| 51 | } |
Roman Elizarov | 932e860 | 2017-06-21 17:21:37 +0300 | [diff] [blame] | 52 | return instance.getResult() |
Roman Elizarov | 04d11ca | 2017-02-27 12:47:32 +0300 | [diff] [blame] | 53 | } |
| 54 | |
Roman Elizarov | db0e22d | 2017-08-29 18:15:57 +0300 | [diff] [blame] | 55 | override fun SelectClause0.invoke(block: suspend () -> R) { |
| 56 | clauses += { registerSelectClause0(instance, block) } |
Roman Elizarov | 04d11ca | 2017-02-27 12:47:32 +0300 | [diff] [blame] | 57 | } |
| 58 | |
Roman Elizarov | db0e22d | 2017-08-29 18:15:57 +0300 | [diff] [blame] | 59 | override fun <Q> SelectClause1<Q>.invoke(block: suspend (Q) -> R) { |
| 60 | clauses += { registerSelectClause1(instance, block) } |
Roman Elizarov | 04d11ca | 2017-02-27 12:47:32 +0300 | [diff] [blame] | 61 | } |
| 62 | |
Roman Elizarov | db0e22d | 2017-08-29 18:15:57 +0300 | [diff] [blame] | 63 | override fun <P, Q> SelectClause2<P, Q>.invoke(param: P, block: suspend (Q) -> R) { |
| 64 | clauses += { registerSelectClause2(instance, param, block) } |
Roman Elizarov | 174c696 | 2017-02-28 17:36:51 +0300 | [diff] [blame] | 65 | } |
Roman Elizarov | 9d61b3e | 2017-04-19 18:32:11 +0300 | [diff] [blame] | 66 | |
| 67 | override fun onTimeout(time: Long, unit: TimeUnit, block: suspend () -> R) { |
| 68 | clauses += { instance.onTimeout(time, unit, block) } |
| 69 | } |
Roman Elizarov | 04d11ca | 2017-02-27 12:47:32 +0300 | [diff] [blame] | 70 | } |