Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: jruby/jruby
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 78d478b93012
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 71e9ed32b6c3
Choose a head ref
  • 2 commits
  • 8 files changed
  • 1 contributor

Commits on Feb 24, 2016

  1. [Truffle] Revert a29b589 manually.

    * It is clearer and less verbose.
    * It helps readability and refactoring.
    eregon committed Feb 24, 2016

    Unverified

    This user has not yet uploaded their public signing key.
    Copy the full SHA
    2eaac6d View commit details
  2. Copy the full SHA
    71e9ed3 View commit details
Original file line number Diff line number Diff line change
@@ -14,9 +14,9 @@
import org.jruby.truffle.RubyContext;
import org.jruby.truffle.core.Layouts;
import org.jruby.truffle.language.RubyGuards;
import org.jruby.truffle.language.SafepointAction;
import org.jruby.truffle.platform.signal.Signal;
import org.jruby.truffle.platform.signal.SignalHandler;
import org.jruby.util.func.Function2;

public class ProcSignalHandler implements SignalHandler {

@@ -33,11 +33,10 @@ 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 Function2<Void, DynamicObject, Node>() {
context.getSafepointManager().pauseThreadAndExecuteLaterFromNonRubyThread(mainThread, new SafepointAction() {
@Override
public Void apply(DynamicObject thread, Node currentNode) {
public void run(DynamicObject thread, Node currentNode) {
ProcNodes.rootCall(proc);
return null;
}
});
}
Original file line number Diff line number Diff line change
@@ -16,10 +16,9 @@
import com.oracle.truffle.api.source.SourceSection;
import org.jruby.truffle.RubyContext;
import org.jruby.truffle.core.Layouts;
import org.jruby.truffle.language.SafepointAction;
import org.jruby.truffle.language.backtrace.Backtrace;
import org.jruby.truffle.language.control.RaiseException;
import org.jruby.util.func.Function2;

import static org.jruby.RubyThread.RUBY_MAX_THREAD_PRIORITY;
import static org.jruby.RubyThread.RUBY_MIN_THREAD_PRIORITY;
import static org.jruby.RubyThread.javaPriorityToRubyPriority;
@@ -47,9 +46,9 @@ 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 Function2<Void, DynamicObject, Node>() {
context.getSafepointManager().pauseThreadAndExecuteLater(javaThread, currentNode, new SafepointAction() {
@Override
public Void apply(DynamicObject currentThread, Node currentNode) {
public void run(DynamicObject currentThread, Node currentNode) {
if (Layouts.EXCEPTION.getBacktrace(exception) == null) {
Backtrace backtrace = context.getCallStack().getBacktrace(currentNode);
Layouts.EXCEPTION.setBacktrace(exception, backtrace);
Original file line number Diff line number Diff line change
@@ -19,11 +19,10 @@
import org.jruby.truffle.core.Layouts;
import org.jruby.truffle.core.fiber.FiberNodes;
import org.jruby.truffle.language.RubyGuards;
import org.jruby.truffle.language.SafepointAction;
import org.jruby.truffle.language.SafepointManager;
import org.jruby.truffle.language.backtrace.BacktraceFormatter;
import org.jruby.truffle.language.control.RaiseException;
import org.jruby.util.func.Function2;

import java.util.Collections;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
@@ -236,13 +235,12 @@ public Object[] getThreadList() {
private void killOtherThreads() {
while (true) {
try {
context.getSafepointManager().pauseAllThreadsAndExecute(null, false, new Function2<Void, DynamicObject, Node>() {
context.getSafepointManager().pauseAllThreadsAndExecute(null, false, new SafepointAction() {
@Override
public synchronized Void apply(DynamicObject thread, Node currentNode) {
public synchronized void run(DynamicObject thread, Node currentNode) {
if (thread != rootThread && Thread.currentThread() == Layouts.THREAD.getThread(thread)) {
ThreadNodes.shutdown(context, thread, currentNode);
}
return null;
}
});
break; // Successfully executed the safepoint and sent the exceptions.
Original file line number Diff line number Diff line change
@@ -34,12 +34,11 @@
import org.jruby.truffle.language.NotProvided;
import org.jruby.truffle.language.RubyGuards;
import org.jruby.truffle.language.RubyNode;
import org.jruby.truffle.language.SafepointAction;
import org.jruby.truffle.language.backtrace.Backtrace;
import org.jruby.truffle.language.control.RaiseException;
import org.jruby.truffle.language.control.ReturnException;
import org.jruby.truffle.language.control.ThreadExitException;
import org.jruby.util.func.Function2;

import java.util.ArrayList;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
@@ -186,12 +185,11 @@ public DynamicObject backtrace(DynamicObject rubyThread) {

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

getContext().getSafepointManager().pauseThreadAndExecute(thread, this, new Function2<Void, DynamicObject, Node>() {
getContext().getSafepointManager().pauseThreadAndExecute(thread, this, new SafepointAction() {
@Override
public Void apply(DynamicObject thread, Node currentNode) {
public void run(DynamicObject thread, Node currentNode) {
final Backtrace backtrace = getContext().getCallStack().getBacktrace(currentNode);
result[0] = ExceptionNodes.backtraceAsRubyStringArray(getContext(), null, backtrace);
return null;
}
});

@@ -230,11 +228,10 @@ public DynamicObject kill(final DynamicObject rubyThread) {
return rubyThread;
}

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright (c) 2015, 2016 Oracle and/or its affiliates. All rights reserved. This
* code is released under a tri EPL/GPL/LGPL license. You can use it,
* redistribute it and/or modify it under the terms of the:
*
* Eclipse Public License version 1.0
* GNU General Public License version 2
* GNU Lesser General Public License version 2.1
*/
package org.jruby.truffle.language;

import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.api.object.DynamicObject;

public interface SafepointAction {

void run(DynamicObject thread, Node currentNode);
}

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

import java.util.Collections;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
@@ -40,7 +38,7 @@ public class SafepointManager {

@CompilationFinal private Assumption assumption = Truffle.getRuntime().createAssumption("SafepointManager");

private volatile Function2<Void,DynamicObject,Node> action;
private volatile SafepointAction action;
private volatile boolean deferred;

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

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

// We're now running again normally and can run deferred actions
if (deferredAction != null) {
deferredAction.apply(thread, currentNode);
deferredAction.run(thread, currentNode);
}
}

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

// Wait for other threads to reach their safepoint
@@ -119,11 +117,11 @@ private Function2<Void,DynamicObject,Node> step(Node currentNode, boolean isDriv
phaser.arriveAndAwaitAdvance();

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

try {
if (!deferred && thread != null && Layouts.THREAD.getStatus(thread) != Status.ABORTING) {
action.apply(thread, currentNode);
action.run(thread, currentNode);
}
} finally {
// Wait for other threads to finish their action
@@ -134,8 +132,7 @@ private Function2<Void,DynamicObject,Node> step(Node currentNode, boolean isDriv
}

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

// Run deferred actions after leaving the SafepointManager lock.
if (deferred) {
action.apply(context.getThreadManager().getCurrentThread(), currentNode);
action.run(context.getThreadManager().getCurrentThread(), currentNode);
}
}

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

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

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

@TruffleBoundary
public void pauseThreadAndExecuteLaterFromNonRubyThread(final Thread thread,
final Function2<Void,DynamicObject,Node> action) {
pauseAllThreadsAndExecuteFromNonRubyThread(true, new Function2<Void, DynamicObject, Node>() {
public void pauseThreadAndExecuteLaterFromNonRubyThread(final Thread thread, final SafepointAction action) {
pauseAllThreadsAndExecuteFromNonRubyThread(true, new SafepointAction() {
@Override
public Void apply(DynamicObject rubyThread, Node currentNode) {
public void run(DynamicObject rubyThread, Node currentNode) {
if (Thread.currentThread() == thread) {
action.apply(rubyThread, currentNode);
action.run(rubyThread, currentNode);
}
return null;
}
});
}

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

Original file line number Diff line number Diff line change
@@ -20,9 +20,8 @@
import org.jruby.truffle.RubyContext;
import org.jruby.truffle.core.Layouts;
import org.jruby.truffle.core.hash.Entry;
import org.jruby.truffle.language.SafepointAction;
import org.jruby.truffle.language.arguments.RubyArguments;
import org.jruby.util.func.Function2;

import java.util.ArrayDeque;
import java.util.Collection;
import java.util.Deque;
@@ -36,11 +35,9 @@ public static Set<DynamicObject> stopAndGetAllObjects(Node currentNode, final Ru

final Thread stoppingThread = Thread.currentThread();

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

context.getSafepointManager().pauseAllThreadsAndExecute(currentNode, false, new SafepointAction() {
@Override
public Void apply(DynamicObject thread, Node currentNode) {
public void run(DynamicObject thread, Node currentNode) {
synchronized (visited) {
final Deque<DynamicObject> stack = new ArrayDeque<>();

@@ -75,8 +72,6 @@ public Object visitFrame(FrameInstance frameInstance) {
stack.addAll(ObjectGraph.getAdjacentObjects(object));
}
}

return null;
}
}
});
@@ -89,20 +84,15 @@ public static Set<DynamicObject> stopAndGetRootObjects(Node currentNode, final R

final Thread stoppingThread = Thread.currentThread();

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

context.getSafepointManager().pauseAllThreadsAndExecute(currentNode, false, new SafepointAction() {
@Override
public Void apply(DynamicObject thread, Node currentNode) {
public void run(DynamicObject thread, Node currentNode) {
objects.add(thread);

if (Thread.currentThread() == stoppingThread) {
visitContextRoots(context, objects);
}

return null;
}

});

return objects;
Original file line number Diff line number Diff line change
@@ -18,10 +18,9 @@
import com.sun.net.httpserver.HttpServer;
import org.jruby.truffle.RubyContext;
import org.jruby.truffle.core.Layouts;
import org.jruby.truffle.language.SafepointAction;
import org.jruby.truffle.language.backtrace.Backtrace;
import org.jruby.truffle.language.backtrace.BacktraceFormatter;
import org.jruby.util.func.Function2;

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
@@ -61,10 +60,9 @@ public void handle(HttpExchange httpExchange) {
try {
final StringBuilder builder = new StringBuilder();

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

context.getSafepointManager().pauseAllThreadsAndExecuteFromNonRubyThread(false, new SafepointAction() {
@Override
public Void apply(DynamicObject thread, Node currentNode) {
public void run(DynamicObject thread, Node currentNode) {
try {
Backtrace backtrace = context.getCallStack().getBacktrace(null);

@@ -83,10 +81,7 @@ public Void apply(DynamicObject thread, Node currentNode) {
} catch (Throwable e) {
e.printStackTrace();
}

return null;
}

});

final byte[] bytes = builder.toString().getBytes("UTF-8");
@@ -113,12 +108,11 @@ 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 Function2<Void, DynamicObject, Node>() {
context.getSafepointManager().pauseThreadAndExecuteLaterFromNonRubyThread(mainThread, new SafepointAction() {
@Override
public Void apply(DynamicObject thread, final Node currentNode) {
public void run(DynamicObject thread, final Node currentNode) {
new SimpleShell(context).run(Truffle.getRuntime().getCurrentFrame()
.getFrame(FrameInstance.FrameAccess.MATERIALIZE, true).materialize(), currentNode);
return null;
}
});