ReadWriteMutex

interface ReadWriteMutex

A writer-preferred non-reentrant read-write mutual exclusion lock.

This mutex allows multiple readers or a single writer to acquire the lock at the same time. It is non-reentrant for both readers (See below) and writers. It only supports ownership tracking for writeMutex.

If any coroutine calls writeMutex's Mutex.lock while the lock is already held by one or more readers, concurrent calls to readMutex's Mutex.lock will block until the writer has acquired (and released) the lock, to ensure that the lock eventually becomes available to the writer. Note that this prohibits recursive read-locking.

It is the responsibility of the caller to ensure that the mutex is unlocked by the same coroutine that locked it, and that it is not over-unlocked.

Types

Link copied to clipboard
value class State

A snapshot of a read-write mutex's state.

Properties

Link copied to clipboard
abstract val readMutex: Mutex

A mutex to be used by readers.

Link copied to clipboard

A snapshot of the state of the read-write mutex.

Link copied to clipboard
abstract val writeMutex: Mutex

A mutex to be used by writers.

Functions

Link copied to clipboard

Shortcut for ReadWriteMutex.writeMutex's Mutex.holdsLock.

Link copied to clipboard
suspend fun ReadWriteMutex.readLock()

Shortcut for ReadWriteMutex.readMutex's Mutex.lock.

Link copied to clipboard

Shortcut for ReadWriteMutex.readMutex's Mutex.unlock.

Link copied to clipboard

Shortcut for ReadWriteMutex.readMutex's Mutex.tryLock.

Link copied to clipboard
fun ReadWriteMutex.tryWriteLock(owner: Any? = null): Boolean

Shortcut for ReadWriteMutex.writeMutex's Mutex.tryLock.

Link copied to clipboard
inline suspend fun <T> ReadWriteMutex.withReadLock(action: () -> T): T

Shortcut for ReadWriteMutex.readMutex's Mutex.holdsLock.

Link copied to clipboard
inline suspend fun <T> ReadWriteMutex.withWriteLock(owner: Any? = null, action: () -> T): T

Shortcut for ReadWriteMutex.writeMutex's Mutex.withLock.

Link copied to clipboard
suspend fun ReadWriteMutex.writeLock(owner: Any? = null)

Shortcut for ReadWriteMutex.writeMutex's Mutex.lock.

Link copied to clipboard
fun ReadWriteMutex.writeUnlock(owner: Any? = null)

Shortcut for ReadWriteMutex.writeMutex's Mutex.unlock.