Mutex

interface Mutex

Mutual exclusion for coroutines.

Mutex has two states: locked and unlocked. It is non-reentrant, that is invoking lock even from the same thread/coroutine that currently holds the lock still suspends the invoker.

JVM API note: Memory semantic of the Mutex is similar to synchronized block on JVM: An unlock on a Mutex happens-before every subsequent successful lock on that Mutex. Unsuccessful call to tryLock do not have any memory effects.

Functions

holdsLock
Link copied to clipboard
common
abstract fun holdsLock(owner: Any): Boolean
Checks mutex locked by owner
lock
Link copied to clipboard
common
abstract suspend fun lock(owner: Any? = null)
Locks this mutex, suspending caller while the mutex is locked.
tryLock
Link copied to clipboard
common
abstract fun tryLock(owner: Any? = null): Boolean
Tries to lock this mutex, returning false if this mutex is already locked.
unlock
Link copied to clipboard
common
abstract fun unlock(owner: Any? = null)
Unlocks this mutex.

Properties

isLocked
Link copied to clipboard
common
abstract val isLocked: Boolean
Returns true when this mutex is locked.
onLock
Link copied to clipboard
common
abstract val onLock: SelectClause2<Any?, Mutex>
Clause for select expression of lock suspending function that selects when the mutex is locked.

Extensions

withLock
Link copied to clipboard
common
inline suspend fun <T> Mutex.withLock(owner: Any? = null, action: () -> T): T
Executes the given action under this mutex's lock.

Sources

common source
Link copied to clipboard