now SmartMutex and SmartSemaphore (and SmartRWLocker as a result) works on SpecialMutableStateFlow

This commit is contained in:
InsanusMokrassar 2024-05-12 21:44:33 +06:00
parent 4901a8844c
commit e75125f6df
3 changed files with 6 additions and 2 deletions

View File

@ -44,7 +44,7 @@ sealed interface SmartMutex {
* @param locked Preset state of [isLocked] and its internal [_lockStateFlow] * @param locked Preset state of [isLocked] and its internal [_lockStateFlow]
*/ */
class Mutable(locked: Boolean = false) : SmartMutex { class Mutable(locked: Boolean = false) : SmartMutex {
private val _lockStateFlow = MutableStateFlow<Boolean>(locked) private val _lockStateFlow = SpecialMutableStateFlow<Boolean>(locked)
override val lockStateFlow: StateFlow<Boolean> = _lockStateFlow.asStateFlow() override val lockStateFlow: StateFlow<Boolean> = _lockStateFlow.asStateFlow()
private val internalChangesMutex = Mutex() private val internalChangesMutex = Mutex()

View File

@ -45,7 +45,7 @@ sealed interface SmartSemaphore {
* @param locked Preset state of [freePermits] and its internal [_freePermitsStateFlow] * @param locked Preset state of [freePermits] and its internal [_freePermitsStateFlow]
*/ */
class Mutable(private val permits: Int, acquiredPermits: Int = 0) : SmartSemaphore { class Mutable(private val permits: Int, acquiredPermits: Int = 0) : SmartSemaphore {
private val _freePermitsStateFlow = MutableStateFlow<Int>(permits - acquiredPermits) private val _freePermitsStateFlow = SpecialMutableStateFlow<Int>(permits - acquiredPermits)
override val permitsStateFlow: StateFlow<Int> = _freePermitsStateFlow.asStateFlow() override val permitsStateFlow: StateFlow<Int> = _freePermitsStateFlow.asStateFlow()
private val internalChangesMutex = Mutex(false) private val internalChangesMutex = Mutex(false)

View File

@ -44,6 +44,10 @@ open class SpecialMutableStateFlow<T>(
override val subscriptionCount: StateFlow<Int> override val subscriptionCount: StateFlow<Int>
get() = sharingFlow.subscriptionCount get() = sharingFlow.subscriptionCount
init {
sharingFlow.tryEmit(initialValue)
}
override fun compareAndSet(expect: T, update: T): Boolean { override fun compareAndSet(expect: T, update: T): Boolean {
if (expect == value) { if (expect == value) {
value = update value = update