188
188
import com .oracle .truffle .api .TruffleLanguage .Env ;
189
189
import com .oracle .truffle .api .TruffleLogger ;
190
190
import com .oracle .truffle .api .TruffleSafepoint ;
191
+ import com .oracle .truffle .api .TruffleThreadBuilder ;
191
192
import com .oracle .truffle .api .dsl .Bind ;
192
193
import com .oracle .truffle .api .dsl .GenerateInline ;
193
194
import com .oracle .truffle .api .dsl .GenerateUncached ;
@@ -1236,7 +1237,7 @@ public SharedMultiprocessingData getSharedMultiprocessingData() {
1236
1237
1237
1238
public long spawnTruffleContext (int fd , int sentinel , int [] fdsToKeep ) {
1238
1239
ChildContextData data = new ChildContextData (isChildContext () ? childContextData .parentCtx : this );
1239
- Builder builder = data .parentCtx .env .newInnerContextBuilder ().//
1240
+ Builder childContextBuilder = data .parentCtx .env .newInnerContextBuilder ().//
1240
1241
forceSharing (getOption (PythonOptions .ForceSharingForInnerContexts )).//
1241
1242
inheritAllAccess (true ).//
1242
1243
initializeCreatorContext (true ).//
@@ -1245,7 +1246,12 @@ public long spawnTruffleContext(int fd, int sentinel, int[] fdsToKeep) {
1245
1246
// with that. Gives "OSError: [Errno 9] Bad file number"
1246
1247
// option("python.PosixModuleBackend", "java").//
1247
1248
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 ();
1249
1255
long tid = PThread .getThreadId (thread );
1250
1256
getSharedMultiprocessingData ().putChildContextThread (tid , thread );
1251
1257
getSharedMultiprocessingData ().putChildContextData (tid , data );
@@ -1270,42 +1276,35 @@ public synchronized List<Integer> getChildContextFDs() {
1270
1276
return childContextFDs ;
1271
1277
}
1272
1278
1273
- private static class ChildContextThread implements Runnable {
1279
+ private static final class ChildContextThread implements Runnable {
1274
1280
private static final TruffleLogger MULTIPROCESSING_LOGGER = PythonLanguage .getLogger (ChildContextThread .class );
1275
1281
private static final Source MULTIPROCESSING_SOURCE = Source .newBuilder (PythonLanguage .ID ,
1276
1282
"from multiprocessing.popen_truffleprocess import spawn_truffleprocess; spawn_truffleprocess(fd, sentinel)" ,
1277
1283
"<spawned-child-context>" ).internal (true ).build ();
1278
1284
1279
1285
private final int fd ;
1280
1286
private final ChildContextData data ;
1281
- private final Builder builder ;
1282
1287
private final int sentinel ;
1283
1288
1284
- public ChildContextThread (int fd , int sentinel , ChildContextData data , Builder builder ) {
1289
+ public ChildContextThread (int fd , int sentinel , ChildContextData data ) {
1285
1290
this .fd = fd ;
1286
1291
this .data = data ;
1287
- this .builder = builder ;
1288
1292
this .sentinel = sentinel ;
1289
1293
}
1290
1294
1291
1295
@ Override
1292
1296
public void run () {
1293
1297
try {
1294
1298
MULTIPROCESSING_LOGGER .fine ("starting spawned child context" );
1295
- TruffleContext ctx = builder .build ();
1296
- data .setTruffleContext (ctx );
1297
- Object parent = ctx .enter (null );
1298
1299
CallTarget ct = PythonContext .get (null ).getEnv ().parsePublic (MULTIPROCESSING_SOURCE , "fd" , "sentinel" );
1299
1300
try {
1300
1301
data .running .countDown ();
1301
1302
Object res = ct .call (fd , sentinel );
1302
1303
int exitCode = CastToJavaIntLossyNode .executeUncached (res );
1303
1304
data .setExitCode (exitCode );
1304
1305
} finally {
1305
- ctx .leave (null , parent );
1306
1306
if (data .compareAndSetExiting (false , true )) {
1307
1307
try {
1308
- ctx .close ();
1309
1308
MULTIPROCESSING_LOGGER .log (Level .FINE , "closed spawned child context" );
1310
1309
} catch (Throwable t ) {
1311
1310
MULTIPROCESSING_LOGGER .log (Level .FINE , "exception while closing spawned child context" , t );
0 commit comments