Skip to content

Commit a76ef4d

Browse files
committed
Extract ThreadManager#leaveAndEnter for convenience
1 parent 48d49c5 commit a76ef4d

File tree

2 files changed

+29
-27
lines changed

2 files changed

+29
-27
lines changed

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

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -246,22 +246,12 @@ private void resume(RubyFiber fromFiber, RubyFiber fiber, FiberOperation operati
246246
@TruffleBoundary
247247
public Object[] transferControlTo(RubyFiber fromFiber, RubyFiber fiber, FiberOperation operation, Object[] args) {
248248
final TruffleContext truffleContext = context.getEnv().getContext();
249-
250-
final FiberMessage message;
251249
final boolean isRubyManagedThread = context.getThreadManager().isRubyManagedThread(Thread.currentThread());
252-
if (isRubyManagedThread) {
253-
context.getSafepointManager().leaveThread();
254-
}
255-
try {
256-
message = truffleContext.leaveAndEnter(null, () -> {
257-
resume(fromFiber, fiber, operation, args);
258-
return waitMessage(fromFiber);
259-
});
260-
} finally {
261-
if (isRubyManagedThread) {
262-
context.getSafepointManager().enterThread();
263-
}
264-
}
250+
251+
final FiberMessage message = context.getThreadManager().leaveAndEnter(truffleContext, null, () -> {
252+
resume(fromFiber, fiber, operation, args);
253+
return waitMessage(fromFiber);
254+
}, isRubyManagedThread);
265255

266256
return handleMessage(fromFiber, message);
267257
}
@@ -325,18 +315,10 @@ public void killOtherFibers() {
325315
// This method might not be executed on the rootFiber Java Thread but possibly on another Java Thread.
326316

327317
final TruffleContext truffleContext = context.getEnv().getContext();
328-
assert truffleContext.isEntered();
329-
assert context.getThreadManager().isRubyManagedThread(Thread.currentThread());
330-
331-
context.getSafepointManager().leaveThread();
332-
try {
333-
truffleContext.leaveAndEnter(null, () -> {
334-
doKillOtherFibers();
335-
return null;
336-
});
337-
} finally {
338-
context.getSafepointManager().enterThread();
339-
}
318+
context.getThreadManager().leaveAndEnter(truffleContext, null, () -> {
319+
doKillOtherFibers();
320+
return null;
321+
}, true);
340322
}
341323

342324
private void doKillOtherFibers() {

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.concurrent.locks.Lock;
2020
import java.util.function.Supplier;
2121

22+
import com.oracle.truffle.api.TruffleContext;
2223
import com.oracle.truffle.api.object.DynamicObjectLibrary;
2324
import org.truffleruby.RubyContext;
2425
import org.truffleruby.RubyLanguage;
@@ -217,6 +218,8 @@ public void spawnFiber(RubyFiber fiber, Runnable task) {
217218
});
218219
}
219220

221+
/** Whether the thread was created by TruffleRuby. Also decides whether we use the {@link SafepointManager} on the
222+
* thread. */
220223
@TruffleBoundary
221224
public boolean isRubyManagedThread(Thread thread) {
222225
return rubyManagedThreads.contains(thread);
@@ -451,6 +454,23 @@ public interface UnblockingAction {
451454
void unblock();
452455
}
453456

457+
public <T> T leaveAndEnter(TruffleContext truffleContext, Node currentNode, Supplier<T> runWhileOutsideContext,
458+
boolean isRubyManagedThread) {
459+
assert truffleContext.isEntered();
460+
assert isRubyManagedThread == isRubyManagedThread(Thread.currentThread());
461+
462+
if (isRubyManagedThread) {
463+
context.getSafepointManager().leaveThread();
464+
}
465+
try {
466+
return truffleContext.leaveAndEnter(currentNode, runWhileOutsideContext);
467+
} finally {
468+
if (isRubyManagedThread) {
469+
context.getSafepointManager().enterThread();
470+
}
471+
}
472+
}
473+
454474
/** Only use when no context is available. */
455475
@TruffleBoundary
456476
public static <T> T retryWhileInterrupted(BlockingAction<T> action) {

0 commit comments

Comments
 (0)