Skip to content

Instantly share code, notes, and snippets.

View yorickhenning's full-sized avatar

Tyson "Yorick" Henning yorickhenning

View GitHub Profile
@yorickhenning
yorickhenning / gist:3275ea38e1619a5c8aa0efac9370484d
Last active March 19, 2023 21:16
Reentrance checking coroutine Mutex
suspend fun <ReturnT> Mutex.checkedWithLock(
block: suspend () -> ReturnT
): ReturnT {
val element = LockedMutexesElement(this).key
check (currentCoroutineContext()[element] == null) {
"Reentered Mutex"
}
return withContext(element) { withLock { block() } }
}