Skip to content

Commit 0aed352

Browse files
committed
Deprecate Mutex.onLock
Fixes #2794
1 parent 97ecf59 commit 0aed352

File tree

10 files changed

+38
-21
lines changed

10 files changed

+38
-21
lines changed

kotlinx-coroutines-core/README.md

-3
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ helper function. [NonCancellable] job object is provided to suppress cancellatio
5757
| [SendChannel][kotlinx.coroutines.channels.SendChannel] | [send][kotlinx.coroutines.channels.SendChannel.send] | [onSend][kotlinx.coroutines.channels.SendChannel.onSend] | [trySend][kotlinx.coroutines.channels.SendChannel.trySend]
5858
| [ReceiveChannel][kotlinx.coroutines.channels.ReceiveChannel] | [receive][kotlinx.coroutines.channels.ReceiveChannel.receive] | [onReceive][kotlinx.coroutines.channels.ReceiveChannel.onReceive] | [tryReceive][kotlinx.coroutines.channels.ReceiveChannel.tryReceive]
5959
| [ReceiveChannel][kotlinx.coroutines.channels.ReceiveChannel] | [receiveCatching][kotlinx.coroutines.channels.receiveCatching] | [onReceiveCatching][kotlinx.coroutines.channels.onReceiveCatching] | [tryReceive][kotlinx.coroutines.channels.ReceiveChannel.tryReceive]
60-
| [Mutex][kotlinx.coroutines.sync.Mutex] | [lock][kotlinx.coroutines.sync.Mutex.lock] | [onLock][kotlinx.coroutines.sync.Mutex.onLock] | [tryLock][kotlinx.coroutines.sync.Mutex.tryLock]
6160
| none | [delay][kotlinx.coroutines.delay] | [onTimeout][kotlinx.coroutines.selects.SelectBuilder.onTimeout] | none
6261

