Seri
05/12/2020, 8:01 PMMutex.withLock { ... }, but with calls while the Mutex is locked being discarded instead of suspending.octylFractal
05/12/2020, 8:13 PMtryLock not good enough?octylFractal
05/12/2020, 8:17 PMinline fun <T : Any> Mutex.tryWithLock(owner: Any? = null, action: () -> T): T? {
return if (tryLock(owner)) {
try {
action()
} finally {
unlock(owner)
}
} else {
null
}
}
explicitly forbids null because it's the failure signal here, you can change that up if you wantSeri
05/12/2020, 8:21 PMSeri
05/12/2020, 8:21 PMoctylFractal
05/12/2020, 8:21 PMtryLock returns true, you got the exclusive lock -- nothing can interrupt itSeri
05/12/2020, 8:23 PMif (!mutex.isLocked) {
mutex.withLock { ... }
}Seri
05/12/2020, 8:24 PMoctylFractal
05/12/2020, 8:24 PMtryLock should workSeri
05/12/2020, 8:24 PM