Skip to content

Commit

Permalink
refactor: Swap atomifcfu for touchlab/Stately due to JVM 21 issues
Browse files Browse the repository at this point in the history
  • Loading branch information
0ffz committed May 2, 2024
1 parent 9a15265 commit 9db7402
Show file tree
Hide file tree
Showing 11 changed files with 38 additions and 41 deletions.
3 changes: 0 additions & 3 deletions addons/geary-prefabs/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ plugins {
}

kotlin {
targets {

}
sourceSets {
val commonMain by getting {
dependencies {
Expand Down
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ allprojects {
pluginManager.withPlugin("org.jetbrains.kotlin.multiplatform") {
kotlin {
jvm {
withJava()
testRuns["test"].executionTask.configure {
useJUnitPlatform()
}
Expand Down
2 changes: 1 addition & 1 deletion geary-benchmarks/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import org.jetbrains.kotlin.allopen.gradle.AllOpenExtension
plugins {
id(idofrontLibs.plugins.mia.kotlin.jvm.get().pluginId)
id("org.jetbrains.kotlinx.benchmark") version "0.4.9"
kotlin("plugin.allopen") version "1.9.10"
kotlin("plugin.allopen") version idofrontLibs.versions.kotlin.get()
}

configure<AllOpenExtension> {
Expand Down
8 changes: 1 addition & 7 deletions geary-core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,11 @@ plugins {
alias(idofrontLibs.plugins.kotlinx.serialization)
}

buildscript {
dependencies {
classpath("org.jetbrains.kotlinx:atomicfu-gradle-plugin:${libs.versions.atomicfu.get()}")
}
}

kotlin {
sourceSets {
val commonMain by getting {
dependencies {
implementation(libs.atomicfu)
implementation(libs.stately.concurrency)
implementation(libs.androidx.collection)
implementation(idofrontLibs.kotlin.reflect)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ package com.mineinabyss.geary.datatypes

import com.mineinabyss.geary.helpers.toGeary

class EntityStack(private val stack: ArrayDeque<Long> = ArrayDeque()) {
class EntityStack(
@PublishedApi
internal val stack: ArrayDeque<Long> = ArrayDeque()
) {
fun push(entity: Entity) {
stack.add(entity.id.toLong())
}

fun pop(): Entity? =
if (stack.isEmpty()) null
inline fun popOrElse(orElse: () -> Entity): Entity =
if (stack.isEmpty()) orElse()
else stack.removeFirst().toGeary()
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
package com.mineinabyss.geary.datatypes.maps

import co.touchlab.stately.concurrency.Synchronizable
import co.touchlab.stately.concurrency.synchronize
import com.mineinabyss.geary.datatypes.Entity
import com.mineinabyss.geary.engine.archetypes.Archetype
import kotlinx.atomicfu.locks.SynchronizedObject
import kotlinx.atomicfu.locks.synchronized

class SynchronizedArrayTypeMap : ArrayTypeMap() {
private val lock = SynchronizedObject()
private val lock = Synchronizable()

override fun getArchAndRow(entity: Entity): ULong {
return synchronized(lock) { super.getArchAndRow(entity) }
return lock.synchronize { super.getArchAndRow(entity) }
}

override fun set(entity: Entity, archetype: Archetype, row: Int) {
synchronized(lock) { super.set(entity, archetype, row) }
lock.synchronize { super.set(entity, archetype, row) }
}

override fun remove(entity: Entity) = synchronized(lock) { super.remove(entity) }
override fun contains(entity: Entity): Boolean = synchronized(lock) { super.contains(entity) }
override fun remove(entity: Entity) = lock.synchronize { super.remove(entity) }
override fun contains(entity: Entity): Boolean = lock.synchronize { super.contains(entity) }
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.mineinabyss.geary.engine.archetypes

import co.touchlab.stately.concurrency.Synchronizable
import co.touchlab.stately.concurrency.synchronize
import com.mineinabyss.geary.datatypes.Entity
import com.mineinabyss.geary.datatypes.family.Family
import com.mineinabyss.geary.datatypes.maps.Family2ObjectArrayMap
Expand All @@ -8,8 +10,6 @@ import com.mineinabyss.geary.helpers.contains
import com.mineinabyss.geary.helpers.fastForEach
import com.mineinabyss.geary.systems.query.CachedQuery
import com.mineinabyss.geary.systems.query.Query
import kotlinx.atomicfu.locks.SynchronizedObject
import kotlinx.atomicfu.locks.synchronized

class ArchetypeQueryManager : QueryManager {
private val queries = mutableListOf<CachedQuery<*>>()
Expand All @@ -19,7 +19,7 @@ class ArchetypeQueryManager : QueryManager {
setIndex = { it, index -> it.id = index }
)

private val archetypeRegistryLock = SynchronizedObject()
private val archetypeRegistryLock = Synchronizable()

val archetypeCount get() = archetypes.elements.size

Expand All @@ -32,13 +32,13 @@ class ArchetypeQueryManager : QueryManager {
return queryRunner
}

internal fun registerArchetype(archetype: Archetype) = synchronized(archetypeRegistryLock) {
internal fun registerArchetype(archetype: Archetype) = archetypeRegistryLock.synchronize {
archetypes.add(archetype, archetype.type)
val matched = queries.filter { archetype.type in it.family }
matched.fastForEach { it.matchedArchetypes += archetype }
}

internal fun unregisterArchetype(archetype: Archetype) = synchronized(archetypeRegistryLock) {
internal fun unregisterArchetype(archetype: Archetype) = archetypeRegistryLock.synchronize {
archetypes.remove(archetype)
val matched = queries.filter { archetype.type in it.family }
matched.fastForEach { it.matchedArchetypes -= archetype }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package com.mineinabyss.geary.engine.archetypes

import co.touchlab.stately.concurrency.Synchronizable
import co.touchlab.stately.concurrency.synchronize
import com.mineinabyss.geary.components.ComponentInfo
import com.mineinabyss.geary.datatypes.ComponentId
import com.mineinabyss.geary.engine.ComponentProvider
import com.mineinabyss.geary.modules.geary
import kotlinx.atomicfu.locks.SynchronizedObject
import kotlinx.atomicfu.locks.synchronized
import kotlin.reflect.KClassifier

class ComponentAsEntityProvider : ComponentProvider {
private val entityProvider get() = geary.entityProvider
private val logger get() = geary.logger

private val classToComponentMap = mutableMapOf<KClassifier, Long>()
private val classToComponentMapLock = SynchronizedObject()
private val classToComponentMapLock = Synchronizable()

internal fun createComponentInfo() {
logger.v("Registering ComponentInfo component")
Expand All @@ -24,11 +24,11 @@ class ComponentAsEntityProvider : ComponentProvider {
}

override fun getOrRegisterComponentIdForClass(kClass: KClassifier): ComponentId =
synchronized(classToComponentMapLock) {
classToComponentMapLock.synchronize {
val id = classToComponentMap.getOrElse(kClass) {
return registerComponentIdForClass(kClass)
return@synchronize registerComponentIdForClass(kClass)
}
return id.toULong()
return@synchronize id.toULong()
}

private fun registerComponentIdForClass(kClass: KClassifier): ComponentId {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.mineinabyss.geary.engine.archetypes

import co.touchlab.stately.concurrency.AtomicLong
import com.mineinabyss.geary.datatypes.Entity
import com.mineinabyss.geary.datatypes.EntityStack
import com.mineinabyss.geary.datatypes.EntityType
Expand All @@ -9,7 +10,6 @@ import com.mineinabyss.geary.engine.EntityProvider
import com.mineinabyss.geary.helpers.*
import com.mineinabyss.geary.modules.geary
import com.mineinabyss.geary.observers.events.OnEntityRemoved
import kotlinx.atomicfu.atomic

class EntityByArchetypeProvider(
private val reuseIDsAfterRemoval: Boolean = true,
Expand All @@ -20,12 +20,12 @@ class EntityByArchetypeProvider(
private lateinit var root: Archetype

private val removedEntities: EntityStack = EntityStack()
private val currId = atomic(0L)
private val currId = AtomicLong(0L)

override fun create(): GearyEntity {
val entity: GearyEntity = if (reuseIDsAfterRemoval) {
removedEntities.pop() ?: currId.getAndIncrement().toGeary()
} else currId.getAndIncrement().toGeary()
removedEntities.popOrElse { (currId.incrementAndGet() - 1).toGeary() }
} else (currId.incrementAndGet() - 1).toGeary()

createRecord(entity)
return entity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package com.mineinabyss.geary.engine.archetypes

import androidx.collection.getOrElse
import androidx.collection.set
import co.touchlab.stately.concurrency.Synchronizable
import co.touchlab.stately.concurrency.synchronize
import com.mineinabyss.geary.datatypes.ComponentId
import com.mineinabyss.geary.datatypes.EntityType
import com.mineinabyss.geary.modules.archetypes
import kotlinx.atomicfu.locks.SynchronizedObject
import kotlinx.atomicfu.locks.synchronized

class SimpleArchetypeProvider : ArchetypeProvider {
private val queryManager: ArchetypeQueryManager get() = archetypes.queryManager
Expand All @@ -17,7 +17,7 @@ class SimpleArchetypeProvider : ArchetypeProvider {
}
}

private val archetypeWriteLock = SynchronizedObject()
private val archetypeWriteLock = Synchronizable()


private fun createArchetype(prevNode: Archetype, componentEdge: ComponentId): Archetype {
Expand All @@ -29,11 +29,11 @@ class SimpleArchetypeProvider : ArchetypeProvider {
return arc
}

override fun getArchetype(entityType: EntityType): Archetype = synchronized(archetypeWriteLock) {
override fun getArchetype(entityType: EntityType): Archetype = archetypeWriteLock.synchronize {
var node = rootArchetype
entityType.forEach { compId ->
node = node.componentAddEdges.getOrElse(compId.toLong()) { createArchetype(node, compId) }
}
return node
return@synchronize node
}
}
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ roaringbitmap = "org.roaringbitmap:RoaringBitmap:0.9.32"
bitvector = "net.onedaybeard.bitvector:bitvector-js:0.1.4"
kermit = "co.touchlab:kermit:1.2.2"
androidx-collection = "androidx.collection:collection:1.4.0"
stately-concurrency = "co.touchlab:stately-concurrency:2.0.7"

0 comments on commit 9db7402

Please sign in to comment.