KMutexGuard
Ensures that only one coroutine at a time can execute a critical section.
Wraps Mutex with an observable isLocked state and optional "skip if busy" semantics via tryRun.
Serialize concurrent writes
val guard = MutexGuard()
// Multiple coroutines calling save() will queue up safely:
suspend fun save(data: Data) = guard.run {
repository.write(data)
}Content copied to clipboard
Skip if already running (debounce-style)
val guard = MutexGuard()
// A second refresh call while the first is in progress does nothing:
suspend fun refresh() = guard.tryRun {
api.fetchLatest()
}Content copied to clipboard
Observe lock state
guard.isLocked.collect { locked -> showSpinner(locked) }Content copied to clipboard