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() } /**