commonMain.pro.respawn.flowmvi.util.ReentrantLock.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of core-jvm Show documentation
Show all versions of core-jvm Show documentation
A Kotlin Multiplatform MVI library based on plugins that is simple, fast, powerful & flexible
package pro.respawn.flowmvi.util
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import kotlinx.coroutines.withContext
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.coroutineContext
import kotlin.jvm.JvmInline
internal suspend fun Mutex.withReentrantLock(block: suspend () -> T): T {
val key = ReentrantMutexContextKey(this)
// call block directly when this mutex is already locked in the context
if (coroutineContext[key] != null) return block()
// otherwise add it to the context and lock the mutex
return withContext(ReentrantMutexContextElement(key)) {
withLock { block() }
}
}
@JvmInline
internal value class ReentrantMutexContextElement(
override val key: ReentrantMutexContextKey
) : CoroutineContext.Element
@JvmInline
internal value class ReentrantMutexContextKey(
val mutex: Mutex
) : CoroutineContext.Key
© 2015 - 2025 Weber Informatics LLC | Privacy Policy