Skip to content

Commit

Permalink
Allow reaper threads to be started at run time
Browse files Browse the repository at this point in the history
Signed-off-by: David M. Lloyd <david.lloyd@redhat.com>
  • Loading branch information
dmlloyd committed Aug 30, 2023
1 parent 01324fc commit 9a2b466
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 15 deletions.
4 changes: 4 additions & 0 deletions ref/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
<groupId>${project.groupId}</groupId>
<artifactId>smallrye-common-constraint</artifactId>
</dependency>
<dependency>
<groupId>org.graalvm.sdk</groupId>
<artifactId>graal-sdk</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public PhantomReference(final T referent, final A attachment, final ReferenceQue
* @param reaper the reaper to use
*/
public PhantomReference(final T referent, final A attachment, final Reaper<T, A> reaper) {
super(referent, References.ReaperThread.REAPER_QUEUE);
super(referent, References.ReaperThread.getReaperQueue());
this.reaper = reaper;
this.attachment = attachment;
}
Expand Down
46 changes: 34 additions & 12 deletions ref/src/main/java/io/smallrye/common/ref/References.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import java.security.PrivilegedAction;
import java.util.concurrent.atomic.AtomicInteger;

import org.graalvm.nativeimage.ImageInfo;

import io.smallrye.common.constraint.Assert;

/**
Expand All @@ -17,27 +19,47 @@ private References() {

private static final Reference<?, ?> NULL = new StrongReference<>(null);

static final class ReaperThread extends Thread {
static final class BuildTimeHolder {
static final ReferenceQueue<Object> REAPER_QUEUE = new ReferenceQueue<Object>();
}

static final class ReaperThread extends Thread {
static ReferenceQueue<Object> getReaperQueue() {
return BuildTimeHolder.REAPER_QUEUE;
}

static {
final AtomicInteger cnt = new AtomicInteger(1);
final PrivilegedAction<Void> action = () -> {
final ReaperThread thr = new ReaperThread();
thr.setName("Reference Reaper #" + cnt.getAndIncrement());
thr.setDaemon(true);
thr.start();
return null;
};
for (int i = 0; i < 3; i++) {
doPrivileged(action);
if (isBuildTime()) {
// do nothing (class should be reinitialized)
} else {
final AtomicInteger cnt = new AtomicInteger(1);
final PrivilegedAction<Void> action = () -> startThreadAction(cnt.getAndIncrement());
for (int i = 0; i < 3; i++) {
doPrivileged(action);
}
}
}

private static boolean isBuildTime() {
try {
return ImageInfo.inImageBuildtimeCode();
} catch (Throwable ignored) {
return false;
}
}

private static Void startThreadAction(int id) {
final ReaperThread thr = new ReaperThread();
thr.setName("Reference Reaper #" + id);
thr.setDaemon(true);
thr.start();
return null;
}

public void run() {
for (;;)
try {
final java.lang.ref.Reference<?> ref = REAPER_QUEUE.remove();
final java.lang.ref.Reference<?> ref = ReaperThread.getReaperQueue().remove();
if (ref instanceof CleanerReference) {
((CleanerReference<?, ?>) ref).clean();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public SoftReference(final T referent, final A attachment, final ReferenceQueue<
* @param reaper the reaper to use
*/
public SoftReference(final T referent, final A attachment, final Reaper<T, A> reaper) {
super(referent, References.ReaperThread.REAPER_QUEUE);
super(referent, References.ReaperThread.getReaperQueue());
this.reaper = reaper;
this.attachment = attachment;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public WeakReference(final T referent, final A attachment, final ReferenceQueue<
* @param reaper the reaper to use
*/
public WeakReference(final T referent, final A attachment, final Reaper<T, A> reaper) {
super(referent, References.ReaperThread.REAPER_QUEUE);
super(referent, References.ReaperThread.getReaperQueue());
this.attachment = attachment;
this.reaper = reaper;
}
Expand Down

0 comments on commit 9a2b466

Please sign in to comment.