Skip to content

Commit

Permalink
Use ConcurrentHashMap for JVM target (#39)
Browse files Browse the repository at this point in the history
* Use `ConcurrentHashMap` for JVM target.
* Fix `nonJvm` and `jvmLincheck` sourcesets.
  • Loading branch information
ychescale9 authored Feb 23, 2024
1 parent d31c054 commit 86aa406
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 20 deletions.
1 change: 0 additions & 1 deletion build-logic/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,3 @@ org.gradle.configuration-cache=true
org.gradle.configuration-cache.problems=warn

kotlin.code.style=official
kotlin.experimental.tryK2=true
Original file line number Diff line number Diff line change
Expand Up @@ -182,23 +182,20 @@ private fun KotlinMultiplatformExtension.configureTargets(project: Project) {
mingwX64()
applyDefaultHierarchyTemplate()

@Suppress("UnusedPrivateProperty")
sourceSets {
val jvmAndIos by creating {
val nonJvmMain by creating {
dependsOn(commonMain.get())
}
iosMain.get().dependsOn(jvmAndIos)
jvmMain.get().dependsOn(jvmAndIos)

val nonJvm by creating {
dependsOn(commonMain.get())
val jvmLincheck by getting {
dependsOn(jvmMain.get())
}
jsMain.get().dependsOn(nonJvm)
@Suppress("UnusedPrivateProperty")
jsMain.get().dependsOn(nonJvmMain)
val wasmJsMain by getting {
dependsOn(nonJvm)
dependsOn(nonJvmMain)
}
appleMain.get().dependsOn(nonJvm)
linuxMain.get().dependsOn(nonJvm)
mingwMain.get().dependsOn(nonJvm)
appleMain.get().dependsOn(nonJvmMain)
linuxMain.get().dependsOn(nonJvmMain)
mingwMain.get().dependsOn(nonJvmMain)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package io.github.reactivecircus.cache4k

internal expect class ConcurrentMutableMap<Key : Any, Value : Any>() {
val size: Int
val values: Collection<Value>
operator fun get(key: Key): Value?
fun put(key: Key, value: Value): Value?
fun remove(key: Key): Value?
fun clear()
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.github.reactivecircus.cache4k

import co.touchlab.stately.collections.IsoMutableMap
import kotlinx.atomicfu.locks.reentrantLock
import kotlinx.atomicfu.locks.withLock
import kotlinx.coroutines.sync.Mutex
Expand All @@ -11,7 +10,7 @@ import kotlinx.coroutines.sync.withLock
*/
internal class KeyedSynchronizer<Key : Any> {

private val keyBasedMutexes = IsoMutableMap<Key, MutexEntry>()
private val keyBasedMutexes = ConcurrentMutableMap<Key, MutexEntry>()

private val mapLock = reentrantLock()

Expand Down Expand Up @@ -40,7 +39,7 @@ internal class KeyedSynchronizer<Key : Any> {
mutexEntry.counter++
// save the lock entry to the map if it has just been created
if (keyBasedMutexes[key] == null) {
keyBasedMutexes[key] = mutexEntry
keyBasedMutexes.put(key, mutexEntry)
}

return mutexEntry.mutex
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.github.reactivecircus.cache4k

import co.touchlab.stately.collections.IsoMutableMap
import co.touchlab.stately.collections.IsoMutableSet
import kotlinx.atomicfu.AtomicRef
import kotlinx.atomicfu.atomic
Expand Down Expand Up @@ -38,7 +37,7 @@ internal class RealCache<Key : Any, Value : Any>(
private val eventListener: CacheEventListener<Key, Value>?,
) : Cache<Key, Value> {

private val cacheEntries = IsoMutableMap<Key, CacheEntry<Key, Value>>()
private val cacheEntries = ConcurrentMutableMap<Key, CacheEntry<Key, Value>>()

/**
* Whether to perform size based evictions.
Expand Down Expand Up @@ -138,7 +137,7 @@ internal class RealCache<Key : Any, Value : Any>(
writeTimeMark = atomic(nowTimeMark),
)
recordWrite(newEntry)
cacheEntries[key] = newEntry
cacheEntries.put(key, newEntry)
}
onEvent(
oldValue?.let {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.github.reactivecircus.cache4k

import java.util.concurrent.ConcurrentHashMap

internal actual class ConcurrentMutableMap<Key : Any, Value : Any> {
private val map = ConcurrentHashMap<Key, Value>()

actual val size: Int get() = map.size

actual val values: Collection<Value> get() = map.values

actual operator fun get(key: Key): Value? = map[key]

actual fun put(key: Key, value: Value): Value? = map.put(key, value)

actual fun remove(key: Key): Value? = map.remove(key)

actual fun clear() = map.clear()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.github.reactivecircus.cache4k

import co.touchlab.stately.collections.IsoMutableMap

internal actual class ConcurrentMutableMap<Key : Any, Value : Any> {
private val map = IsoMutableMap<Key, Value>()

actual val size: Int get() = map.size

actual val values: Collection<Value> get() = map.values

actual operator fun get(key: Key): Value? = map[key]

actual fun put(key: Key, value: Value): Value? = map.put(key, value)

actual fun remove(key: Key): Value? = map.remove(key)

actual fun clear() = map.clear()
}
1 change: 0 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,3 @@ org.gradle.configureondemand=true
org.gradle.caching=true

kotlin.code.style=official
kotlin.experimental.tryK2=false

0 comments on commit 86aa406

Please sign in to comment.