From e0f46a039444d7e2896f76e299a7e190391b6787 Mon Sep 17 00:00:00 2001 From: Gleb Minaev <43728100+lounres@users.noreply.github.com> Date: Fri, 9 Oct 2026 20:18:40 +0300 Subject: [PATCH] Add requirements on constructor arguments of `SmartSemaphore`, `SmartRWLocker`, and `SmartKeyRWLocker`. Correct docs of `SmartSemaphore.freePermits`. --- .../inmo/micro_utils/coroutines/SmartKeyRWLocker.kt | 5 +++++ .../dev/inmo/micro_utils/coroutines/SmartRWLocker.kt | 4 ++++ .../inmo/micro_utils/coroutines/SmartSemaphore.kt | 12 ++++++++++-- 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/SmartKeyRWLocker.kt b/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/SmartKeyRWLocker.kt index 1f92ed2651b..e29ce3d077b 100644 --- a/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/SmartKeyRWLocker.kt +++ b/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/SmartKeyRWLocker.kt @@ -28,6 +28,11 @@ class SmartKeyRWLocker( globalLockerWriteIsLocked: Boolean = false, private val perKeyReadPermits: Int = Int.MAX_VALUE ) { + init { + require(globalLockerReadPermits > 0) { "The maximal number 'globalLockerReadPermits' of global permits on reading ($globalLockerReadPermits) must be positive." } + require(perKeyReadPermits > 0) { "The maximal number 'perKeyReadPermits' of permits on reading per key ($perKeyReadPermits) must be positive." } + } + private val globalRWLocker: SmartRWLocker = SmartRWLocker( readPermits = globalLockerReadPermits, writeIsLocked = globalLockerWriteIsLocked 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 f1f74e7b59f..79d925f2752 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 @@ -17,6 +17,10 @@ import kotlin.contracts.contract * * [unlockWrite] will just unlock [writeMutex] */ class SmartRWLocker(private val readPermits: Int = Int.MAX_VALUE, writeIsLocked: Boolean = false) { + init { + require(readPermits > 0) { "The maximal number 'readPermits' of permits on reading ($readPermits) must be positive." } + } + private val _readSemaphore = SmartSemaphore.Mutable(permits = readPermits, acquiredPermits = if (writeIsLocked) readPermits else 0) private val _writeMutex = SmartMutex.Mutable(locked = writeIsLocked) diff --git a/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/SmartSemaphore.kt b/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/SmartSemaphore.kt index 43f0550e4fc..b918a6f1e34 100644 --- a/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/SmartSemaphore.kt +++ b/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/SmartSemaphore.kt @@ -29,8 +29,10 @@ sealed interface SmartSemaphore { val permitsStateFlow: StateFlow /** - * * True - locked - * * False - unlocked + * The number of the available permits. + * + * If returns `0`, it is fully released (unlocked). + * If it returns the same value as `maxPermits`, it is fully acquired (locked). */ val freePermits: Int get() = permitsStateFlow.value @@ -47,6 +49,12 @@ sealed interface SmartSemaphore { * @param locked Preset state of [freePermits] and its internal [_freePermitsStateFlow] */ class Mutable(permits: Int, acquiredPermits: Int = 0) : SmartSemaphore { + init { + require(permits > 0) { "The maximal number 'permits' of permits ($permits) must be positive." } + require(acquiredPermits >= 0) { "The number 'acquiredPermits' of initially acquired permits ($acquiredPermits) must be non-negative." } + require(permits >= acquiredPermits) { "The number 'acquiredPermits' of acquired initially permits ($acquiredPermits) must not exceed the maximal number 'permits' of permits ($permits)." } + } + override val maxPermits: Int = permits private val _freePermitsStateFlow = MutableRedeliverStateFlow(permits - acquiredPermits) override val permitsStateFlow: StateFlow = _freePermitsStateFlow.asStateFlow()