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 {
9191 public fun unlock (owner : Any? = null)
9292}
9393
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+
94108internal class MutexImpl (locked : Boolean ) : Mutex {
95109 // State is: Empty | LockedQueue | OpDescriptor
96110 @Volatile
@@ -396,4 +410,4 @@ internal class MutexImpl(locked: Boolean) : Mutex {
396410 return if (affected._state == = queue) UNLOCK_FAIL else null
397411 }
398412 }
399- }
413+ }
You can’t perform that action at this time.
0 commit comments