Skip to content
Open
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
32 changes: 27 additions & 5 deletions core/src/main/java/org/jruby/RubyThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,9 @@ private record SchedulerJoiner(IRubyObject scheduler, RubyThread thread, IRubyOb

private volatile RubyThread fiberCurrentThread;

// the RubyThread of the fiber now running on this thread, null for the root fiber. MRI: th->ec
private volatile RubyThread runningFiberThread;

private IRubyObject scheduler;
private volatile int blockingCount = 1;

Expand Down Expand Up @@ -466,6 +469,25 @@ public RubyThread getFiberCurrentThread() {
return fiberCurrentThread == null ? this : fiberCurrentThread;
}

/**
* Record which fiber is running on this thread. Called by the fiber that just got control.
*
* @param fiberThread the RubyThread backing that fiber, or this thread for its root fiber
*/
public void setRunningFiberThread(RubyThread fiberThread) {
this.runningFiberThread = fiberThread == this ? null : fiberThread;
}

/**
* Each fiber runs on a RubyThread of its own, but Thread.current answers with the thread owning it.
* Fiber-scoped state asked of that thread belongs to whichever of its fibers is running.
* MRI: th->ec
*/
private RubyThread running() {
RubyThread runningFiberThread = this.runningFiberThread;
return runningFiberThread == null ? this : runningFiberThread;
}

/**
* Perform pre-execution tasks once the native thread is running, but we
* have not yet called the Ruby code for the thread.
Expand Down Expand Up @@ -1016,7 +1038,7 @@ public IRubyObject getName(ThreadContext context) {

@JRubyMethod(meta = true)
public static RubyThread current(IRubyObject recv) {
return recv.getRuntime().getCurrentContext().getThread();
return recv.getRuntime().getCurrentContext().getFiberCurrentThread();
}

@JRubyMethod(meta = true)
Expand Down Expand Up @@ -1160,7 +1182,7 @@ public IRubyObject fetch(ThreadContext context, IRubyObject key, IRubyObject _de
@JRubyMethod(name = "[]")
public IRubyObject op_aref(ThreadContext context, IRubyObject key) {
key = RubySymbol.idSymbolFromObject(context, key);
final Map<IRubyObject, IRubyObject> locals = getFiberLocals();
final Map<IRubyObject, IRubyObject> locals = running().getFiberLocals();
synchronized (locals) {
IRubyObject value;
return (value = locals.get(key)) == null ? context.nil : value;
Expand All @@ -1172,7 +1194,7 @@ public IRubyObject op_aset(ThreadContext context, IRubyObject key, IRubyObject v
if (isFrozen()) throw frozenError(context, this, "can't modify frozen thread locals");

key = RubySymbol.idSymbolFromObject(context, key);
final Map<IRubyObject, IRubyObject> locals = getFiberLocals();
final Map<IRubyObject, IRubyObject> locals = running().getFiberLocals();
synchronized (locals) {
locals.put(key, value);
}
Expand All @@ -1182,15 +1204,15 @@ public IRubyObject op_aset(ThreadContext context, IRubyObject key, IRubyObject v
@JRubyMethod(name = "key?")
public RubyBoolean key_p(ThreadContext context, IRubyObject key) {
key = RubySymbol.idSymbolFromObject(context, key);
final Map<IRubyObject, IRubyObject> locals = getFiberLocals();
final Map<IRubyObject, IRubyObject> locals = running().getFiberLocals();
synchronized (locals) {
return asBoolean(context, locals.containsKey(key));
}
}

@JRubyMethod
public RubyArray keys() {
final Map<IRubyObject, IRubyObject> locals = getFiberLocals();
final Map<IRubyObject, IRubyObject> locals = running().getFiberLocals();
IRubyObject[] ary;
synchronized (locals) {
ary = new IRubyObject[locals.size()];
Expand Down
4 changes: 4 additions & 0 deletions core/src/main/java/org/jruby/ext/fiber/ThreadFiber.java
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,9 @@ private static FiberRequest exchangeWithFiber(ThreadContext context, FiberData c
// MRI: fiber_switch re-reads the current fiber; control can return into a different one
if (currentFiberData.blocking) context.getFiberCurrentThread().incrementBlocking();

// MRI: fiber_restore_thread, th->ec now belongs to us again
context.getFiberCurrentThread().setRunningFiberThread(context.getThread());

context.trace(RubyEvent.FIBER_SWITCH, "resume", context.runtime.getFiber());
}
}
Expand Down Expand Up @@ -614,6 +617,7 @@ static RubyThread createThread(ThreadContext context, final FiberData data, fina
try {
FiberRequest init = data.queue.pop(ctxt);
data.started = true;
data.parent.setRunningFiberThread(rubyThread);

// MRI: rb_fiber_start. A first run does not return through a switch
if (data.blocking) data.parent.incrementBlocking();
Expand Down
26 changes: 26 additions & 0 deletions spec/ruby/core/thread/element_reference_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,32 @@
th[key].should == 1
end

it "returns the local of the fiber running in the thread when called from another thread" do
q1 = Queue.new
q2 = Queue.new
th = Thread.new do
Thread.current[:value] = :thread
Fiber.new do
Thread.current[:value] = :fiber
q1 << :ready
q2.pop
end.resume
q1 << :ready
q2.pop
end

begin
q1.pop
th[:value].should == :fiber
q2 << :done
q1.pop
th[:value].should == :thread
ensure
q2 << :done << :done
th.join
end
end

it "raises exceptions on the wrong type of keys" do
-> { Thread.current[nil] }.should.raise(TypeError)
-> { Thread.current[5] }.should.raise(TypeError)
Expand Down
2 changes: 0 additions & 2 deletions test/mri/excludes/TestThread.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
exclude :test_list, "JVM finalizer threads get adopted, randomly skewing the result (#4960)"
exclude :test_machine_stack_size, "slow and unreliable"
exclude :test_main_thread_status_at_exit, "we do not set main thread to aborting when it terminates; differing opinions on thread lifecycle at main shutdown"
exclude :test_main_thread_variable_in_enumerator, "differing notions of current thread within a fiber"
exclude :test_mutex_unlock_on_trap, "traps execute in a separate thread in JRuby, and can't unlock a mutex"
exclude :test_no_valid_cfp, "raises NullPointerException, may depend on MRI internal stack structures for nesting call without block"
exclude :test_priority, "unreliably depends on thread scheduling"
Expand All @@ -13,7 +12,6 @@
exclude :test_thread_join_main_thread, "hangs"
exclude :test_thread_setname_in_initialize, "needs investigation #4308"
exclude :test_thread_timer_and_interrupt, "event statuses do not match expected"
exclude :test_thread_variable_in_enumerator, "fibers in JRuby have their own Thread.current"
exclude :test_vm_machine_stack_size, "slow and unreliable"
exclude :test_yield_across_thread_through_enum, "JRuby prevents parallelism in enum in a different way than CRuby"
exclude :test_handle_interrupt_masks_sigint, "handle_interrupt does not mask SIGINT"
Loading