Skip to content

Commit 1467ba6

Browse files
committed
Change keepalive code to preserve individual VALUEWrapper objects
1 parent e3cfcf1 commit 1467ba6

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

src/main/java/org/truffleruby/cext/ValueWrapperManager.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,6 @@ private HandleBlock() {
227227
public HandleBlock(RubyContext context, RubyLanguage language, ValueWrapperManager manager) {
228228
HandleBlockAllocator allocator = language.handleBlockAllocator;
229229
long base = allocator.getFreeBlock();
230-
if (context != null && context.getOptions().CEXTS_KEEP_HANDLES_ALIVE) {
231-
keepAlive(this);
232-
}
233230
this.base = base;
234231
this.wrappers = new ValueWrapperWeakReference[BLOCK_SIZE];
235232
this.count = 0;
@@ -243,11 +240,6 @@ private static Runnable makeCleaner(ValueWrapperManager manager, long base, Hand
243240
};
244241
}
245242

246-
@TruffleBoundary
247-
private static void keepAlive(HandleBlock block) {
248-
keepAlive.add(block);
249-
}
250-
251243
public long getBase() {
252244
return base;
253245
}
@@ -298,10 +290,15 @@ public static class HandleBlockHolder {
298290
@GenerateUncached
299291
public abstract static class AllocateHandleNode extends RubyBaseNode {
300292

293+
private static final Set<ValueWrapper> keepAlive = ConcurrentHashMap.newKeySet();
294+
301295
public abstract long execute(ValueWrapper wrapper);
302296

303297
@Specialization(guards = "!isSharedObject(wrapper)")
304298
protected long allocateHandleOnKnownThread(ValueWrapper wrapper) {
299+
if (getContext().getOptions().CEXTS_KEEP_HANDLES_ALIVE) {
300+
keepAlive(wrapper);
301+
}
305302
return allocateHandle(
306303
wrapper,
307304
getContext(),
@@ -312,6 +309,9 @@ protected long allocateHandleOnKnownThread(ValueWrapper wrapper) {
312309

313310
@Specialization(guards = "isSharedObject(wrapper)")
314311
protected long allocateSharedHandleOnKnownThread(ValueWrapper wrapper) {
312+
if (getContext().getOptions().CEXTS_KEEP_HANDLES_ALIVE) {
313+
keepAlive(wrapper);
314+
}
315315
return allocateHandle(
316316
wrapper,
317317
getContext(),
@@ -320,6 +320,11 @@ protected long allocateSharedHandleOnKnownThread(ValueWrapper wrapper) {
320320
true);
321321
}
322322

323+
@TruffleBoundary
324+
protected static void keepAlive(ValueWrapper wrapper) {
325+
keepAlive.add(wrapper);
326+
}
327+
323328
protected long allocateHandle(ValueWrapper wrapper, RubyContext context, RubyLanguage language,
324329
HandleBlockHolder holder, boolean shared) {
325330
HandleBlock block;

0 commit comments

Comments
 (0)