Skip to content

Commit 7b89b20

Browse files
committed
Use TruffleThreadBuilder instead of deprecated createThread API
1 parent b533f6a commit 7b89b20

File tree

3 files changed

+18
-16
lines changed

3 files changed

+18
-16
lines changed

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
8686
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
8787
import com.oracle.truffle.api.TruffleLanguage;
88+
import com.oracle.truffle.api.TruffleThreadBuilder;
8889
import com.oracle.truffle.api.dsl.Bind;
8990
import com.oracle.truffle.api.dsl.Cached;
9091
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
@@ -228,15 +229,15 @@ long start(VirtualFrame frame, Object cls, Object callable, Object args, Object
228229

229230
// TODO: python thread stack size != java thread stack size
230231
// ignore setting the stack size for the moment
231-
Thread thread = env.createThread(() -> {
232+
TruffleThreadBuilder threadBuilder = env.newTruffleThreadBuilder(() -> {
232233
try (GilNode.UncachedAcquire gil = GilNode.uncachedAcquire()) {
233234

234235
// the increment is protected by the gil
235236
int curCount = (int) HiddenAttr.ReadNode.executeUncached(threadModule, THREAD_COUNT, 0);
236237
HiddenAttr.WriteNode.executeUncached(threadModule, THREAD_COUNT, curCount + 1);
237238
try {
238239
// n.b.: It is important to pass 'null' frame here because each thread has
239-
// it's own stack and if we would pass the current frame, this would be
240+
// its own stack and if we would pass the current frame, this would be
240241
// connected as a caller which is incorrect. However, the thread-local
241242
// 'topframeref' is initialized with EMPTY which will be picked up.
242243
callNode.execute(null, callable, arguments, keywords);
@@ -251,9 +252,9 @@ long start(VirtualFrame frame, Object cls, Object callable, Object args, Object
251252
HiddenAttr.WriteNode.executeUncached(threadModule, THREAD_COUNT, curCount - 1);
252253
}
253254
}
254-
}, env.getContext(), context.getThreadGroup());
255+
}).context(env.getContext()).threadGroup(context.getThreadGroup());
255256

256-
PThread pThread = factory.createPythonThread(cls, thread);
257+
PThread pThread = factory.createPythonThread(cls, threadBuilder.build());
257258
pThread.start();
258259
return pThread.getId();
259260
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/hpy/GraalHPyContext.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@
105105
import com.oracle.truffle.api.RootCallTarget;
106106
import com.oracle.truffle.api.TruffleLanguage.Env;
107107
import com.oracle.truffle.api.TruffleLogger;
108+
import com.oracle.truffle.api.TruffleThreadBuilder;
108109
import com.oracle.truffle.api.dsl.GenerateInline;
109110
import com.oracle.truffle.api.dsl.GenerateUncached;
110111
import com.oracle.truffle.api.dsl.ImportStatic;
@@ -1153,7 +1154,8 @@ private ReferenceQueue<Object> createReferenceQueue() {
11531154
// lazily register the runnable that concurrently collects the queued references
11541155
Env env = getContext().getEnv();
11551156
if (env.isCreateThreadAllowed()) {
1156-
Thread thread = env.createThread(new GraalHPyReferenceCleanerRunnable(referenceQueue), null, getContext().getThreadGroup());
1157+
TruffleThreadBuilder truffleThreadBuilder = env.newTruffleThreadBuilder(new GraalHPyReferenceCleanerRunnable(referenceQueue)).threadGroup(getContext().getThreadGroup());
1158+
Thread thread = truffleThreadBuilder.build();
11571159
// Make the cleaner thread a daemon; it should not prevent JVM shutdown.
11581160
thread.setDaemon(true);
11591161
thread.start();

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/PythonContext.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@
188188
import com.oracle.truffle.api.TruffleLanguage.Env;
189189
import com.oracle.truffle.api.TruffleLogger;
190190
import com.oracle.truffle.api.TruffleSafepoint;
191+
import com.oracle.truffle.api.TruffleThreadBuilder;
191192
import com.oracle.truffle.api.dsl.Bind;
192193
import com.oracle.truffle.api.dsl.GenerateInline;
193194
import com.oracle.truffle.api.dsl.GenerateUncached;
@@ -1236,7 +1237,7 @@ public SharedMultiprocessingData getSharedMultiprocessingData() {
12361237

12371238
public long spawnTruffleContext(int fd, int sentinel, int[] fdsToKeep) {
12381239
ChildContextData data = new ChildContextData(isChildContext() ? childContextData.parentCtx : this);
1239-
Builder builder = data.parentCtx.env.newInnerContextBuilder().//
1240+
Builder childContextBuilder = data.parentCtx.env.newInnerContextBuilder().//
12401241
forceSharing(getOption(PythonOptions.ForceSharingForInnerContexts)).//
12411242
inheritAllAccess(true).//
12421243
initializeCreatorContext(true).//
@@ -1245,7 +1246,12 @@ public long spawnTruffleContext(int fd, int sentinel, int[] fdsToKeep) {
12451246
// with that. Gives "OSError: [Errno 9] Bad file number"
12461247
// option("python.PosixModuleBackend", "java").//
12471248
config(PythonContext.CHILD_CONTEXT_DATA, data);
1248-
Thread thread = data.parentCtx.env.createThread(new ChildContextThread(fd, sentinel, data, builder));
1249+
1250+
TruffleContext childContext = childContextBuilder.build();
1251+
data.setTruffleContext(childContext);
1252+
ChildContextThread childContextRunnable = new ChildContextThread(fd, sentinel, data);
1253+
TruffleThreadBuilder threadBuilder = data.parentCtx.env.newTruffleThreadBuilder(childContextRunnable).context(childContext);
1254+
Thread thread = threadBuilder.build();
12491255
long tid = PThread.getThreadId(thread);
12501256
getSharedMultiprocessingData().putChildContextThread(tid, thread);
12511257
getSharedMultiprocessingData().putChildContextData(tid, data);
@@ -1270,42 +1276,35 @@ public synchronized List<Integer> getChildContextFDs() {
12701276
return childContextFDs;
12711277
}
12721278

1273-
private static class ChildContextThread implements Runnable {
1279+
private static final class ChildContextThread implements Runnable {
12741280
private static final TruffleLogger MULTIPROCESSING_LOGGER = PythonLanguage.getLogger(ChildContextThread.class);
12751281
private static final Source MULTIPROCESSING_SOURCE = Source.newBuilder(PythonLanguage.ID,
12761282
"from multiprocessing.popen_truffleprocess import spawn_truffleprocess; spawn_truffleprocess(fd, sentinel)",
12771283
"<spawned-child-context>").internal(true).build();
12781284

12791285
private final int fd;
12801286
private final ChildContextData data;
1281-
private final Builder builder;
12821287
private final int sentinel;
12831288

1284-
public ChildContextThread(int fd, int sentinel, ChildContextData data, Builder builder) {
1289+
public ChildContextThread(int fd, int sentinel, ChildContextData data) {
12851290
this.fd = fd;
12861291
this.data = data;
1287-
this.builder = builder;
12881292
this.sentinel = sentinel;
12891293
}
12901294

12911295
@Override
12921296
public void run() {
12931297
try {
12941298
MULTIPROCESSING_LOGGER.fine("starting spawned child context");
1295-
TruffleContext ctx = builder.build();
1296-
data.setTruffleContext(ctx);
1297-
Object parent = ctx.enter(null);
12981299
CallTarget ct = PythonContext.get(null).getEnv().parsePublic(MULTIPROCESSING_SOURCE, "fd", "sentinel");
12991300
try {
13001301
data.running.countDown();
13011302
Object res = ct.call(fd, sentinel);
13021303
int exitCode = CastToJavaIntLossyNode.executeUncached(res);
13031304
data.setExitCode(exitCode);
13041305
} finally {
1305-
ctx.leave(null, parent);
13061306
if (data.compareAndSetExiting(false, true)) {
13071307
try {
1308-
ctx.close();
13091308
MULTIPROCESSING_LOGGER.log(Level.FINE, "closed spawned child context");
13101309
} catch (Throwable t) {
13111310
MULTIPROCESSING_LOGGER.log(Level.FINE, "exception while closing spawned child context", t);

0 commit comments

Comments
 (0)