6362
# Package kotlinx.coroutines
@@ -121,8 +120,6 @@ Obsolete and deprecated module to test coroutines. Replaced with `kotlinx-corout
121120

122121
[kotlinx.coroutines.sync.Mutex]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.sync/-mutex/index.html
123122
[kotlinx.coroutines.sync.Mutex.lock]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.sync/-mutex/lock.html
124-
[kotlinx.coroutines.sync.Mutex.onLock]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.sync/-mutex/on-lock.html
125-
[kotlinx.coroutines.sync.Mutex.tryLock]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.sync/-mutex/try-lock.html
126123

127124
<!--- INDEX kotlinx.coroutines.channels -->
128125

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

-1
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,6 @@ public interface SelectInstance<in R> {
186186
* | [SendChannel] | [send][SendChannel.send] | [onSend][SendChannel.onSend]
187187
* | [ReceiveChannel] | [receive][ReceiveChannel.receive] | [onReceive][ReceiveChannel.onReceive]
188188
* | [ReceiveChannel] | [receiveCatching][ReceiveChannel.receiveCatching] | [onReceiveCatching][ReceiveChannel.onReceiveCatching]
189-
* | [Mutex] | [lock][Mutex.lock] | [onLock][Mutex.onLock]
190189
* | none | [delay] | [onTimeout][SelectBuilder.onTimeout]
191190
*
192191
* This suspending function is cancellable. If the [Job] of the current coroutine is cancelled or completed while this

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

+4-5
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ public interface Mutex {
5252
* Note that this function does not check for cancellation when it is not suspended.
5353
* Use [yield] or [CoroutineScope.isActive] to periodically check for cancellation in tight loops if needed.
5454
*
55-
* This function can be used in [select] invocation with [onLock] clause.
56-
* Use [tryLock] to try acquire lock without waiting.
55+
* Use [tryLock] to try acquiring a lock without waiting.
5756
*
5857
* This function is fair; suspended callers are resumed in first-in-first-out order.
5958
*
@@ -63,10 +62,10 @@ public interface Mutex {
6362
public suspend fun lock(owner: Any? = null)
6463

6564
/**
66-
* Clause for [select] expression of [lock] suspending function that selects when the mutex is locked.
67-
* Additional parameter for the clause in the `owner` (see [lock]) and when the clause is selected
68-
* the reference to this mutex is passed into the corresponding block.
65+
* Deprecated for removal without built-in replacement.
6966
*/
67+
@Deprecated(level = DeprecationLevel.WARNING, message = "Mutex.onLock deprecated without replacement. " +
68+
"For additional details please refer to #2794")
7069
public val onLock: SelectClause2<Any?, Mutex>
7170

7271
/**

reactive/kotlinx-coroutines-reactive/src/Publish.kt

-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@ public class PublisherCoroutine<in T>(
110110
block(this)
111111
}
112112

113-
// TODO discuss it
114113
launch(start = CoroutineStart.UNDISPATCHED) {
115114
mutex.lock()
116115
// Already selected -- bail out

reactive/kotlinx-coroutines-reactive/test/PublishTest.kt

+3-6
Original file line numberDiff line numberDiff line change
@@ -296,12 +296,10 @@ class PublishTest : TestBase() {
296296
expect(2)
297297
// Collector is ready
298298
send(1)
299-
expect(3)
300299
try {
301300
send(2)
302301
expectUnreached()
303302
} catch (e: CancellationException) {
304-
expect(7)
305303
// publisher cancellation is async
306304
latch.countDown()
307305
throw e
@@ -312,15 +310,14 @@ class PublishTest : TestBase() {
312310
val collectorLatch = Mutex(true)
313311
val job = launch {
314312
published.asFlow().buffer(0).collect {
315-
expect(4)
316313
collectorLatch.unlock()
317-
hang { expect(6) }
314+
hang { expect(4) }
318315
}
319316
}
320317
collectorLatch.lock()
321-
expect(5)
318+
expect(3)
322319
job.cancelAndJoin()
323320
latch.await()
324-
finish(8)
321+
finish(5)
325322
}
326323
}

reactive/kotlinx-coroutines-reactive/test/PublisherMultiTest.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class PublisherMultiTest : TestBase() {
3333
@Test
3434
fun testConcurrentStressOnSend() = runBlocking {
3535
val n = 10_000 * stressTestMultiplier
36-
val observable = publish {
36+
val observable = publish<Int> {
3737
// concurrent emitters (many coroutines)
3838
val jobs = List(n) {
3939
// launch

reactive/kotlinx-coroutines-rx2/src/RxObservable.kt

+14-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import kotlinx.atomicfu.*
1010
import kotlinx.coroutines.*
1111
import kotlinx.coroutines.channels.*
1212
import kotlinx.coroutines.internal.*
13+
import kotlinx.coroutines.intrinsics.*
1314
import kotlinx.coroutines.selects.*
1415
import kotlinx.coroutines.sync.*
1516
import kotlin.coroutines.*
@@ -95,10 +96,22 @@ private class RxObservableCoroutine<T : Any>(
9596
element: T,
9697
block: suspend (SendChannel<T>) -> R
9798
) {
98-
mutex.onLock.registerSelectClause2(select, null) {
99+
val clause = suspend {
99100
doLockedNext(element)?.let { throw it }
100101
block(this)
101102
}
103+
104+
// This is the default replacement proposed in onLock replacement
105+
launch(start = CoroutineStart.UNDISPATCHED) {
106+
mutex.lock()
107+
// Already selected -- bail out
108+
if (!select.trySelect()) {
109+
mutex.unlock()
110+
return@launch
111+
}
112+
113+
clause.startCoroutineCancellable(select.completion)
114+
}
102115
}
103116

104117
// assert: mutex.isLocked()

reactive/kotlinx-coroutines-rx2/test/ObservableMultiTest.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class ObservableMultiTest : TestBase() {
5151
@Test
5252
fun testConcurrentStressOnSend() {
5353
val n = 10_000 * stressTestMultiplier
54-
val observable = rxObservable {
54+
val observable = rxObservable<Int> {
5555
newCoroutineContext(coroutineContext)
5656
// concurrent emitters (many coroutines)
5757
val jobs = List(n) {

reactive/kotlinx-coroutines-rx3/src/RxObservable.kt

+14-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import kotlinx.coroutines.selects.*
1313
import kotlinx.coroutines.sync.*
1414
import kotlin.coroutines.*
1515
import kotlinx.coroutines.internal.*
16+
import kotlinx.coroutines.intrinsics.*
1617

1718
/**
1819
* Creates cold [observable][Observable] that will run a given [block] in a coroutine.
@@ -95,10 +96,22 @@ private class RxObservableCoroutine<T : Any>(
9596
element: T,
9697
block: suspend (SendChannel<T>) -> R
9798
) {
98-
mutex.onLock.registerSelectClause2(select, null) {
99+
val clause = suspend {
99100
doLockedNext(element)?.let { throw it }
100101
block(this)
101102
}
103+
104+
// This is the default replacement proposed in onLock replacement
105+
launch(start = CoroutineStart.UNDISPATCHED) {
106+
mutex.lock()
107+
// Already selected -- bail out
108+
if (!select.trySelect()) {
109+
mutex.unlock()
110+
return@launch
111+
}
112+
113+
clause.startCoroutineCancellable(select.completion)
114+
}
102115
}
103116

104117
// assert: mutex.isLocked()

reactive/kotlinx-coroutines-rx3/test/ObservableMultiTest.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class ObservableMultiTest : TestBase() {
5151
@Test
5252
fun testConcurrentStressOnSend() {
5353
val n = 10_000 * stressTestMultiplier
54-
val observable = rxObservable {
54+
val observable = rxObservable<Int> {
5555
newCoroutineContext(coroutineContext)
5656
// concurrent emitters (many coroutines)
5757
val jobs = List(n) {

0 commit comments

Comments
 (0)