Inline reentrant, added comments

This commit is contained in:
Robinson 2022-08-19 23:30:08 +02:00
parent 5f69697663
commit de7c53567a
No known key found for this signature in database
GPG Key ID: 8E7DB78588BD6F5C

View File

@ -74,7 +74,9 @@ fun asyncIO(action: suspend CoroutineScope.() -> Unit): Job {
}
}
suspend fun <T> Mutex.withReentrantLock(block: suspend () -> T): T {
// From: https://elizarov.medium.com/phantom-of-the-coroutine-afc63b03a131
suspend inline fun <T> 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 <T> 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()
}
}
}