From cea53ec35e7a32e47820e993dea7112cc7c940e6 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Fri, 31 May 2024 10:44:35 +0700 Subject: [PATCH] Lil adjustments to delegate syncher --- gradle.properties | 2 +- .../dbotthepony/kommons/io/DelegateSyncher.kt | 21 +++++++++++-------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/gradle.properties b/gradle.properties index 964b92a..87aa714 100644 --- a/gradle.properties +++ b/gradle.properties @@ -4,7 +4,7 @@ kotlin.code.style=official specifyKotlinAsDependency=false projectGroup=ru.dbotthepony.kommons -projectVersion=3.0.0 +projectVersion=3.0.1 guavaDepVersion=33.0.0 gsonDepVersion=2.8.9 diff --git a/src/main/kotlin/ru/dbotthepony/kommons/io/DelegateSyncher.kt b/src/main/kotlin/ru/dbotthepony/kommons/io/DelegateSyncher.kt index 44f769c..fbcf4b2 100644 --- a/src/main/kotlin/ru/dbotthepony/kommons/io/DelegateSyncher.kt +++ b/src/main/kotlin/ru/dbotthepony/kommons/io/DelegateSyncher.kt @@ -39,7 +39,7 @@ import kotlin.concurrent.withLock * Values and delegates can be attached using [add], or by constructing subclassed directly. * Delta changes are tracked by [DelegateSyncher.Remote] instances. * - * In general, this class is not meant to be concurrently mutated by different threads, + * In general, this class is not meant to be _structurally_ concurrently mutated by different threads, * to avoid structure corruption a lock is employed. * * Attached delegates can be safely mutated by multiple threads concurrently @@ -630,7 +630,8 @@ class DelegateSyncher : Observer { * To get changes to be networked to remote client, [write] should be called. */ inner class Remote : Closeable { - private val isRemoved = AtomicBoolean() + @Volatile + private var isRemoved = false internal val dirty = ConcurrentLinkedQueue() private val remoteSlots = CopyOnWriteArrayList() @@ -647,7 +648,7 @@ class DelegateSyncher : Observer { } fun markDirty() { - if (!isRemoved.get() && isDirty.compareAndSet(false, true)) { + if (!isRemoved && isDirty.compareAndSet(false, true)) { dirty.add(this) } } @@ -819,14 +820,16 @@ class DelegateSyncher : Observer { } override fun close() { - if (isRemoved.compareAndSet(false, true)) { + if (!isRemoved) { lock.withLock { - remoteSlots.forEach { - it.remove() - it.parent.remoteSlots.remove(it) - } + if (!isRemoved) { + remoteSlots.forEach { + it.remove() + it.parent.remoteSlots.remove(it) + } - remotes.remove(this) + remotes.remove(this) + } } dirty.clear()