File tree Expand file tree Collapse file tree 1 file changed +15
-1
lines changed
kotlinx-coroutines-core/src/main/kotlin/kotlinx/coroutines/experimental/sync Expand file tree Collapse file tree 1 file changed +15
-1
lines changed Original file line number Diff line number Diff line change @@ -91,6 +91,20 @@ public interface Mutex {
91
91
public fun unlock (owner : Any? = null)
92
92
}
93
93
94
+ /* *
95
+ * Executes the given [action] under this mutex.
96
+ * @return the return value of the action.
97
+ */
98
+ // :todo: this function needs to be make inline as soon as this bug is fixed: https://youtrack.jetbrains.com/issue/KT-16448
99
+ public suspend fun <T > Mutex.withMutex (action : suspend () -> T ): T {
100
+ lock()
101
+ try {
102
+ return action()
103
+ } finally {
104
+ unlock()
105
+ }
106
+ }
107
+
94
108
internal class MutexImpl (locked : Boolean ) : Mutex {
95
109
// State is: Empty | LockedQueue | OpDescriptor
96
110
@Volatile
@@ -396,4 +410,4 @@ internal class MutexImpl(locked: Boolean) : Mutex {
396
410
return if (affected._state == = queue) UNLOCK_FAIL else null
397
411
}
398
412
}
399
- }
413
+ }
You can’t perform that action at this time.
0 commit comments