From 5fbc1a132fddec064f16c6d1515520d8bdd63f3e Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 2 Aug 2024 23:58:04 +0600 Subject: [PATCH] improvements in SmartRWLocker and breaking of FullKeyValueCacheRepo tests --- .../kotlin/dev/inmo/micro_utils/coroutines/SmartRWLocker.kt | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/SmartRWLocker.kt b/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/SmartRWLocker.kt index 65eb1732bb6..faeea009ed9 100644 --- a/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/SmartRWLocker.kt +++ b/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/SmartRWLocker.kt @@ -13,7 +13,7 @@ import kotlin.contracts.contract * * [unlockWrite] will just unlock [writeMutex] */ 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) 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 */ suspend fun acquireRead() { - _writeMutex.waitUnlock() _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 */ suspend fun lockWrite() { - _writeMutex.lock() _readSemaphore.acquire(readPermits) + _writeMutex.lock() } /**