Hello, is there a coroutine-friendly alternative to
ConcurrentHashMap
? The idea is that an update to the map via
compute
might be a suspending function, and the map should lock out the hash bucket being updated until that function has completed. I have this naive version which locks out the entire map:
class CoroutineMap<K : Any, V : Any> {
private val data: MutableMap<K, V> = mutableMapOf()
private val dataLock = Mutex()
suspend fun compute(key: K, update: suspend (V?) -> V?): V? = dataLock.withLock {
val updated = update(data[key])
if (updated == null) data.remove(key) else data[key] = updated
updated
}
}
but a version with per-bucket mutexes would be better, I believe?