Skip to content

Commit 907074f

Browse files
committed
A fix & optimization for select impl helping scheme
1 parent 7a7b009 commit 907074f

File tree

1 file changed

+9
-1
lines changed
  • kotlinx-coroutines-core/src/main/kotlin/kotlinx/coroutines/experimental/selects

1 file changed

+9
-1
lines changed

kotlinx-coroutines-core/src/main/kotlin/kotlinx/coroutines/experimental/selects/Select.kt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,15 @@ internal class SelectBuilderImpl<in R>(
360360
@JvmField val desc: AtomicDesc,
361361
@JvmField val select: Boolean
362362
) : AtomicOp<Any?>() {
363-
override fun prepare(affected: Any?): Any? = prepareIfNotSelected() ?: desc.prepare(this)
363+
override fun prepare(affected: Any?): Any? {
364+
// only originator of operation makes preparation move of installing descriptor into this selector's state
365+
// helpers should never do it, or risk ruining progress when they come late
366+
if (affected == null) {
367+
// we are originator (affected reference is not null if helping)
368+
prepareIfNotSelected()?.let { return it }
369+
}
370+
return desc.prepare(this)
371+
}
364372

365373
override fun complete(affected: Any?, failure: Any?) {
366374
completeSelect(failure)

0 commit comments

Comments
 (0)