Skip to content

Commit 9b0cf19

Browse files
committed
Remove the now-unnecessary javaThreadToRubyThread Map
1 parent 1b6663c commit 9b0cf19

File tree

4 files changed

+8
-45
lines changed

4 files changed

+8
-45
lines changed

src/main/java/org/truffleruby/RubyLanguage.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -583,8 +583,8 @@ public void initializeThread(RubyContext context, Thread thread) {
583583
}
584584

585585
if (context.getThreadManager().isRubyManagedThread(thread)) {
586-
final RubyThread rubyThread = context.getThreadManager().getCurrentThreadOrNull();
587-
if (rubyThread != null && rubyThread.thread == thread) { // new Ruby Thread
586+
final RubyThread rubyThread = getCurrentThread();
587+
if (rubyThread.thread == thread) { // new Ruby Thread
588588
if (thread != Thread.currentThread()) {
589589
throw CompilerDirectives
590590
.shouldNotReachHere("Ruby threads should be initialized on their Java thread");
@@ -602,9 +602,7 @@ public void initializeThread(RubyContext context, Thread thread) {
602602

603603
@Override
604604
public void disposeThread(RubyContext context, Thread thread) {
605-
LOGGER.fine(
606-
() -> "disposeThread(#" + thread.getId() + " " + thread + " on " +
607-
context.getThreadManager().getCurrentThreadOrNull() + ")");
605+
LOGGER.fine(() -> "disposeThread(#" + thread.getId() + " " + thread + " on " + getCurrentThread() + ")");
608606

609607
if (thread == context.getThreadManager().getRootJavaThread()) {
610608
if (context.getEnv().isPreInitialization()) {
@@ -623,8 +621,8 @@ public void disposeThread(RubyContext context, Thread thread) {
623621
}
624622

625623
if (context.getThreadManager().isRubyManagedThread(thread)) {
626-
final RubyThread rubyThread = context.getThreadManager().getCurrentThreadOrNull();
627-
if (rubyThread != null && rubyThread.thread == thread) { // Thread
624+
final RubyThread rubyThread = getCurrentThread();
625+
if (rubyThread.thread == thread) { // Thread
628626
if (thread != Thread.currentThread()) {
629627
throw CompilerDirectives.shouldNotReachHere("Ruby threads should be disposed on their Java thread");
630628
}
@@ -636,7 +634,7 @@ public void disposeThread(RubyContext context, Thread thread) {
636634
}
637635

638636
// A foreign Thread, its Fibers are considered isRubyManagedThread()
639-
final RubyThread rubyThread = context.getThreadManager().getRubyThreadForJavaThread(thread);
637+
final RubyThread rubyThread = this.rubyThread.get(thread);
640638
context.getThreadManager().cleanup(rubyThread, thread);
641639
}
642640

src/main/java/org/truffleruby/core/fiber/FiberManager.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -315,27 +315,20 @@ public void safepoint(RubyFiber fromFiber, RubyFiber fiber, SafepointAction acti
315315
}
316316

317317
public void start(RubyFiber fiber, Thread javaThread) {
318-
final ThreadManager threadManager = context.getThreadManager();
319-
320318
fiber.thread = javaThread;
321319

322320
final RubyThread rubyThread = fiber.rubyThread;
323-
threadManager.initializeValuesForJavaThread(rubyThread, javaThread);
324321

325322
// share RubyFiber as its fiberLocals might be accessed by other threads with Thread#[]
326323
SharedObjects.propagate(language, rubyThread, fiber);
327324
rubyThread.runningFibers.add(fiber);
328325
}
329326

330327
public void cleanup(RubyFiber fiber, Thread javaThread) {
331-
final ThreadManager threadManager = context.getThreadManager();
332-
333328
context.getValueWrapperManager().cleanup(context, fiber.handleData);
334329

335330
fiber.status = FiberStatus.TERMINATED;
336331

337-
threadManager.cleanupValuesForJavaThread(javaThread);
338-
339332
fiber.rubyThread.runningFibers.remove(fiber);
340333

341334
fiber.thread = null;

src/main/java/org/truffleruby/core/thread/ThreadManager.java

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
package org.truffleruby.core.thread;
1111

1212
import java.util.Collections;
13-
import java.util.Map;
1413
import java.util.Set;
1514
import java.util.Timer;
1615
import java.util.WeakHashMap;
@@ -21,7 +20,6 @@
2120
import java.util.function.Supplier;
2221

2322
import com.oracle.truffle.api.CompilerAsserts;
24-
import com.oracle.truffle.api.CompilerDirectives;
2523
import com.oracle.truffle.api.CompilerDirectives.ValueType;
2624
import com.oracle.truffle.api.TruffleContext;
2725
import com.oracle.truffle.api.TruffleSafepoint;
@@ -73,8 +71,6 @@ public class ThreadManager {
7371
private final RubyThread rootThread;
7472
@CompilationFinal private Thread rootJavaThread;
7573

76-
private final Map<Thread, RubyThread> javaThreadToRubyThread = new ConcurrentHashMap<>();
77-
7874
private final Set<RubyThread> runningRubyThreads = ConcurrentHashMap.newKeySet();
7975

8076
/** The set of Java threads TruffleRuby created, and is responsible to exit in {@link #killAndWaitOtherThreads()}.
@@ -170,7 +166,6 @@ private Thread createJavaThread(Runnable runnable, RubyThread rubyThread, String
170166
language.rubyFiberInitMap.put(thread, rubyThread.getRootFiber());
171167
thread.setName(NAME_PREFIX + " id=" + thread.getId() + " from " + info);
172168
rubyManagedThreads.add(thread); // need to be set before initializeThread()
173-
javaThreadToRubyThread.put(thread, rubyThread); // need to be set before initializeThread()
174169
thread.setUncaughtExceptionHandler(uncaughtExceptionHandler(rubyThread.getRootFiber()));
175170
return thread;
176171
}
@@ -591,29 +586,6 @@ Interrupter getNativeCallInterrupter() {
591586
}
592587
}
593588

594-
public void initializeValuesForJavaThread(RubyThread rubyThread, Thread thread) {
595-
javaThreadToRubyThread.put(thread, rubyThread);
596-
}
597-
598-
public void cleanupValuesForJavaThread(Thread thread) {
599-
javaThreadToRubyThread.remove(thread);
600-
}
601-
602-
@TruffleBoundary
603-
public RubyThread getRubyThreadForJavaThread(Thread thread) {
604-
final RubyThread rubyThread = javaThreadToRubyThread.get(thread);
605-
if (rubyThread == null) {
606-
throw CompilerDirectives.shouldNotReachHere(
607-
"No Ruby Thread is associated with Java Thread: " + thread);
608-
}
609-
return rubyThread;
610-
}
611-
612-
@TruffleBoundary
613-
public RubyThread getCurrentThreadOrNull() {
614-
return javaThreadToRubyThread.get(Thread.currentThread());
615-
}
616-
617589
public void registerThread(RubyThread thread) {
618590
if (!runningRubyThreads.add(thread)) {
619591
throw new UnsupportedOperationException(thread + " was already registered");
@@ -714,7 +686,7 @@ public String getThreadDebugInfo() {
714686
}
715687

716688
// cannot use getCurrentThread() as it might have been cleared
717-
if (thread == getCurrentThreadOrNull()) {
689+
if (thread == language.getCurrentThread()) {
718690
builder.append(" (current)");
719691
}
720692

src/main/java/org/truffleruby/language/SafepointManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public void pauseRubyThreadAndExecute(Node currentNode, SafepointAction action)
3535
final ThreadManager threadManager = context.getThreadManager();
3636
final RubyThread rubyThread = action.getTargetThread();
3737

38-
if (threadManager.getCurrentThreadOrNull() == rubyThread) {
38+
if (context.getEnv().getContext().isEntered() && context.getLanguageSlow().getCurrentThread() == rubyThread) {
3939
if (context.getLanguageSlow().getCurrentFiber() != rubyThread.getCurrentFiber()) {
4040
throw CompilerDirectives.shouldNotReachHere(
4141
"The currently executing Java thread does not correspond to the currently active fiber for the current Ruby thread");

0 commit comments

Comments
 (0)