added more tools, start shifting to kotlin 2.2.0

This commit is contained in:
sergeych committed 2025-09-25 10:29:47 +04:00
1 parent a121f2c4a6
commit 01d9e0239e
7 files changed
+132 -8

No files matched your search

@@ -0,0 +1,29 @@
@file:Suppress("unused")
package net.sergecyh.diwan.tools
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
/**
* Experimental
*
* set of mutexes associated with keys `K`, so each key can have its own mutex
* used with [lock] or [withLock]
*/
class IndividualLock<K> {
private val access = Mutex()
private val locks = mutableMapOf<K, Mutex>()
suspend inline fun <T>withLock(key: K, block: ()->T): T = lock(key).use { block() }
suspend fun lock(key: K): AutoCloseable {
val m = access.withLock {
locks.getOrPut(key) { Mutex() }
}
m.lock()
return AutoCloseable { m.unlock() }
}
}