improvements in SmartRWLocker and breaking of FullKeyValueCacheRepo tests

This commit is contained in:
InsanusMokrassar 2024-08-02 23:58:04 +06:00
parent 16f2ca2cde
commit 95d2f6de01

View File

@ -13,7 +13,7 @@ import kotlin.contracts.contract
* * [unlockWrite] will just unlock [writeMutex] * * [unlockWrite] will just unlock [writeMutex]
*/ */
class SmartRWLocker(private val readPermits: Int = Int.MAX_VALUE, writeIsLocked: Boolean = false) { class SmartRWLocker(private val readPermits: Int = Int.MAX_VALUE, writeIsLocked: Boolean = false) {
private val _readSemaphore = SmartSemaphore.Mutable(permits = readPermits, acquiredPermits = 0) private val _readSemaphore = SmartSemaphore.Mutable(permits = readPermits, acquiredPermits = if (writeIsLocked) readPermits else 0)
private val _writeMutex = SmartMutex.Mutable(locked = writeIsLocked) private val _writeMutex = SmartMutex.Mutable(locked = writeIsLocked)
val readSemaphore: SmartSemaphore.Immutable = _readSemaphore.immutable() val readSemaphore: SmartSemaphore.Immutable = _readSemaphore.immutable()
@ -23,7 +23,6 @@ class SmartRWLocker(private val readPermits: Int = Int.MAX_VALUE, writeIsLocked:
* Do lock in [readSemaphore] inside of [writeMutex] locking * Do lock in [readSemaphore] inside of [writeMutex] locking
*/ */
suspend fun acquireRead() { suspend fun acquireRead() {
_writeMutex.waitUnlock()
_readSemaphore.acquire() _readSemaphore.acquire()
} }
@ -38,8 +37,8 @@ class SmartRWLocker(private val readPermits: Int = Int.MAX_VALUE, writeIsLocked:
* Locking [writeMutex] and wait while all [readSemaphore] permits will be freed * Locking [writeMutex] and wait while all [readSemaphore] permits will be freed
*/ */
suspend fun lockWrite() { suspend fun lockWrite() {
_writeMutex.lock()
_readSemaphore.acquire(readPermits) _readSemaphore.acquire(readPermits)
_writeMutex.lock()
} }
/** /**