Skip to content

Commit a31621a

Browse files
Nikita Kovalqwwdfsad
Nikita Koval
authored andcommitted
Review fixes
1 parent 288499e commit a31621a

File tree

11 files changed

+51
-60
lines changed

11 files changed

+51
-60
lines changed

kotlinx-coroutines-core/common/src/JobSupport.kt

+2-4
Original file line numberDiff line numberDiff line change
@@ -1268,10 +1268,8 @@ public open class JobSupport constructor(active: Boolean) : Job, ChildJob, Paren
12681268
) : JobNode() {
12691269
override fun invoke(cause: Throwable?) {
12701270
val state = this@JobSupport.state
1271-
if (state !is Incomplete) {
1272-
val result = if (state is CompletedExceptionally) state else state.unboxState()
1273-
select.trySelect(this@JobSupport, result)
1274-
}
1271+
val result = if (state is CompletedExceptionally) state else state.unboxState()
1272+
select.trySelect(this@JobSupport, result)
12751273
}
12761274
}
12771275
}

kotlinx-coroutines-core/common/src/selects/OnTimeout.kt

+29-29
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,29 @@ package kotlinx.coroutines.selects
77
import kotlinx.coroutines.*
88
import kotlin.time.*
99

10+
/**
11+
* Clause that selects the given [block] after a specified timeout passes.
12+
* If timeout is negative or zero, [block] is selected immediately.
13+
*
14+
* **Note: This is an experimental api.** It may be replaced with light-weight timer/timeout channels in the future.
15+
*
16+
* @param timeMillis timeout time in milliseconds.
17+
*/
18+
@ExperimentalCoroutinesApi
19+
@Suppress("EXTENSION_SHADOWED_BY_MEMBER")
20+
public fun <R> SelectBuilder<R>.onTimeout(timeMillis: Long, block: suspend () -> R): Unit =
21+
OnTimeout(timeMillis).selectClause.invoke(block)
22+
23+
/**
24+
* Clause that selects the given [block] after the specified [timeout] passes.
25+
* If timeout is negative or zero, [block] is selected immediately.
26+
*
27+
* **Note: This is an experimental api.** It may be replaced with light-weight timer/timeout channels in the future.
28+
*/
29+
@ExperimentalCoroutinesApi
30+
public fun <R> SelectBuilder<R>.onTimeout(timeout: Duration, block: suspend () -> R): Unit =
31+
onTimeout(timeout.toDelayMillis(), block)
32+
1033
/**
1134
* We implement [SelectBuilder.onTimeout] as a clause, so each invocation creates
1235
* an instance of [OnTimeout] that specifies the registration part according to
@@ -15,6 +38,12 @@ import kotlin.time.*
1538
private class OnTimeout(
1639
private val timeMillis: Long
1740
) {
41+
val selectClause: SelectClause0
42+
get() = SelectClause0Impl(
43+
clauseObject = this@OnTimeout,
44+
regFunc = OnTimeout::register as RegistrationFunction
45+
)
46+
1847
private fun register(select: SelectInstance<*>, ignoredParam: Any?) {
1948
// Should this clause complete immediately?
2049
if (timeMillis <= 0) {
@@ -31,33 +60,4 @@ private class OnTimeout(
3160
// Do not forget to clean-up when this `select` is completed or cancelled.
3261
select.disposeOnCompletion(disposableHandle)
3362
}
34-
35-
val selectClause: SelectClause0
36-
get() = SelectClause0Impl(
37-
clauseObject = this@OnTimeout,
38-
regFunc = OnTimeout::register as RegistrationFunction
39-
)
4063
}
41-
42-
/**
43-
* Clause that selects the given [block] after a specified timeout passes.
44-
* If timeout is negative or zero, [block] is selected immediately.
45-
*
46-
* **Note: This is an experimental api.** It may be replaced with light-weight timer/timeout channels in the future.
47-
*
48-
* @param timeMillis timeout time in milliseconds.
49-
*/
50-
@ExperimentalCoroutinesApi
51-
@Suppress("EXTENSION_SHADOWED_BY_MEMBER")
52-
public fun <R> SelectBuilder<R>.onTimeout(timeMillis: Long, block: suspend () -> R): Unit =
53-
OnTimeout(timeMillis).selectClause.invoke(block)
54-
55-
/**
56-
* Clause that selects the given [block] after the specified [timeout] passes.
57-
* If timeout is negative or zero, [block] is selected immediately.
58-
*
59-
* **Note: This is an experimental api.** It may be replaced with light-weight timer/timeout channels in the future.
60-
*/
61-
@ExperimentalCoroutinesApi
62-
public fun <R> SelectBuilder<R>.onTimeout(timeout: Duration, block: suspend () -> R): Unit =
63-
onTimeout(timeout.toDelayMillis(), block)

