35 lines
870 B
Kotlin
35 lines
870 B
Kotlin
/*
|
|
* Copyright (c) 2025. Sergey S. Chernov - All Rights Reserved
|
|
*
|
|
* You may use, distribute and modify this code under the
|
|
* terms of the private license, which you must obtain from the author
|
|
*
|
|
* To obtain the license, contact the author: https://t.me/real_sergeych or email to
|
|
* real dot sergeych at gmail.
|
|
*/
|
|
|
|
package net.sergeych.kiloparsec
|
|
|
|
import kotlinx.coroutines.sync.Mutex
|
|
import kotlinx.coroutines.sync.withLock
|
|
|
|
fun String.encodeToUByteArray() =
|
|
encodeToByteArray().toUByteArray()
|
|
|
|
class SyncValue<T>(initialValue: T) {
|
|
private val access = Mutex()
|
|
|
|
var value = initialValue
|
|
private set
|
|
|
|
suspend fun mutate(f: suspend (T)->T): T = access.withLock { f(value).also { value = it } }
|
|
|
|
@Suppress("unused")
|
|
suspend fun getAndSet(newValue: T): T = mutate {
|
|
val old = value
|
|
value = newValue
|
|
old
|
|
}
|
|
|
|
}
|