Skip to content

Commit 0e93ab9

Browse files
committed
[Truffle] Thread#kill.
1 parent a1a3efc commit 0e93ab9

File tree

3 files changed

+42
-10
lines changed

3 files changed

+42
-10
lines changed

core/src/main/java/org/jruby/truffle/nodes/core/ThreadNodes.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.jruby.truffle.runtime.core.RubyProc;
2020
import org.jruby.truffle.runtime.core.RubyString;
2121
import org.jruby.truffle.runtime.core.RubyThread;
22+
import org.jruby.truffle.runtime.util.Consumer;
2223

2324
@CoreClass(name = "Thread")
2425
public abstract class ThreadNodes {
@@ -97,6 +98,35 @@ public RubyNilClass exit() {
9798

9899
}
99100

101+
@CoreMethod(names = "kill")
102+
public abstract static class KillNode extends CoreMethodNode {
103+
104+
public KillNode(RubyContext context, SourceSection sourceSection) {
105+
super(context, sourceSection);
106+
}
107+
108+
public KillNode(KillNode prev) {
109+
super(prev);
110+
}
111+
112+
@Specialization
113+
public RubyThread kill(final RubyThread thread) {
114+
getContext().getSafepointManager().pauseAllThreadsAndExecute(new Consumer<Boolean>() {
115+
116+
@Override
117+
public void accept(Boolean isPausingThread) {
118+
if (getContext().getThreadManager().getCurrentThread() == thread) {
119+
throw new ThreadExitException();
120+
}
121+
}
122+
123+
});
124+
125+
return thread;
126+
}
127+
128+
}
129+
100130
@CoreMethod(names = "initialize", needsBlock = true)
101131
public abstract static class InitializeNode extends CoreMethodNode {
102132

core/src/main/java/org/jruby/truffle/runtime/subsystems/ObjectSpaceManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ public boolean visit(RubyBasicObject object) {
219219
context.getSafepointManager().pauseAllThreadsAndExecute(new Consumer<Boolean>() {
220220

221221
@Override
222-
public void accept(Boolean pausingThread) {
222+
public void accept(Boolean isPausingThread) {
223223
synchronized (liveObjects) {
224224
context.getCoreLibrary().getGlobalVariablesObject().visitObjectGraph(visitor);
225225
context.getCoreLibrary().getMainObject().visitObjectGraph(visitor);

core/src/main/java/org/jruby/truffle/runtime/subsystems/SafepointManager.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,13 @@ public void poll() {
6666
try {
6767
assumption.check();
6868
} catch (InvalidAssumptionException e) {
69-
7069
waitOnBarrier();
7170

72-
action.accept(false);
73-
74-
75-
waitOnBarrier();
71+
try {
72+
action.accept(false);
73+
} finally {
74+
waitOnBarrier();
75+
}
7676
}
7777
}
7878

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

9191
waitOnBarrier();
9292

93-
action.accept(true);
94-
95-
waitOnBarrier();
96-
9793
assumption = Truffle.getRuntime().createAssumption();
94+
95+
try {
96+
action.accept(true);
97+
} finally {
98+
waitOnBarrier();
99+
}
98100
} finally {
99101
lock.unlock();
100102
}

0 commit comments

Comments
 (0)