kotlinx-coroutines-core/common/src/selects/Select.kt

+11-18
Original file line numberDiff line numberDiff line change
@@ -150,14 +150,14 @@ public typealias ProcessResultFunction = (clauseObject: Any, param: Any?, clause
150150
* cancellation while dispatching. Unfortunately, we cannot pass this function only in [SelectInstance.trySelect],
151151
* as [SelectInstance.selectInRegistrationPhase] can be called when the coroutine is already cancelled.
152152
*/
153+
@InternalCoroutinesApi
153154
public typealias OnCancellationConstructor = (select: SelectInstance<*>, param: Any?, internalResult: Any?) -> (Throwable) -> Unit
154155

155156
/**
156157
* Clause for [select] expression without additional parameters that does not select any value.
157158
*/
158159
public interface SelectClause0 : SelectClause
159-
@InternalCoroutinesApi
160-
public class SelectClause0Impl(
160+
internal class SelectClause0Impl(
161161
override val clauseObject: Any,
162162
override val regFunc: RegistrationFunction,
163163
override val onCancellationConstructor: OnCancellationConstructor? = null
@@ -171,8 +171,7 @@ private val DUMMY_PROCESS_RESULT_FUNCTION: ProcessResultFunction = { _, _, _ ->
171171
* Clause for [select] expression without additional parameters that selects value of type [Q].
172172
*/
173173
public interface SelectClause1<out Q> : SelectClause
174-
@InternalCoroutinesApi
175-
public class SelectClause1Impl<Q>(
174+
internal class SelectClause1Impl<Q>(
176175
override val clauseObject: Any,
177176
override val regFunc: RegistrationFunction,
178177
override val processResFunc: ProcessResultFunction,
@@ -183,8 +182,7 @@ public class SelectClause1Impl<Q>(
183182
* Clause for [select] expression with additional parameter of type [P] that selects value of type [Q].
184183
*/
185184
public interface SelectClause2<in P, out Q> : SelectClause
186-
@InternalCoroutinesApi
187-
public class SelectClause2Impl<P, Q>(
185+
internal class SelectClause2Impl<P, Q>(
188186
override val clauseObject: Any,
189187
override val regFunc: RegistrationFunction,
190188
override val processResFunc: ProcessResultFunction,
@@ -348,7 +346,7 @@ internal open class SelectImplementation<R> constructor(
348346
it === STATE_REG || it is List<*>
349347
}
350348
/**
351-
* Returns `true` if this `select` is already selected or cancelled;
349+
* Returns `true` if this `select` is already selected;
352350
* thus, other parties are bound to fail when making a rendezvous with it.
353351
*/
354352
private val isSelected
@@ -437,8 +435,9 @@ internal open class SelectImplementation<R> constructor(
437435
* updates the state to this clause reference.
438436
*/
439437
protected fun ClauseData<R>.register(reregister: Boolean = false) {
438+
assert { state.value !is Cancelled }
440439
// Is there already selected clause?
441-
if (state.value.let { it is ClauseData<*> || it is Cancelled }) return
440+
if (state.value.let { it is ClauseData<*> }) return
442441
// For new clauses, check that there does not exist
443442
// another clause with the same object.
444443
if (!reregister) checkClauseObject(clauseObject)
@@ -459,13 +458,7 @@ internal open class SelectImplementation<R> constructor(
459458
} else {
460459
// This clause has been selected!
461460
// Update the state correspondingly.
462-
state.update {
463-
if (it is Cancelled) {
464-
createOnCancellationAction(this@SelectImplementation, internalResult)?.invoke(it.cause)
465-
return
466-
}
467-
this
468-
}
461+
state.value = this
469462
}
470463
}
471464

@@ -529,8 +522,6 @@ internal open class SelectImplementation<R> constructor(
529522
cont.resume(Unit, curState.createOnCancellationAction(this, internalResult))
530523
return@sc
531524
}
532-
// Cancelled
533-
curState is Cancelled -> return@sc
534525
// This `select` cannot be in any other state.
535526
else -> error("unexpected state: $curState")
536527
}
@@ -787,6 +778,8 @@ private val NO_RESULT = Symbol("NO_RESULT")
787778
// We use this marker parameter objects to distinguish
788779
// SelectClause[0,1,2] and invoke the user-specified block correctly.
789780
@SharedImmutable
781+
@JvmField
790782
internal val PARAM_CLAUSE_0 = Symbol("PARAM_CLAUSE_0")
791783
@SharedImmutable
792-
internal val PARAM_CLAUSE_1 = Symbol("PARAM_CLAUSE_1")
784+
@JvmField
785+
internal val PARAM_CLAUSE_1 = Symbol("PARAM_CLAUSE_1")

kotlinx-coroutines-core/common/src/selects/SelectOld.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,4 @@ private fun <T> CancellableContinuation<T>.resumeUndispatched(result: T) {
102102
} else {
103103
resume(result)
104104
}
105-
}
105+
}

kotlinx-coroutines-core/common/src/selects/SelectUnbiased.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,4 @@ internal open class UnbiasedSelectImplementation<R>(context: CoroutineContext) :
6565
} finally {
6666
clausesToRegister.clear()
6767
}
68-
}
68+
}

kotlinx-coroutines-core/common/src/sync/Semaphore.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ internal open class SemaphoreImpl(private val permits: Int, acquiredPermits: Int
305305
// If the cell already has PERMIT from tryResumeNextFromQueue, try to grab it
306306
if (segment.cas(i, PERMIT, TAKEN)) { // took permit thus eliminating acquire/release pair
307307
/// This continuation is not yet published, but still can be cancelled via outer job
308-
when(waiter) {
308+
when (waiter) {
309309
is CancellableContinuation<*> -> {
310310
waiter as CancellableContinuation<Unit>
311311
waiter.resume(Unit, onCancellationRelease)

kotlinx-coroutines-core/common/test/selects/SelectOldTest.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,4 @@ class SelectOldTest : TestBase() {
121121
finish(4)
122122
assertEquals("OK", res)
123123
}
124-
}
124+
}

kotlinx-coroutines-core/concurrent/test/selects/SelectMutexStressTest.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,4 @@ class SelectMutexStressTest : TestBase() {
3030
assertTrue(mutex.isLocked)
3131
finish(n + 2)
3232
}
33-
}
33+
}

kotlinx-coroutines-core/jvm/test/MutexStressTest.kt renamed to kotlinx-coroutines-core/jvm/test/MutexCancellationStressTest.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import kotlinx.coroutines.sync.*
99
import org.junit.*
1010
import java.util.concurrent.*
1111

12-
class MutexStressTest : TestBase() {
12+
class MutexCancellationStressTest : TestBase() {
1313
@Test
1414
fun testStressCancellationDoesNotBreakMutex() = runTest {
1515
val mutex = Mutex()
@@ -72,4 +72,4 @@ class MutexStressTest : TestBase() {
7272
check(counter == counterLocal.sumOf { it.value })
7373
dispatcher.close()
7474
}
75-
}
75+
}

kotlinx-coroutines-core/jvm/test/lincheck/MutexLincheckTest.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,4 @@ class MutexLincheckTest : AbstractLincheckTest() {
4141
override fun extractState() = (1..2).map { mutex.holdsLock(it) } + mutex.isLocked
4242

4343
private val Int.asOwnerOrNull get() = if (this == 0) null else this
44-
}
44+
}

kotlinx-coroutines-core/jvm/test/lincheck/SemaphoreLincheckTest.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@ abstract class SemaphoreLincheckTestBase(permits: Int) : AbstractLincheckTest()
3232
}
3333

3434
class Semaphore1LincheckTest : SemaphoreLincheckTestBase(1)
35-
class Semaphore2LincheckTest : SemaphoreLincheckTestBase(2)
35+
class Semaphore2LincheckTest : SemaphoreLincheckTestBase(2)

0 commit comments

Comments
 (0)