29
29
var LibraryPThread = {
30
30
$PThread__postset : 'PThread.init();' ,
31
31
$PThread__deps : [ '_emscripten_thread_init' ,
32
- '$killThread' ,
33
32
'$cancelThread' , '$cleanupThread' , '$zeroMemory' ,
34
33
'$spawnThread' ,
35
34
'_emscripten_thread_free_data' ,
@@ -158,10 +157,7 @@ var LibraryPThread = {
158
157
err ( 'terminateAllThreads' ) ;
159
158
#endif
160
159
for ( var t in PThread . pthreads ) {
161
- var pthread = PThread . pthreads [ t ] ;
162
- if ( pthread && pthread . worker ) {
163
- PThread . returnWorkerToPool ( pthread . worker ) ;
164
- }
160
+ cleanupThread ( t , true ) ;
165
161
}
166
162
167
163
#if ASSERTIONS
@@ -181,7 +177,7 @@ var LibraryPThread = {
181
177
}
182
178
PThread . unusedWorkers = [ ] ;
183
179
} ,
184
- returnWorkerToPool : function ( worker ) {
180
+ returnWorkerToPool : function ( worker , terminate ) {
185
181
// We don't want to run main thread queued calls here, since we are doing
186
182
// some operations that leave the worker queue in an invalid state until
187
183
// we are completely done (it would be bad if free() ends up calling a
@@ -190,9 +186,13 @@ var LibraryPThread = {
190
186
// we are all done.
191
187
var pthread_ptr = worker . pthread . threadInfoStruct ;
192
188
delete PThread . pthreads [ pthread_ptr ] ;
193
- // Note: worker is intentionally not terminated so the pool can
194
- // dynamically grow.
195
- PThread . unusedWorkers . push ( worker ) ;
189
+ if ( terminate ) {
190
+ worker . terminate ( ) ;
191
+ } else {
192
+ // No termination needed? Return it to the worker pool as an
193
+ // unused worker so the pool can dynamically grow.
194
+ PThread . unusedWorkers . push ( worker ) ;
195
+ }
196
196
PThread . runningWorkers . splice ( PThread . runningWorkers . indexOf ( worker ) , 1 ) ;
197
197
// Not a running Worker anymore
198
198
// Detach the worker from the pthread object, and return it to the
@@ -256,9 +256,7 @@ var LibraryPThread = {
256
256
} else if ( cmd === 'spawnThread' ) {
257
257
spawnThread ( d ) ;
258
258
} else if ( cmd === 'cleanupThread' ) {
259
- cleanupThread ( d [ 'thread' ] ) ;
260
- } else if ( cmd === 'killThread' ) {
261
- killThread ( d [ 'thread' ] ) ;
259
+ cleanupThread ( d [ 'thread' ] , d [ 'terminate' ] ) ;
262
260
} else if ( cmd === 'cancelThread' ) {
263
261
cancelThread ( d [ 'thread' ] ) ;
264
262
} else if ( cmd === 'loaded' ) {
@@ -435,44 +433,24 @@ var LibraryPThread = {
435
433
}
436
434
} ,
437
435
438
- $killThread__deps: [ '_emscripten_thread_free_data' ] ,
439
- $killThread : function ( pthread_ptr ) {
440
- #if PTHREADS_DEBUG
441
- err ( 'killThread ' + ptrToString ( pthread_ptr ) ) ;
442
- #endif
443
- #if ASSERTIONS
444
- assert ( ! ENVIRONMENT_IS_PTHREAD , 'Internal Error! killThread() can only ever be called from main application thread!' ) ;
445
- assert ( pthread_ptr , 'Internal Error! Null pthread_ptr in killThread!' ) ;
446
- #endif
447
- var pthread = PThread . pthreads [ pthread_ptr ] ;
448
- delete PThread . pthreads [ pthread_ptr ] ;
449
- pthread . worker . terminate ( ) ;
450
- __emscripten_thread_free_data ( pthread_ptr ) ;
451
- // The worker was completely nuked (not just the pthread execution it was hosting), so remove it from running workers
452
- // but don't put it back to the pool.
453
- PThread . runningWorkers . splice ( PThread . runningWorkers . indexOf ( pthread . worker ) , 1 ) ; // Not a running Worker anymore.
454
- pthread . worker . pthread = undefined ;
455
- } ,
456
-
457
436
__emscripten_thread_cleanup: function ( thread ) {
458
437
// Called when a thread needs to be cleaned up so it can be reused.
459
438
// A thread is considered reusable when it either returns from its
460
439
// entry point, calls pthread_exit, or acts upon a cancellation.
461
440
// Detached threads are responsible for calling this themselves,
462
441
// otherwise pthread_join is responsible for calling this.
463
- if ( ! ENVIRONMENT_IS_PTHREAD ) cleanupThread ( thread ) ;
464
- else postMessage ( { 'cmd' : 'cleanupThread' , 'thread' : thread } ) ;
442
+ if ( ! ENVIRONMENT_IS_PTHREAD ) cleanupThread ( thread , false ) ;
443
+ else postMessage ( { 'cmd' : 'cleanupThread' , 'thread' : thread , 'terminate' : false } ) ;
465
444
} ,
466
445
467
- $cleanupThread : function ( pthread_ptr ) {
446
+ $cleanupThread : function ( pthread_ptr , terminate ) {
468
447
#if ASSERTIONS
469
448
assert ( ! ENVIRONMENT_IS_PTHREAD , 'Internal Error! cleanupThread() can only ever be called from main application thread!' ) ;
470
449
assert ( pthread_ptr , 'Internal Error! Null pthread_ptr in cleanupThread!' ) ;
471
450
#endif
472
451
var pthread = PThread . pthreads [ pthread_ptr ] ;
473
452
assert ( pthread ) ;
474
- var worker = pthread . worker ;
475
- PThread . returnWorkerToPool ( worker ) ;
453
+ PThread . returnWorkerToPool ( pthread . worker , terminate ) ;
476
454
} ,
477
455
478
456
#if MAIN_MODULE
@@ -799,8 +777,8 @@ var LibraryPThread = {
799
777
if ( ! ENVIRONMENT_IS_PTHREAD ) cancelThread ( thread ) ;
800
778
else postMessage ( { 'cmd' : 'cancelThread' , 'thread' : thread } ) ;
801
779
} else {
802
- if ( ! ENVIRONMENT_IS_PTHREAD ) killThread ( thread ) ;
803
- else postMessage ( { 'cmd' : 'killThread ' , 'thread' : thread } ) ;
780
+ if ( ! ENVIRONMENT_IS_PTHREAD ) cleanupThread ( thread , true ) ;
781
+ else postMessage ( { 'cmd' : 'cleanupThread ' , 'thread' : thread , 'terminate' : true } ) ;
804
782
}
805
783
return 0 ;
806
784
} ,
0 commit comments