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
24 changes: 24 additions & 0 deletions core/src/main/java/org/jruby/RubyThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -1029,6 +1029,30 @@ private Map<IRubyObject, IRubyObject> getThreadLocals0() {
return locals;
}

/**
* Clear the fiber local variable storage for this thread.
* Meant for Java consumers when reusing threads (e.g. during thread pooling).
* @see #clearThreadLocals()
*/
public void clearFiberLocals() {
final Map<IRubyObject, IRubyObject> locals = getFiberLocals();
synchronized (locals) {
locals.clear();
}
}

/**
* Clear the thread local variable storage for this thread.
* Meant for Java consumers when reusing threads (e.g. during thread pooling).
* @see #clearFiberLocals()
*/
public void clearThreadLocals() {
final Map<IRubyObject, IRubyObject> locals = getThreadLocals();
synchronized (locals) {
locals.clear();
}
}

@Override
public final Map<Object, IRubyObject> getContextVariables() {
return contextVariables;
Expand Down
50 changes: 50 additions & 0 deletions core/src/test/java/org/jruby/test/TestRubyThread.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package org.jruby.test;

import org.jruby.RubyThread;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;

public class TestRubyThread extends Base {
Expand Down Expand Up @@ -50,4 +54,50 @@ public void testJavaErrorDoesPropagate() throws InterruptedException {
assertSame(runtime.getNil(), thread.status(runtime.getCurrentContext()));
}

public void testClearLocals() throws InterruptedException {
final CountDownLatch latch1 = new CountDownLatch(1);
final AtomicReference<RubyThread> otherThread = new AtomicReference<>();
final CountDownLatch latch2 = new CountDownLatch(1);

Thread thread = new Thread(() -> {
runtime.evalScriptlet("Thread.current[:foo] = :bar");
runtime.evalScriptlet("Thread.current.thread_variable_set('local', 42)");
otherThread.set(RubyThread.current(runtime.getThread()));

runtime.evalScriptlet("sleep(0.1)");
latch1.countDown();

runtime.evalScriptlet("sleep(0.1)");
try {
latch2.await(3, TimeUnit.SECONDS);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
});

thread.start();
latch1.await(3, TimeUnit.SECONDS);

final ThreadContext context = runtime.getCurrentContext();
IRubyObject local;

local = otherThread.get().op_aref(context, runtime.newSymbol("foo"));
assertEquals("bar", local.toString());

otherThread.get().clearFiberLocals();

local = otherThread.get().op_aref(context, runtime.newSymbol("foo"));
assertSame(runtime.getNil(), local);
assertEquals(0, otherThread.get().keys().size());

local = otherThread.get().thread_variable_p(context, runtime.newString("local"));
assertSame(runtime.getTrue(), local);

otherThread.get().clearThreadLocals();

local = otherThread.get().thread_variable_p(context, runtime.newString("local"));
assertSame(runtime.getFalse(), local);

latch2.countDown();
}
}