From a4b8d21775f34ef964b8975961a554cf3dce5145 Mon Sep 17 00:00:00 2001 From: Davide Angelocola Date: Wed, 18 Nov 2026 07:42:33 +0100 Subject: [PATCH] NativeObject holds its pointer in an AtomicReference and immortal in an AtomicBoolean Replaces the volatile field plus VarHandle (Sonar S3077) with plain atomics; the cost is two small allocations per wrapper. Co-Authored-By: Claude Sonnet 5 --- .../github/dfa1/rocksdbffm/NativeObject.java | 40 +++++++------------ .../dfa1/rocksdbffm/NativeObjectParent.java | 4 +- 2 files changed, 17 insertions(+), 27 deletions(-) diff --git a/core/src/main/java/io/github/dfa1/rocksdbffm/NativeObject.java b/core/src/main/java/io/github/dfa1/rocksdbffm/NativeObject.java index 77d2ea91..1e07d236 100644 --- a/core/src/main/java/io/github/dfa1/rocksdbffm/NativeObject.java +++ b/core/src/main/java/io/github/dfa1/rocksdbffm/NativeObject.java @@ -1,12 +1,12 @@ package io.github.dfa1.rocksdbffm; import java.lang.foreign.MemorySegment; -import java.lang.invoke.MethodHandles; -import java.lang.invoke.VarHandle; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicReference; /// Base class for all Java wrappers around native RocksDB pointers. /// -/// Holds the native pointer in a volatile field. On [#close()], +/// Holds the native pointer in an [AtomicReference]. On [#close()], /// it is atomically swapped to [MemorySegment#NULL], ensuring /// [#tryClose(MemorySegment)] is called exactly once even under /// concurrent or repeated close() calls. A `close()` that loses that race returns at once, possibly @@ -23,27 +23,17 @@ public abstract class NativeObject implements AutoCloseable { private static final System.Logger LOG = System.getLogger(NativeObject.class.getName()); - private static final VarHandle OWNING_POINTER; - - static { - try { - OWNING_POINTER = MethodHandles.lookup().findVarHandle(NativeObject.class, "owningPointer", MemorySegment.class); - } catch (ReflectiveOperationException e) { - throw new ExceptionInInitializerError(e); - } - } - - private volatile MemorySegment owningPointer; + private final AtomicReference owningPointer; /// Set by [#makeImmortal()] and never cleared. Usually set before the object is published (a `DEFAULTS` field), - /// but a [CompactionFilter] sets it on every attach, hence `volatile`. - private volatile boolean immortal; + /// but a [CompactionFilter] sets it on every attach, hence atomic. + private final AtomicBoolean immortal = new AtomicBoolean(); /// Initializes this wrapper with the given native pointer. /// /// @param owningPointer the non-NULL native pointer this object now owns protected NativeObject(MemorySegment owningPointer) { - this.owningPointer = owningPointer; + this.owningPointer = new AtomicReference<>(owningPointer); } /// Returns the current native pointer. @@ -51,7 +41,7 @@ protected NativeObject(MemorySegment owningPointer) { /// @return the live native pointer /// @throws IllegalStateException if this object has been closed or its ownership transferred public final MemorySegment ptr() { - MemorySegment p = owningPointer; + MemorySegment p = owningPointer.get(); if (MemorySegment.NULL.equals(p)) { throw new IllegalStateException("native object is closed or ownership has been transferred"); } @@ -67,7 +57,7 @@ public final MemorySegment ptr() { /// /// @return `true` if [#ptr()] would currently throw final boolean isClosed() { - return MemorySegment.NULL.equals(owningPointer); + return MemorySegment.NULL.equals(owningPointer.get()); } /// Releases the native resource, unless this object is immortal. A wrapper that is released by its owner and @@ -79,7 +69,7 @@ final boolean isClosed() { /// would otherwise deadlock. @Override public void close() { - if (immortal || isClosed()) { + if (immortal.get() || isClosed()) { return; } release(); @@ -87,7 +77,7 @@ public void close() { /// @return `true` if [#makeImmortal()] was called, so [#close()] does nothing final boolean isImmortal() { - return immortal; + return immortal.get(); } /// Swaps the pointer to NULL and, if this call won that swap, runs [#tryClose(MemorySegment)], logging @@ -95,7 +85,7 @@ final boolean isImmortal() { /// an owner that frees an object whose `close()` is a no-op. A [NativeObjectChild] overrides it to add the /// monitor a parent's sweep waits on, and its unregistration from the parents. void release() { - MemorySegment ptr = (MemorySegment) OWNING_POINTER.getAndSet(this, MemorySegment.NULL); + MemorySegment ptr = owningPointer.getAndSet(MemorySegment.NULL); if (!MemorySegment.NULL.equals(ptr)) { try { tryClose(ptr); @@ -145,7 +135,7 @@ void release() { /// @return this object, so it chains off the factory: `static final X DEFAULTS = newX().makeImmortal();` @SuppressWarnings("unchecked") T makeImmortal() { - this.immortal = true; + immortal.set(true); return (T) this; } @@ -164,9 +154,9 @@ T makeImmortal() { /// already provides: the pointer is dropped and further use throws [IllegalStateException] /// instead of double-freeing or touching freed memory. void transferOwnership() { - if (immortal) { + if (immortal.get()) { throw new IllegalStateException("immortal object cannot transfer ownership: it is shared process-wide"); } - owningPointer = MemorySegment.NULL; + owningPointer.set(MemorySegment.NULL); } } diff --git a/core/src/main/java/io/github/dfa1/rocksdbffm/NativeObjectParent.java b/core/src/main/java/io/github/dfa1/rocksdbffm/NativeObjectParent.java index b7a3db90..d3bfc6b0 100644 --- a/core/src/main/java/io/github/dfa1/rocksdbffm/NativeObjectParent.java +++ b/core/src/main/java/io/github/dfa1/rocksdbffm/NativeObjectParent.java @@ -20,7 +20,7 @@ /// closes, racing its own constructor's `registerChild` call against the one-shot sweep. Rather /// than a lock around the check, `registerChild` inserts optimistically and validates /// afterward, undoing if the validation says it shouldn't have happened — an "insert, recheck, -/// compensate" pattern that needs nothing beyond [NativeObject]'s volatile pointer (the recheck reads it through +/// compensate" pattern that needs nothing beyond [NativeObject]'s `AtomicReference` pointer (the recheck reads it through /// [NativeObject#isClosed()]) and [ConcurrentHashMap]'s own atomic add/remove. abstract class NativeObjectParent extends NativeObjectChild { @@ -70,7 +70,7 @@ protected NativeObjectParent(MemorySegment ptr) { /// Both outcomes of a "child visited by the sweep" and "child abandoned here" landing on the /// very same instance at once are safe regardless of which wins: [NativeObject#close()] and /// [NativeObject#transferOwnership()] both just (idempotently) null the same - /// pointer field, so whichever runs second is a no-op. + /// `AtomicReference`, so whichever runs second is a no-op. /// /// @param child the child resource to close alongside (and before) this object final void registerChild(NativeObjectChild child) {