Skip to content

Commit 0555393

Browse files
committed
Replace FiberPoolThread with a plain Thread to prepare for VirtualThread
1 parent 5c06f16 commit 0555393

File tree

4 files changed

+14
-32
lines changed

4 files changed

+14
-32
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050
import org.truffleruby.core.exception.RubySyntaxError;
5151
import org.truffleruby.core.exception.RubySystemCallError;
5252
import org.truffleruby.core.exception.RubySystemExit;
53-
import org.truffleruby.core.fiber.FiberPoolThread;
5453
import org.truffleruby.core.fiber.RubyFiber;
5554
import org.truffleruby.core.hash.RubyHash;
5655
import org.graalvm.options.OptionValues;
@@ -192,9 +191,10 @@ public static final class ThreadLocalState {
192191
}
193192

194193
private final ContextThreadLocal<ThreadLocalState> threadLocalState = createContextThreadLocal(
195-
(context, thread) -> thread instanceof FiberPoolThread
196-
? ((FiberPoolThread) thread).threadLocalState
197-
: new ThreadLocalState());
194+
(context, thread) -> {
195+
var state = context.getThreadManager().threadLocalStates.remove(thread);
196+
return state != null ? state : new ThreadLocalState();
197+
});
198198

199199
private final CyclicAssumption tracingCyclicAssumption = new CyclicAssumption("object-space-tracing");
200200
@CompilationFinal private volatile Assumption tracingAssumption = tracingCyclicAssumption.getAssumption();

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ private void fiberMain(RubyContext context, RubyFiber fiber, RubyProc block, Nod
9898
assert !fiber.isRootFiber() : "Root Fibers execute threadMain() and not fiberMain()";
9999
assertNotEntered("Fibers should start unentered to avoid triggering multithreading");
100100

101-
final FiberPoolThread thread = (FiberPoolThread) Thread.currentThread();
101+
final Thread thread = Thread.currentThread();
102+
var threadLocalState = context.getThreadManager().threadLocalStates.get(thread);
102103
final SourceSection sourceSection = block.getSharedMethodInfo().getSourceSection();
103104
final String oldName = thread.getName();
104105
thread.setName(NAME_PREFIX + " id=" + thread.getId() + " from " + context.fileLine(sourceSection));
@@ -116,7 +117,7 @@ private void fiberMain(RubyContext context, RubyFiber fiber, RubyProc block, Nod
116117
* for multiple Fibers and multiple Ruby Threads due to the Fiber pool, but initializeThread() is only called
117118
* once per thread. */
118119
fiber.rubyThread.setCurrentFiber(fiber);
119-
thread.threadLocalState.rubyThread = fiber.rubyThread;
120+
threadLocalState.rubyThread = fiber.rubyThread;
120121

121122
final Object prev = truffleContext.enter(currentNode);
122123

@@ -153,7 +154,7 @@ private void fiberMain(RubyContext context, RubyFiber fiber, RubyProc block, Nod
153154
fiber.status = FiberStatus.TERMINATED;
154155
// Leave context before addToMessageQueue() -> parent Fiber starts executing
155156
truffleContext.leave(currentNode, prev);
156-
thread.threadLocalState.rubyThread = null;
157+
threadLocalState.rubyThread = null;
157158
cleanup(fiber, thread);
158159
thread.setName(oldName);
159160

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

Lines changed: 0 additions & 23 deletions
This file was deleted.

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import com.oracle.truffle.api.object.DynamicObjectLibrary;
3434
import org.truffleruby.RubyContext;
3535
import org.truffleruby.RubyLanguage;
36+
import org.truffleruby.RubyLanguage.ThreadLocalState;
3637
import org.truffleruby.SuppressFBWarnings;
3738
import org.truffleruby.collections.Memo;
3839
import org.truffleruby.core.DummyNode;
@@ -41,7 +42,6 @@
4142
import org.truffleruby.core.exception.RubyException;
4243
import org.truffleruby.core.exception.RubySystemExit;
4344
import org.truffleruby.core.fiber.FiberManager;
44-
import org.truffleruby.core.fiber.FiberPoolThread;
4545
import org.truffleruby.core.fiber.RubyFiber;
4646
import org.truffleruby.core.klass.RubyClass;
4747
import org.truffleruby.core.string.StringUtils;
@@ -151,6 +151,8 @@ public void restartMainThread(Thread mainJavaThread) {
151151
// spawning Thread => Fiber object
152152
public static final ThreadLocal<RubyFiber> FIBER_BEING_SPAWNED = new ThreadLocal<>();
153153

154+
public final Map<Thread, ThreadLocalState> threadLocalStates = new ConcurrentHashMap<>();
155+
154156
private Thread createFiberJavaThread(Runnable runnable) {
155157
final RubyFiber fiber = FIBER_BEING_SPAWNED.get();
156158
assert fiber != null;
@@ -159,7 +161,9 @@ private Thread createFiberJavaThread(Runnable runnable) {
159161
throw new UnsupportedOperationException("fibers should not be created while pre-initializing the context");
160162
}
161163

162-
final Thread thread = new FiberPoolThread(runnable); // context.getEnv().createUnenteredThread(runnable);
164+
final Thread thread = new Thread(runnable); // context.getEnv().createUnenteredThread(runnable);
165+
166+
threadLocalStates.put(thread, new ThreadLocalState());
163167
thread.setName("Ruby-FiberPool-" + thread.getName());
164168
thread.setDaemon(true); // GR-33255
165169
rubyManagedThreads.add(thread); // need to be set before initializeThread()

0 commit comments

Comments
 (0)