withLockOrNull

suspend fun <T> Mutex.withLockOrNull(timeout: Duration, block: suspend () -> T): T?

Runs block while holding this mutex, waiting at most timeout for the lock, returning null on timeout.

A non-positive timeout degrades to a single Mutex.tryLock attempt. Once the lock is acquired, block runs without any further deadline and the mutex is released even when block throws.

Cancellation: waiting for the lock is cancellable and rethrows without leaving the mutex locked; cancellation inside block releases the mutex and rethrows.

Parameters

timeout

how long to wait for the lock.