@@ -51,6 +51,10 @@ var LibraryPThread = {
51
51
// Contains all Workers that are currently hosting an active pthread.
52
52
runningWorkers : [ ] ,
53
53
tlsInitFunctions : [ ] ,
54
+ // Maps pthread_t pointers to the workers on which they are running. For
55
+ // the reverse mapping, each worker has a `pthread_ptr` when its running a
56
+ // pthread.
57
+ pthreads : { } ,
54
58
#if PTHREADS_DEBUG
55
59
nextWorkerID : 1 ,
56
60
debugInit : function ( ) {
@@ -114,8 +118,6 @@ var LibraryPThread = {
114
118
noExitRuntime = false ;
115
119
#endif
116
120
} ,
117
- // Maps pthread_t to pthread info objects
118
- pthreads : { } ,
119
121
120
122
#if PTHREADS_PROFILING
121
123
getThreadName : function ( pthreadPtr ) {
@@ -158,9 +160,9 @@ var LibraryPThread = {
158
160
err ( 'terminateAllThreads' ) ;
159
161
#endif
160
162
for ( var t in PThread . pthreads ) {
161
- var pthread = PThread . pthreads [ t ] ;
162
- if ( pthread && pthread . worker ) {
163
- PThread . returnWorkerToPool ( pthread . worker ) ;
163
+ var worker = PThread . pthreads [ t ] ;
164
+ if ( worker ) {
165
+ PThread . returnWorkerToPool ( worker ) ;
164
166
}
165
167
}
166
168
@@ -175,7 +177,7 @@ var LibraryPThread = {
175
177
var worker = PThread . unusedWorkers [ i ] ;
176
178
#if ASSERTIONS
177
179
// This Worker should not be hosting a pthread at this time.
178
- assert ( ! worker . pthread ) ;
180
+ assert ( ! worker . pthread_ptr ) ;
179
181
#endif
180
182
worker . terminate ( ) ;
181
183
}
@@ -188,7 +190,7 @@ var LibraryPThread = {
188
190
// queued pthread_create which looks at the global data structures we are
189
191
// modifying). To achieve that, defer the free() til the very end, when
190
192
// we are all done.
191
- var pthread_ptr = worker . pthread . threadInfoStruct ;
193
+ var pthread_ptr = worker . pthread_ptr ;
192
194
delete PThread . pthreads [ pthread_ptr ] ;
193
195
// Note: worker is intentionally not terminated so the pool can
194
196
// dynamically grow.
@@ -197,7 +199,7 @@ var LibraryPThread = {
197
199
// Not a running Worker anymore
198
200
// Detach the worker from the pthread object, and return it to the
199
201
// worker pool as an unused worker.
200
- worker . pthread = undefined ;
202
+ worker . pthread_ptr = 0 ;
201
203
202
204
// Finally, free the underlying (and now-unused) pthread structure in
203
205
// linear memory.
@@ -237,13 +239,13 @@ var LibraryPThread = {
237
239
// HTML5 DOM events handlers such as
238
240
// emscripten_set_mousemove_callback()), so keep track in a globally
239
241
// accessible variable about the thread that initiated the proxying.
240
- if ( worker . pthread ) PThread . currentProxiedOperationCallerThread = worker . pthread . threadInfoStruct ;
242
+ if ( worker . pthread_ptr ) PThread . currentProxiedOperationCallerThread = worker . pthread_ptr ;
241
243
242
244
// If this message is intended to a recipient that is not the main thread, forward it to the target thread.
243
245
if ( d [ 'targetThread' ] && d [ 'targetThread' ] != _pthread_self ( ) ) {
244
- var thread = PThread . pthreads [ d . targetThread ] ;
245
- if ( thread ) {
246
- thread . worker . postMessage ( d , d [ 'transferList' ] ) ;
246
+ var targetWorker = PThread . pthreads [ d . targetThread ] ;
247
+ if ( targetWorker ) {
248
+ targetWorker . postMessage ( d , d [ 'transferList' ] ) ;
247
249
} else {
248
250
err ( 'Internal error! Worker sent a message "' + cmd + '" to target pthread ' + d [ 'targetThread' ] + ', but that thread no longer exists!' ) ;
249
251
}
@@ -295,11 +297,8 @@ var LibraryPThread = {
295
297
worker . onerror = ( e ) => {
296
298
var message = 'worker sent an error!' ;
297
299
#if ASSERTIONS
298
- if ( worker . pthread ) {
299
- var pthread_ptr = worker . pthread . threadInfoStruct ;
300
- if ( pthread_ptr ) {
301
- message = 'Pthread ' + ptrToString ( pthread_ptr ) + ' sent an error!' ;
302
- }
300
+ if ( worker . pthread_ptr ) {
301
+ message = 'Pthread ' + ptrToString ( worker . pthread_ptr ) + ' sent an error!' ;
303
302
}
304
303
#endif
305
304
err ( message + ' ' + e . filename + ':' + e . lineno + ': ' + e . message ) ;
@@ -444,14 +443,14 @@ var LibraryPThread = {
444
443
assert ( ! ENVIRONMENT_IS_PTHREAD , 'Internal Error! killThread() can only ever be called from main application thread!' ) ;
445
444
assert ( pthread_ptr , 'Internal Error! Null pthread_ptr in killThread!' ) ;
446
445
#endif
447
- var pthread = PThread . pthreads [ pthread_ptr ] ;
446
+ var worker = PThread . pthreads [ pthread_ptr ] ;
448
447
delete PThread . pthreads [ pthread_ptr ] ;
449
- pthread . worker . terminate ( ) ;
448
+ worker . terminate ( ) ;
450
449
__emscripten_thread_free_data ( pthread_ptr ) ;
451
450
// The worker was completely nuked (not just the pthread execution it was hosting), so remove it from running workers
452
451
// 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 ;
452
+ PThread . runningWorkers . splice ( PThread . runningWorkers . indexOf ( worker ) , 1 ) ; // Not a running Worker anymore.
453
+ worker . pthread_ptr = 0 ;
455
454
} ,
456
455
457
456
__emscripten_thread_cleanup : function ( thread ) {
@@ -469,9 +468,8 @@ var LibraryPThread = {
469
468
assert ( ! ENVIRONMENT_IS_PTHREAD , 'Internal Error! cleanupThread() can only ever be called from main application thread!' ) ;
470
469
assert ( pthread_ptr , 'Internal Error! Null pthread_ptr in cleanupThread!' ) ;
471
470
#endif
472
- var pthread = PThread . pthreads [ pthread_ptr ] ;
473
- assert ( pthread ) ;
474
- var worker = pthread . worker ;
471
+ var worker = PThread . pthreads [ pthread_ptr ] ;
472
+ assert ( worker ) ;
475
473
PThread . returnWorkerToPool ( worker ) ;
476
474
} ,
477
475
@@ -522,8 +520,8 @@ var LibraryPThread = {
522
520
assert ( ! ENVIRONMENT_IS_PTHREAD , 'Internal Error! cancelThread() can only ever be called from main application thread!' ) ;
523
521
assert ( pthread_ptr , 'Internal Error! Null pthread_ptr in cancelThread!' ) ;
524
522
#endif
525
- var pthread = PThread . pthreads [ pthread_ptr ] ;
526
- pthread . worker . postMessage ( { 'cmd' : 'cancel' } ) ;
523
+ var worker = PThread . pthreads [ pthread_ptr ] ;
524
+ worker . postMessage ( { 'cmd' : 'cancel' } ) ;
527
525
} ,
528
526
529
527
$spawnThread : function ( threadParams ) {
@@ -538,24 +536,20 @@ var LibraryPThread = {
538
536
return { { { cDefine ( 'EAGAIN' ) } } } ;
539
537
}
540
538
#if ASSERTIONS
541
- assert ( ! worker . pthread , 'Internal error!' ) ;
539
+ assert ( ! worker . pthread_ptr , 'Internal error!' ) ;
542
540
#endif
543
541
544
542
PThread . runningWorkers . push ( worker ) ;
545
543
546
- // Create a pthread info object to represent this thread.
547
- var pthread = PThread . pthreads [ threadParams . pthread_ptr ] = {
548
- worker : worker ,
549
- // Info area for this thread in Emscripten HEAP (shared)
550
- threadInfoStruct : threadParams . pthread_ptr
551
- } ;
544
+ // Add to pthreads map
545
+ PThread . pthreads [ threadParams . pthread_ptr ] = worker ;
552
546
553
- worker . pthread = pthread ;
547
+ worker . pthread_ptr = threadParams . pthread_ptr ;
554
548
var msg = {
555
549
'cmd' : 'run' ,
556
550
'start_routine' : threadParams . startRoutine ,
557
551
'arg' : threadParams . arg ,
558
- 'threadInfoStruct ' : threadParams . pthread_ptr ,
552
+ 'pthread_ptr ' : threadParams . pthread_ptr ,
559
553
} ;
560
554
#if OFFSCREENCANVAS_SUPPORT
561
555
// Note that we do not need to quote these names because they are only used
@@ -1017,8 +1011,7 @@ var LibraryPThread = {
1017
1011
} else if ( ENVIRONMENT_IS_PTHREAD ) {
1018
1012
postMessage ( { 'targetThread' : targetThreadId , 'cmd' : 'processProxyingQueue' , 'queue' : queue } ) ;
1019
1013
} else {
1020
- var pthread = PThread . pthreads [ targetThreadId ] ;
1021
- var worker = pthread && pthread . worker ;
1014
+ var worker = PThread . pthreads [ targetThreadId ] ;
1022
1015
if ( ! worker ) {
1023
1016
#if ASSERTIONS
1024
1017
err ( 'Cannot send message to thread with ID ' + targetThreadId + ', unknown thread ID!' ) ;
0 commit comments