Skip to content

Instantly share code, notes, and snippets.

@orcchg
Created October 21, 2023 18:22
Show Gist options
  • Save orcchg/8b4bb6254c13888ad7e16f75f68f2294 to your computer and use it in GitHub Desktop.
Save orcchg/8b4bb6254c13888ad7e16f75f68f2294 to your computer and use it in GitHub Desktop.
Next coroutine suspends at the very beginning
suspend fun <T> synchronized(block: suspend () -> T): T {
val thisJobId = id.getAndIncrement()
while (isInsideCriticalSection) {
suspendCoroutine { cont -> // <-- suspending call
suspendedJobs[thisJobId] = cont
}
suspendedJobs.remove(thisJobId)
}
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment