From de7c53567abf8dcacf38de4d79ded3f5c143f0ba Mon Sep 17 00:00:00 2001 From: Robinson Date: Fri, 19 Aug 2022 23:30:08 +0200 Subject: [PATCH] Inline reentrant, added comments --- src/dorkbox/util/CommonUtils.kt | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/dorkbox/util/CommonUtils.kt b/src/dorkbox/util/CommonUtils.kt index 1b41241..5f9a185 100644 --- a/src/dorkbox/util/CommonUtils.kt +++ b/src/dorkbox/util/CommonUtils.kt @@ -74,7 +74,9 @@ fun asyncIO(action: suspend CoroutineScope.() -> Unit): Job { } } -suspend fun Mutex.withReentrantLock(block: suspend () -> T): T { + +// From: https://elizarov.medium.com/phantom-of-the-coroutine-afc63b03a131 +suspend inline fun Mutex.withReentrantLock(crossinline block: suspend () -> T): T { val key = ReentrantMutexContextKey(this) // call block directly when this mutex is already locked in the context @@ -82,7 +84,9 @@ suspend fun Mutex.withReentrantLock(block: suspend () -> T): T { // otherwise add it to the context and lock the mutex return withContext(ReentrantMutexContextElement(key)) { - withLock { block() } + withLock { + return@withContext block() + } } }