# SmartRWLocker

class SmartRWLocker (readPermits: Int = Int.MAX_VALUE, writeIsLocked: Boolean = false)(source)

Composite mutex which works with next rules:

- acquireRead require to writeMutex to be free. Then it will take one lock from readSemaphore

- releaseRead will just free up one permit in readSemaphore

- lockWrite will lock writeMutex and then await while all readSemaphore will be freed. If coroutine will be cancelled during read semaphore freeing, locking will be cancelled too with SmartMutex.Mutable.unlocking of writeMutex

- unlockWrite will just unlock writeMutex

## Constructors

SmartRWLocker
Link copied to clipboard
constructor(readPermits: Int = Int.MAX_VALUE, writeIsLocked: Boolean = false)

## Properties

readSemaphore
Link copied to clipboard
val readSemaphore : SmartSemaphore.Immutable
writeMutex
Link copied to clipboard
val writeMutex : SmartMutex.Immutable

## Functions

acquireRead
Link copied to clipboard
suspend fun acquireRead ()

Do lock in readSemaphore inside of writeMutex locking

lockWrite
Link copied to clipboard
suspend fun lockWrite ()

Locking writeMutex and wait while all readSemaphore permits will be freed

releaseRead
Link copied to clipboard
suspend fun releaseRead (): Boolean

Release one read permit in readSemaphore

unlockWrite
Link copied to clipboard
suspend fun unlockWrite (): Boolean

Unlock writeMutex

waitReadRelease
Link copied to clipboard
suspend fun SmartRWLocker . waitReadRelease (permits: Int = 1): Int

Will wait until the SmartSemaphore.permitsStateFlow of this instance will have permits count free permits.

waitWriteUnlock
Link copied to clipboard
suspend fun SmartRWLocker . waitWriteUnlock (): Boolean

Will wait until the SmartMutex.lockStateFlow of this instance will be false.

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

Will call SmartSemaphore.Mutable.lock, then execute action and return the result after SmartSemaphore.Mutable.unlock

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

Will call SmartMutex.Mutable.lock, then execute action and return the result after SmartMutex.Mutable.unlock