Skip to content

Commit 3a78aa2

Browse files
Francesco Vascoelizarov
Francesco Vasco
authored andcommitted
'owner' support for Mutex.withLock
1 parent a1ff3bd commit 3a78aa2

File tree

1 file changed

+12
-3
lines changed
  • kotlinx-coroutines-core/src/main/kotlin/kotlinx/coroutines/experimental/sync

1 file changed

+12
-3
lines changed

kotlinx-coroutines-core/src/main/kotlin/kotlinx/coroutines/experimental/sync/Mutex.kt

+12-3
Original file line numberDiff line numberDiff line change
@@ -106,18 +106,27 @@ public fun Mutex(locked: Boolean = false): Mutex = MutexImpl(locked)
106106

107107
/**
108108
* Executes the given [action] under this mutex's lock.
109+
*
110+
* @param owner Optional owner token for debugging.
111+
*
109112
* @return the return value of the action.
110113
*/
111114
// :todo: this function needs to be make inline as soon as this bug is fixed: https://youtrack.jetbrains.com/issue/KT-16448
112-
public suspend fun <T> Mutex.withLock(action: suspend () -> T): T {
113-
lock()
115+
public suspend fun <T> Mutex.withLock(owner: Any? = null, action: suspend () -> T): T {
116+
lock(owner)
114117
try {
115118
return action()
116119
} finally {
117-
unlock()
120+
unlock(owner)
118121
}
119122
}
120123

124+
/**
125+
* @suppress: **Deprecated**: Use [withLock]
126+
*/
127+
@Deprecated("Use `withLock(owner, action)", level = DeprecationLevel.HIDDEN)
128+
public suspend fun <T> Mutex.withLock(action: suspend () -> T): T = withLock(null, action)
129+
121130
/**
122131
* @suppress: **Deprecated**: Use [withLock]
123132
*/

0 commit comments

Comments
 (0)