Skip to content

Commit

Permalink
[Truffle] Thread#kill.
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisseaton committed Nov 9, 2014
1 parent a1a3efc commit 0e93ab9
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 10 deletions.
30 changes: 30 additions & 0 deletions core/src/main/java/org/jruby/truffle/nodes/core/ThreadNodes.java
Expand Up @@ -19,6 +19,7 @@
import org.jruby.truffle.runtime.core.RubyProc;
import org.jruby.truffle.runtime.core.RubyString;
import org.jruby.truffle.runtime.core.RubyThread;
import org.jruby.truffle.runtime.util.Consumer;

@CoreClass(name = "Thread")
public abstract class ThreadNodes {
Expand Down Expand Up @@ -97,6 +98,35 @@ public RubyNilClass exit() {

}

@CoreMethod(names = "kill")
public abstract static class KillNode extends CoreMethodNode {

public KillNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
}

public KillNode(KillNode prev) {
super(prev);
}

@Specialization
public RubyThread kill(final RubyThread thread) {
getContext().getSafepointManager().pauseAllThreadsAndExecute(new Consumer<Boolean>() {

@Override
public void accept(Boolean isPausingThread) {
if (getContext().getThreadManager().getCurrentThread() == thread) {
throw new ThreadExitException();
}
}

});

return thread;
}

}

@CoreMethod(names = "initialize", needsBlock = true)
public abstract static class InitializeNode extends CoreMethodNode {

Expand Down
Expand Up @@ -219,7 +219,7 @@ public boolean visit(RubyBasicObject object) {
context.getSafepointManager().pauseAllThreadsAndExecute(new Consumer<Boolean>() {

@Override
public void accept(Boolean pausingThread) {
public void accept(Boolean isPausingThread) {
synchronized (liveObjects) {
context.getCoreLibrary().getGlobalVariablesObject().visitObjectGraph(visitor);
context.getCoreLibrary().getMainObject().visitObjectGraph(visitor);
Expand Down
Expand Up @@ -66,13 +66,13 @@ public void poll() {
try {
assumption.check();
} catch (InvalidAssumptionException e) {

waitOnBarrier();

action.accept(false);


waitOnBarrier();
try {
action.accept(false);
} finally {
waitOnBarrier();
}
}
}

Expand All @@ -90,11 +90,13 @@ public void pauseAllThreadsAndExecute(final Consumer<Boolean> action) {

waitOnBarrier();

action.accept(true);

waitOnBarrier();

assumption = Truffle.getRuntime().createAssumption();

try {
action.accept(true);
} finally {
waitOnBarrier();
}
} finally {
lock.unlock();
}
Expand Down

0 comments on commit 0e93ab9

Please sign in to comment.