withLockOrNull

suspend fun <T> withLockOrNull(key: K, timeout: Duration, block: suspend () -> T): T?

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

A non-positive timeout degrades to a single non-waiting lock attempt. Once the lock is acquired, block runs without any further deadline.

Cancellation: waiting for the key's mutex is cancellable and rethrows; cancellation inside block releases the mutex and rethrows. Entry cleanup runs in a NonCancellable context, so a cancelled caller never leaks its entry.

Parameters

timeout

how long to wait for the key's mutex.