All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.ch.softappeal.yass2.remote.session.Sync.kt Maven / Gradle / Ivy

There is a newer version: 5.0.0
Show newest version
package ch.softappeal.yass2.remote.session

import kotlinx.coroutines.sync.*

internal class AtomicBoolean(private var value: Boolean) {
    private val mutex = Mutex()
    suspend fun get(): Boolean = mutex.withLock { value }
    suspend fun getAndSet(value: Boolean): Boolean = mutex.withLock {
        val oldValue = this.value
        this.value = value
        oldValue
    }
}

internal class AtomicInteger(private var value: Int) {
    private val mutex = Mutex()
    suspend fun incrementAndGet(): Int = mutex.withLock { ++value }
}

internal class ThreadSafeMap(initialCapacity: Int) {
    private val mutex = Mutex()
    private val map = HashMap(initialCapacity)
    suspend fun put(key: K, value: V): Unit = mutex.withLock { map[key] = value }
    suspend fun remove(key: K): V? = mutex.withLock { map.remove(key) }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy