Skip to content

Commit 21779aa

Browse files
committed
python threads: switch to using the Truffle createThread API
1 parent 88f8918 commit 21779aa

File tree

4 files changed

+25
-19
lines changed

4 files changed

+25
-19
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/ThreadModuleBuiltins.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,14 +128,24 @@ long getStackSize(@SuppressWarnings("unused") PNone stackSize) {
128128
return getContext().getPythonThreadStackSize();
129129
}
130130

131-
@Specialization
132-
long getStackSize(long stackSize,
133-
@Cached("createBinaryProfile()") ConditionProfile invalidSizeProfile) {
131+
private long setAndGetStackSizeInternal(long stackSize, ConditionProfile invalidSizeProfile) {
134132
if (invalidSizeProfile.profile(stackSize < 0)) {
135133
throw raise(ValueError, "size must be 0 or a positive value");
136134
}
137135
return getContext().getAndSetPythonsThreadStackSize(stackSize);
138136
}
137+
138+
@Specialization
139+
long getStackSize(int stackSize,
140+
@Cached("createBinaryProfile()") ConditionProfile invalidSizeProfile) {
141+
return setAndGetStackSizeInternal(stackSize, invalidSizeProfile);
142+
}
143+
144+
@Specialization
145+
long getStackSize(long stackSize,
146+
@Cached("createBinaryProfile()") ConditionProfile invalidSizeProfile) {
147+
return setAndGetStackSizeInternal(stackSize, invalidSizeProfile);
148+
}
139149
}
140150

141151
@Builtin(name = "start_new_thread", minNumOfPositionalArgs = 3, maxNumOfPositionalArgs = 4, constructsClass = PythonBuiltinClassType.PThread)

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/thread/PThread.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,9 @@ public class PThread extends PythonBuiltinObject {
4848
public final static String GRAALPYTHON_THREADS = "GRAALPYTHON_THREADS";
4949
private final Thread thread;
5050

51-
public PThread(LazyPythonClass cls, ThreadGroup group, Runnable runnable) {
52-
this(cls, group, 0, runnable);
53-
}
54-
55-
public PThread(LazyPythonClass cls, ThreadGroup group, long stackSize, Runnable runnable) {
51+
public PThread(LazyPythonClass cls, Thread thread) {
5652
super(cls);
57-
this.thread = new Thread(group, runnable, "graalpython-thread-" + group.activeCount(), stackSize);
53+
this.thread = thread;
5854
}
5955

6056
@TruffleBoundary

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/thread/CreateThreadNode.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
import com.oracle.graal.python.nodes.call.CallNode;
5252
import com.oracle.graal.python.nodes.truffle.PythonTypes;
5353
import com.oracle.graal.python.runtime.PythonContext;
54-
import com.oracle.truffle.api.TruffleContext;
54+
import com.oracle.truffle.api.TruffleLanguage;
5555
import com.oracle.truffle.api.dsl.ImportStatic;
5656
import com.oracle.truffle.api.dsl.TypeSystemReference;
5757
import com.oracle.truffle.api.frame.VirtualFrame;
@@ -65,17 +65,17 @@ public class CreateThreadNode extends PNodeWithContext {
6565

6666
public PThread execute(VirtualFrame frame, PythonClass cls, Object callable, Object args, Object kwargs) {
6767
PythonContext context = getContext();
68-
TruffleContext truffleContext = context.getEnv().getContext();
68+
TruffleLanguage.Env env = context.getEnv();
6969

7070
// TODO: python thread stack size != java thread stack size
7171
// ignore setting the stack size for the moment
72-
return factory().createThread(cls, context.getThreadGroup(), 0, () -> {
73-
Object previous = truffleContext.enter();
72+
Thread thread = env.createThread(() -> {
7473
Object[] arguments = getArgsNode.executeWith(args);
7574
PKeyword[] keywords = getKwArgsNode.executeWith(kwargs);
7675
callNode.execute(frame, callable, arguments, keywords);
77-
truffleContext.leave(previous);
78-
});
76+
}, env.getContext(), context.getThreadGroup());
77+
78+
return factory().createPythonThread(cls, thread);
7979
}
8080

8181
public static CreateThreadNode create() {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/object/PythonObjectFactory.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -768,11 +768,11 @@ public PRLock createRLock(PythonClass cls) {
768768
return trace(new PRLock(cls));
769769
}
770770

771-
public PThread createThread(ThreadGroup group, long stackSize, Runnable runnable) {
772-
return trace(new PThread(PythonBuiltinClassType.PThread, group, stackSize, runnable));
771+
public PThread createPythonThread(Thread thread) {
772+
return trace(new PThread(PythonBuiltinClassType.PThread, thread));
773773
}
774774

775-
public PThread createThread(PythonClass cls, ThreadGroup group, long stackSize, Runnable runnable) {
776-
return trace(new PThread(cls, group, stackSize, runnable));
775+
public PThread createPythonThread(PythonClass cls, Thread thread) {
776+
return trace(new PThread(cls, thread));
777777
}
778778
}

0 commit comments

Comments
 (0)