Skip to content

Commit

Permalink
Showing 8 changed files with 29 additions and 46 deletions.
Original file line number Diff line number Diff line change
@@ -19,7 +19,7 @@
import org.jruby.truffle.language.backtrace.Backtrace;
import org.jruby.truffle.language.control.RaiseException;
import org.jruby.truffle.core.Layouts;
import org.jruby.truffle.language.SafepointAction;
import org.jruby.util.func.Function2;

import static org.jruby.RubyThread.*;

@@ -45,7 +45,7 @@ public DynamicObject raise(DynamicObject thread, final DynamicObject exception)
public static void raiseInThread(final RubyContext context, DynamicObject rubyThread, final DynamicObject exception, Node currentNode) {
final Thread javaThread = Layouts.FIBER.getThread((Layouts.THREAD.getFiberManager(rubyThread).getCurrentFiber()));

context.getSafepointManager().pauseThreadAndExecuteLater(javaThread, currentNode, new SafepointAction() {
context.getSafepointManager().pauseThreadAndExecuteLater(javaThread, currentNode, new Function2<Void, DynamicObject, Node>() {
@Override
public Void apply(DynamicObject currentThread, Node currentNode) {
if (Layouts.EXCEPTION.getBacktrace(exception) == null) {
Original file line number Diff line number Diff line change
@@ -21,8 +21,8 @@
import org.jruby.truffle.RubyContext;
import org.jruby.truffle.language.backtrace.BacktraceFormatter;
import org.jruby.truffle.language.control.RaiseException;
import org.jruby.truffle.language.SafepointAction;
import org.jruby.truffle.language.SafepointManager;
import org.jruby.util.func.Function2;

import java.util.Collections;
import java.util.Set;
@@ -236,7 +236,7 @@ public Object[] getThreadList() {
private void killOtherThreads() {
while (true) {
try {
context.getSafepointManager().pauseAllThreadsAndExecute(null, false, new SafepointAction() {
context.getSafepointManager().pauseAllThreadsAndExecute(null, false, new Function2<Void, DynamicObject, Node>() {
@Override
public synchronized Void apply(DynamicObject thread, Node currentNode) {
if (thread != rootThread && Thread.currentThread() == Layouts.THREAD.getThread(thread)) {
Original file line number Diff line number Diff line change
@@ -33,7 +33,7 @@
import org.jruby.truffle.language.control.RaiseException;
import org.jruby.truffle.language.control.ReturnException;
import org.jruby.truffle.language.control.ThreadExitException;
import org.jruby.truffle.language.SafepointAction;
import org.jruby.util.func.Function2;

import java.util.ArrayList;
import java.util.concurrent.CountDownLatch;
@@ -181,7 +181,7 @@ public DynamicObject backtrace(DynamicObject rubyThread) {

final DynamicObject[] result = new DynamicObject[1];

getContext().getSafepointManager().pauseThreadAndExecute(thread, this, new SafepointAction() {
getContext().getSafepointManager().pauseThreadAndExecute(thread, this, new Function2<Void, DynamicObject, Node>() {
@Override
public Void apply(DynamicObject thread, Node currentNode) {
final Backtrace backtrace = RubyCallStack.getBacktrace(getContext(), currentNode);
@@ -225,7 +225,7 @@ public DynamicObject kill(final DynamicObject rubyThread) {
return rubyThread;
}

getContext().getSafepointManager().pauseThreadAndExecuteLater(toKill, this, new SafepointAction() {
getContext().getSafepointManager().pauseThreadAndExecuteLater(toKill, this, new Function2<Void, DynamicObject, Node>() {
@Override
public Void apply(DynamicObject currentThread, Node currentNode) {
shutdown(getContext(), currentThread, currentNode);

This file was deleted.

Original file line number Diff line number Diff line change
@@ -21,6 +21,7 @@
import org.jruby.truffle.core.InterruptMode;
import org.jruby.truffle.RubyContext;
import org.jruby.truffle.core.Layouts;
import org.jruby.util.func.Function2;

import java.util.Collections;
import java.util.Set;
@@ -38,7 +39,7 @@ public class SafepointManager {
private final ReentrantLock lock = new ReentrantLock();

private final Phaser phaser = new Phaser();
private volatile SafepointAction action;
private volatile Function2<Void,DynamicObject,Node> action;
private volatile boolean deferred;

public SafepointManager(RubyContext context) {
@@ -93,7 +94,7 @@ private void assumptionInvalidated(Node currentNode, boolean fromBlockingCall) {
return; // interrupt me later
}

SafepointAction deferredAction = step(currentNode, false);
Function2<Void,DynamicObject,Node> deferredAction = step(currentNode, false);

// We're now running again normally and can run deferred actions
if (deferredAction != null) {
@@ -102,7 +103,7 @@ private void assumptionInvalidated(Node currentNode, boolean fromBlockingCall) {
}

@TruffleBoundary
private SafepointAction step(Node currentNode, boolean isDrivingThread) {
private Function2<Void,DynamicObject,Node> step(Node currentNode, boolean isDrivingThread) {
final DynamicObject thread = context.getThreadManager().getCurrentThread();

// wait other threads to reach their safepoint
@@ -116,7 +117,7 @@ private SafepointAction step(Node currentNode, boolean isDrivingThread) {
phaser.arriveAndAwaitAdvance();

// Read these while in the safepoint.
SafepointAction deferredAction = deferred ? action : null;
Function2<Void,DynamicObject,Node> deferredAction = deferred ? action : null;

try {
if (!deferred && thread != null && Layouts.THREAD.getStatus(thread) != Status.ABORTING) {
@@ -139,7 +140,7 @@ private void interruptOtherThreads() {
}
}

private void pauseAllThreadsAndExecute(Node currentNode, boolean isRubyThread, SafepointAction action, boolean deferred) {
private void pauseAllThreadsAndExecute(Node currentNode, boolean isRubyThread, Function2<Void,DynamicObject,Node> action, boolean deferred) {
this.action = action;
this.deferred = deferred;

@@ -156,7 +157,7 @@ private void pauseAllThreadsAndExecute(Node currentNode, boolean isRubyThread, S
// Variants for all threads

@TruffleBoundary
public void pauseAllThreadsAndExecute(Node currentNode, boolean deferred, SafepointAction action) {
public void pauseAllThreadsAndExecute(Node currentNode, boolean deferred, Function2<Void,DynamicObject,Node> action) {
if (lock.isHeldByCurrentThread()) {
throw new IllegalStateException("Re-entered SafepointManager");
}
@@ -179,7 +180,7 @@ public void pauseAllThreadsAndExecute(Node currentNode, boolean deferred, Safepo
}

@TruffleBoundary
public void pauseAllThreadsAndExecuteFromNonRubyThread(boolean deferred, SafepointAction action) {
public void pauseAllThreadsAndExecuteFromNonRubyThread(boolean deferred, Function2<Void,DynamicObject,Node> action) {
if (lock.isHeldByCurrentThread()) {
throw new IllegalStateException("Re-entered SafepointManager");
}
@@ -203,13 +204,13 @@ public void pauseAllThreadsAndExecuteFromNonRubyThread(boolean deferred, Safepoi
// Variants for a single thread

@TruffleBoundary
public void pauseThreadAndExecute(final Thread thread, Node currentNode, final SafepointAction action) {
public void pauseThreadAndExecute(final Thread thread, Node currentNode, final Function2<Void,DynamicObject,Node> action) {
if (Thread.currentThread() == thread) {
// fast path if we are already the right thread
DynamicObject rubyThread = context.getThreadManager().getCurrentThread();
action.apply(rubyThread, currentNode);
} else {
pauseAllThreadsAndExecute(currentNode, false, new SafepointAction() {
pauseAllThreadsAndExecute(currentNode, false, new Function2<Void, DynamicObject, Node>() {
@Override
public Void apply(DynamicObject rubyThread, Node currentNode) {
if (Thread.currentThread() == thread) {
@@ -222,13 +223,13 @@ public Void apply(DynamicObject rubyThread, Node currentNode) {
}

@TruffleBoundary
public void pauseThreadAndExecuteLater(final Thread thread, Node currentNode, final SafepointAction action) {
public void pauseThreadAndExecuteLater(final Thread thread, Node currentNode, final Function2<Void,DynamicObject,Node> action) {
if (Thread.currentThread() == thread) {
// fast path if we are already the right thread
DynamicObject rubyThread = context.getThreadManager().getCurrentThread();
action.apply(rubyThread, currentNode);
} else {
pauseAllThreadsAndExecute(currentNode, true, new SafepointAction() {
pauseAllThreadsAndExecute(currentNode, true, new Function2<Void, DynamicObject, Node>() {
@Override
public Void apply(DynamicObject rubyThread, Node currentNode) {
if (Thread.currentThread() == thread) {
@@ -241,8 +242,8 @@ public Void apply(DynamicObject rubyThread, Node currentNode) {
}

@TruffleBoundary
public void pauseThreadAndExecuteLaterFromNonRubyThread(final Thread thread, final SafepointAction action) {
pauseAllThreadsAndExecuteFromNonRubyThread(true, new SafepointAction() {
public void pauseThreadAndExecuteLaterFromNonRubyThread(final Thread thread, final Function2<Void,DynamicObject,Node> action) {
pauseAllThreadsAndExecuteFromNonRubyThread(true, new Function2<Void, DynamicObject, Node>() {
@Override
public Void apply(DynamicObject rubyThread, Node currentNode) {
if (Thread.currentThread() == thread) {
Original file line number Diff line number Diff line change
@@ -21,7 +21,7 @@
import org.jruby.truffle.RubyContext;
import org.jruby.truffle.core.hash.Entry;
import org.jruby.truffle.core.Layouts;
import org.jruby.truffle.language.SafepointAction;
import org.jruby.util.func.Function2;

import java.util.*;

@@ -33,7 +33,7 @@ public static Set<DynamicObject> stopAndGetAllObjects(

final Thread stoppingThread = Thread.currentThread();

context.getSafepointManager().pauseAllThreadsAndExecute(currentNode, false, new SafepointAction() {
context.getSafepointManager().pauseAllThreadsAndExecute(currentNode, false, new Function2<Void, DynamicObject, Node>() {

@Override
public Void apply(DynamicObject thread, Node currentNode) {
@@ -80,7 +80,7 @@ public static Set<DynamicObject> stopAndGetRootObjects(Node currentNode, final R

final Thread stoppingThread = Thread.currentThread();

context.getSafepointManager().pauseAllThreadsAndExecute(currentNode, false, new SafepointAction() {
context.getSafepointManager().pauseAllThreadsAndExecute(currentNode, false, new Function2<Void, DynamicObject, Node>() {
@Override
public Void apply(DynamicObject thread, Node currentNode) {
objects.add(thread);
Original file line number Diff line number Diff line change
@@ -15,7 +15,7 @@
import org.jruby.truffle.core.proc.ProcNodes;
import org.jruby.truffle.RubyContext;
import org.jruby.truffle.core.Layouts;
import org.jruby.truffle.language.SafepointAction;
import org.jruby.util.func.Function2;

public class ProcSignalHandler implements SignalHandler {

@@ -32,7 +32,7 @@ public ProcSignalHandler(RubyContext context, DynamicObject proc) {
@Override
public void handle(Signal signal) {
Thread mainThread = Layouts.FIBER.getThread((Layouts.THREAD.getFiberManager(context.getThreadManager().getRootThread()).getCurrentFiber()));
context.getSafepointManager().pauseThreadAndExecuteLaterFromNonRubyThread(mainThread, new SafepointAction() {
context.getSafepointManager().pauseThreadAndExecuteLaterFromNonRubyThread(mainThread, new Function2<Void, DynamicObject, Node>() {
@Override
public Void apply(DynamicObject thread, Node currentNode) {
ProcNodes.rootCall(proc);
Original file line number Diff line number Diff line change
@@ -18,10 +18,10 @@
import com.sun.net.httpserver.HttpServer;
import org.jruby.truffle.language.RubyCallStack;
import org.jruby.truffle.RubyContext;
import org.jruby.truffle.language.SafepointAction;
import org.jruby.truffle.language.backtrace.Backtrace;
import org.jruby.truffle.language.backtrace.BacktraceFormatter;
import org.jruby.truffle.core.Layouts;
import org.jruby.util.func.Function2;

import java.io.IOException;
import java.io.OutputStream;
@@ -62,7 +62,7 @@ public void handle(HttpExchange httpExchange) {
try {
final StringBuilder builder = new StringBuilder();

context.getSafepointManager().pauseAllThreadsAndExecuteFromNonRubyThread(false, new SafepointAction() {
context.getSafepointManager().pauseAllThreadsAndExecuteFromNonRubyThread(false, new Function2<Void, DynamicObject, Node>() {

@Override
public Void apply(DynamicObject thread, Node currentNode) {
@@ -114,7 +114,7 @@ public Void apply(DynamicObject thread, Node currentNode) {
public void handle(HttpExchange httpExchange) {
try {
Thread mainThread = Layouts.FIBER.getThread((Layouts.THREAD.getFiberManager(context.getThreadManager().getRootThread()).getCurrentFiber()));
context.getSafepointManager().pauseThreadAndExecuteLaterFromNonRubyThread(mainThread, new SafepointAction() {
context.getSafepointManager().pauseThreadAndExecuteLaterFromNonRubyThread(mainThread, new Function2<Void, DynamicObject, Node>() {
@Override
public Void apply(DynamicObject thread, final Node currentNode) {
new SimpleShell(context).run(Truffle.getRuntime().getCurrentFrame()

1 comment on commit a29b589

@eregon
Copy link
Member

@eregon eregon commented on a29b589 Feb 24, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why? It's much less clear and much more verbose.
It also prevents easy refactoring.

Sorry, something went wrong.

Please sign in to comment.