Skip to content

Commit 9d0f7a0

Browse files
committed
Deprecate Mutex.onLock
Fixes #2794
1 parent 97ecf59 commit 9d0f7a0

File tree

6 files changed

+32
-12
lines changed

6 files changed

+32
-12
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-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-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()

0 commit comments

Comments
 (0)