Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 15 additions & 25 deletions core/src/main/java/io/github/dfa1/rocksdbffm/NativeObject.java
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -23,35 +23,25 @@ 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<MemorySegment> 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.
///
/// @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");
}
Expand All @@ -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
Expand All @@ -79,23 +69,23 @@ final boolean isClosed() {
/// would otherwise deadlock.
@Override
public void close() {
if (immortal || isClosed()) {
if (immortal.get() || isClosed()) {
return;
}
release();
}

/// @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
/// instead of throwing. Idempotent and never throws. Whatever [#close()] would otherwise do: the entry point for
/// 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);
Expand Down Expand Up @@ -145,7 +135,7 @@ void release() {
/// @return this object, so it chains off the factory: `static final X DEFAULTS = newX().makeImmortal();`
@SuppressWarnings("unchecked")
<T extends NativeObject> T makeImmortal() {
this.immortal = true;
immortal.set(true);
return (T) this;
}

Expand All @@ -164,9 +154,9 @@ <T extends NativeObject> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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) {
Expand Down
Loading