4
4
// found in the LICENSE file.
5
5
6
6
var LibraryPThread = {
7
- $PThread__postset : 'if (!ENVIRONMENT_IS_PTHREAD) PThread.initMainThreadBlock();' ,
8
- $PThread__deps : [ '$PROCINFO' , '_register_pthread_ptr' , 'emscripten_main_thread_process_queued_calls' , '$ERRNO_CODES' , 'emscripten_futex_wake' ] ,
7
+ $PThread__postset : 'if (!ENVIRONMENT_IS_PTHREAD) PThread.initMainThreadBlock(); else PThread.initWorker();' ,
8
+ $PThread__deps : [ '$PROCINFO' , '_register_pthread_ptr' ,
9
+ 'emscripten_main_thread_process_queued_calls' ,
10
+ '$ERRNO_CODES' , 'emscripten_futex_wake' , '_kill_thread' ,
11
+ '_cancel_thread' , '_cleanup_thread' ] ,
9
12
$PThread : {
10
13
MAIN_THREAD_ID : 1 , // A special constant that identifies the main JS thread ID.
11
14
mainThreadInfo : {
@@ -65,6 +68,16 @@ var LibraryPThread = {
65
68
PThread . createProfilerBlock ( PThread . mainThreadBlock ) ;
66
69
PThread . setThreadName ( PThread . mainThreadBlock , "Browser main thread" ) ;
67
70
PThread . setThreadStatus ( PThread . mainThreadBlock , { { { cDefine ( 'EM_THREAD_STATUS_RUNNING' ) } } } ) ;
71
+ #endif
72
+ } ,
73
+ initWorker: function ( ) {
74
+ #if USE_CLOSURE_COMPILER
75
+ // worker.js is not compiled together with us, and must access certain
76
+ // things.
77
+ PThread [ 'receiveObjectTransfer' ] = PThread . receiveObjectTransfer ;
78
+ PThread [ 'setThreadStatus' ] = PThread . setThreadStatus ;
79
+ PThread [ 'threadCancel' ] = PThread . threadCancel ;
80
+ PThread [ 'threadExit' ] = PThread . threadExit ;
68
81
#endif
69
82
} ,
70
83
// Maps pthread_t to pthread info objects
@@ -178,7 +191,7 @@ var LibraryPThread = {
178
191
if ( ENVIRONMENT_IS_PTHREAD ) {
179
192
// Note: in theory we would like to return any offscreen canvases back to the main thread,
180
193
// but if we ever fetched a rendering context for them that would not be valid, so we don't try.
181
- postMessage ( { cmd : 'exit' } ) ;
194
+ postMessage ( { ' cmd' : 'exit' } ) ;
182
195
}
183
196
}
184
197
} ,
@@ -190,7 +203,7 @@ var LibraryPThread = {
190
203
_emscripten_futex_wake ( threadInfoStruct + { { { C_STRUCTS . pthread . threadStatus } } } , { { { cDefine ( 'INT_MAX' ) } } } ) ; // wake all threads
191
204
threadInfoStruct = selfThreadId = 0 ; // Not hosting a pthread anymore in this worker, reset the info structures to null.
192
205
__register_pthread_ptr ( 0 , 0 , 0 ) ; // Unregister the thread block also inside the asm.js scope.
193
- postMessage ( { cmd : 'cancelDone' } ) ;
206
+ postMessage ( { ' cmd' : 'cancelDone' } ) ;
194
207
} ,
195
208
196
209
terminateAllThreads : function ( ) {
@@ -305,32 +318,32 @@ var LibraryPThread = {
305
318
306
319
// Ask the new worker to load up the Emscripten-compiled page. This is a heavy operation.
307
320
worker . postMessage ( {
308
- cmd : 'load' ,
321
+ ' cmd' : 'load' ,
309
322
// If the application main .js file was loaded from a Blob, then it is not possible
310
323
// to access the URL of the current script that could be passed to a Web Worker so that
311
324
// it could load up the same file. In that case, developer must either deliver the Blob
312
325
// object in Module['mainScriptUrlOrBlob'], or a URL to it, so that pthread Workers can
313
326
// independently load up the same main application file.
314
- urlOrBlob : Module [ 'mainScriptUrlOrBlob' ] || _scriptDir ,
327
+ ' urlOrBlob' : Module [ 'mainScriptUrlOrBlob' ] || _scriptDir ,
315
328
#if WASM
316
- wasmMemory : wasmMemory ,
317
- wasmModule : wasmModule ,
329
+ ' wasmMemory' : wasmMemory ,
330
+ ' wasmModule' : wasmModule ,
318
331
#if LOAD_SOURCE_MAP
319
- wasmSourceMap : wasmSourceMap ,
332
+ ' wasmSourceMap' : wasmSourceMap ,
320
333
#endif
321
334
#if USE_OFFSET_CONVERTER
322
- wasmOffsetConverter : wasmOffsetConverter ,
335
+ ' wasmOffsetConverter' : wasmOffsetConverter ,
323
336
#endif
324
337
#else
325
- buffer : HEAPU8 . buffer ,
326
- asmJsUrlOrBlob : Module [ "asmJsUrlOrBlob" ] ,
338
+ ' buffer' : HEAPU8 . buffer ,
339
+ ' asmJsUrlOrBlob' : Module [ "asmJsUrlOrBlob" ] ,
327
340
#endif
328
341
#if ! WASM_BACKEND
329
- tempDoublePtr : tempDoublePtr ,
342
+ ' tempDoublePtr' : tempDoublePtr ,
330
343
#endif
331
- DYNAMIC_BASE : DYNAMIC_BASE ,
332
- DYNAMICTOP_PTR : DYNAMICTOP_PTR ,
333
- PthreadWorkerInit : PthreadWorkerInit
344
+ ' DYNAMIC_BASE' : DYNAMIC_BASE ,
345
+ ' DYNAMICTOP_PTR' : DYNAMICTOP_PTR ,
346
+ ' PthreadWorkerInit' : PthreadWorkerInit
334
347
} ) ;
335
348
PThread . unusedWorkers . push ( worker ) ;
336
349
}
@@ -346,34 +359,35 @@ var LibraryPThread = {
346
359
var worker = workers [ i ] ;
347
360
( function ( worker ) {
348
361
worker . onmessage = function ( e ) {
349
- var d = e . data ;
362
+ var d = e [ 'data' ] ;
363
+ var cmd = d [ 'cmd' ] ;
350
364
// Sometimes we need to backproxy events to the calling thread (e.g. HTML5 DOM events handlers such as emscripten_set_mousemove_callback()), so keep track in a globally accessible variable about the thread that initiated the proxying.
351
365
if ( worker . pthread ) PThread . currentProxiedOperationCallerThread = worker . pthread . threadInfoStruct ;
352
366
353
367
// If this message is intended to a recipient that is not the main thread, forward it to the target thread.
354
- if ( d . targetThread && d . targetThread != _pthread_self ( ) ) {
368
+ if ( d [ ' targetThread' ] && d [ ' targetThread' ] != _pthread_self ( ) ) {
355
369
var thread = PThread . pthreads [ d . targetThread ] ;
356
370
if ( thread ) {
357
- thread . worker . postMessage ( e . data , d . transferList ) ;
371
+ thread . worker . postMessage ( e . data , d [ ' transferList' ] ) ;
358
372
} else {
359
- console . error ( 'Internal error! Worker sent a message "' + d . cmd + '" to target pthread ' + d . targetThread + ', but that thread no longer exists!' ) ;
373
+ console . error ( 'Internal error! Worker sent a message "' + cmd + '" to target pthread ' + d [ ' targetThread' ] + ', but that thread no longer exists!' ) ;
360
374
}
361
375
PThread . currentProxiedOperationCallerThread = undefined ;
362
376
return ;
363
377
}
364
378
365
- if ( d . cmd === 'processQueuedMainThreadWork' ) {
379
+ if ( cmd === 'processQueuedMainThreadWork' ) {
366
380
// TODO: Must post message to main Emscripten thread in PROXY_TO_WORKER mode.
367
381
_emscripten_main_thread_process_queued_calls ( ) ;
368
- } else if ( d . cmd === 'spawnThread' ) {
382
+ } else if ( cmd === 'spawnThread' ) {
369
383
__spawn_thread ( e . data ) ;
370
- } else if ( d . cmd === 'cleanupThread' ) {
371
- __cleanup_thread ( d . thread ) ;
372
- } else if ( d . cmd === 'killThread' ) {
373
- __kill_thread ( d . thread ) ;
374
- } else if ( d . cmd === 'cancelThread' ) {
375
- __cancel_thread ( d . thread ) ;
376
- } else if ( d . cmd === 'loaded' ) {
384
+ } else if ( cmd === 'cleanupThread' ) {
385
+ __cleanup_thread ( d [ ' thread' ] ) ;
386
+ } else if ( cmd === 'killThread' ) {
387
+ __kill_thread ( d [ ' thread' ] ) ;
388
+ } else if ( cmd === 'cancelThread' ) {
389
+ __cancel_thread ( d [ ' thread' ] ) ;
390
+ } else if ( cmd === 'loaded' ) {
377
391
worker . loaded = true ;
378
392
// If this Worker is already pending to start running a thread, launch the thread now
379
393
if ( worker . runPthread ) {
@@ -384,34 +398,34 @@ var LibraryPThread = {
384
398
if ( numWorkersLoaded === numWorkers && onFinishedLoading ) {
385
399
onFinishedLoading ( ) ;
386
400
}
387
- } else if ( d . cmd === 'print' ) {
388
- out ( 'Thread ' + d . threadId + ': ' + d . text ) ;
389
- } else if ( d . cmd === 'printErr' ) {
390
- err ( 'Thread ' + d . threadId + ': ' + d . text ) ;
391
- } else if ( d . cmd === 'alert' ) {
392
- alert ( 'Thread ' + d . threadId + ': ' + d . text ) ;
393
- } else if ( d . cmd === 'exit' ) {
401
+ } else if ( cmd === 'print' ) {
402
+ out ( 'Thread ' + d [ ' threadId' ] + ': ' + d [ ' text' ] ) ;
403
+ } else if ( cmd === 'printErr' ) {
404
+ err ( 'Thread ' + d [ ' threadId' ] + ': ' + d [ ' text' ] ) ;
405
+ } else if ( cmd === 'alert' ) {
406
+ alert ( 'Thread ' + d [ ' threadId' ] + ': ' + d [ ' text' ] ) ;
407
+ } else if ( cmd === 'exit' ) {
394
408
var detached = worker . pthread && Atomics . load ( HEAPU32 , ( worker . pthread . thread + { { { C_STRUCTS . pthread . detached } } } ) >> 2 ) ;
395
409
if ( detached ) {
396
410
PThread . returnWorkerToPool ( worker ) ;
397
411
}
398
- } else if ( d . cmd === 'exitProcess' ) {
412
+ } else if ( cmd === 'exitProcess' ) {
399
413
// A pthread has requested to exit the whole application process (runtime).
400
414
noExitRuntime = false ;
401
415
try {
402
- exit ( d . returnCode ) ;
416
+ exit ( d [ ' returnCode' ] ) ;
403
417
} catch ( e ) {
404
418
if ( e instanceof ExitStatus ) return ;
405
419
throw e ;
406
420
}
407
- } else if ( d . cmd === 'cancelDone' ) {
421
+ } else if ( cmd === 'cancelDone' ) {
408
422
PThread . returnWorkerToPool ( worker ) ;
409
- } else if ( d . cmd === 'objectTransfer' ) {
423
+ } else if ( cmd === 'objectTransfer' ) {
410
424
PThread . receiveObjectTransfer ( e . data ) ;
411
425
} else if ( e . data . target === 'setimmediate' ) {
412
426
worker . postMessage ( e . data ) ; // Worker wants to postMessage() to itself to implement setImmediate() emulation.
413
427
} else {
414
- err ( "worker sent an unknown command " + d . cmd ) ;
428
+ err ( "worker sent an unknown command " + cmd ) ;
415
429
}
416
430
PThread . currentProxiedOperationCallerThread = undefined ;
417
431
} ;
@@ -483,7 +497,7 @@ var LibraryPThread = {
483
497
if ( ENVIRONMENT_IS_PTHREAD ) throw 'Internal Error! _cancel_thread() can only ever be called from main application thread!' ;
484
498
if ( ! pthread_ptr ) throw 'Internal Error! Null pthread_ptr in _cancel_thread!' ;
485
499
var pthread = PThread . pthreads [ pthread_ptr ] ;
486
- pthread . worker . postMessage ( { cmd : 'cancel' } ) ;
500
+ pthread . worker . postMessage ( { ' cmd' : 'cancel' } ) ;
487
501
} ,
488
502
489
503
_spawn_thread : function ( threadParams ) {
@@ -537,17 +551,17 @@ var LibraryPThread = {
537
551
538
552
worker . pthread = pthread ;
539
553
var msg = {
540
- cmd : 'run ',
541
- start_routine : threadParams . startRoutine ,
542
- arg : threadParams . arg ,
543
- threadInfoStruct : threadParams . pthread_ptr ,
544
- selfThreadId : threadParams . pthread_ptr , // TODO: Remove this since thread ID is now the same as the thread address.
545
- parentThreadId : threadParams . parent_pthread_ptr ,
546
- stackBase : threadParams . stackBase ,
547
- stackSize : threadParams . stackSize ,
554
+ ' cmd ' : 'run ',
555
+ ' start_routine' : threadParams . startRoutine ,
556
+ ' arg ' : threadParams . arg ,
557
+ ' threadInfoStruct ' : threadParams . pthread_ptr ,
558
+ ' selfThreadId ' : threadParams . pthread_ptr , // TODO: Remove this since thread ID is now the same as the thread address.
559
+ ' parentThreadId ' : threadParams . parent_pthread_ptr ,
560
+ ' stackBase ' : threadParams . stackBase ,
561
+ ' stackSize ' : threadParams . stackSize ,
548
562
#if OFFSCREENCANVAS_SUPPORT
549
- moduleCanvasId : threadParams . moduleCanvasId ,
550
- offscreenCanvases : threadParams . offscreenCanvases ,
563
+ ' moduleCanvasId ' : threadParams . moduleCanvasId ,
564
+ ' offscreenCanvases ' : threadParams . offscreenCanvases ,
551
565
#endif
552
566
} ;
553
567
worker . runPthread = function ( ) {
@@ -562,7 +576,7 @@ var LibraryPThread = {
562
576
} ,
563
577
564
578
_num_logical_cores__deps : [ 'emscripten_force_num_logical_cores '] ,
565
- _num_logical_cores : '; if (ENVIRONMENT_IS_PTHREAD) __num_logical_cores = PthreadWorkerInit. __num_logical_cores; else { PthreadWorkerInit. __num_logical_cores = __num_logical_cores = {{{ makeStaticAlloc(4) }}}; HEAPU32[__num_logical_cores>>2] = navigator["hardwareConcurrency"] || ' + { { { PTHREAD_HINT_NUM_CORES } } } + '; }' ,
579
+ _num_logical_cores : '; if (ENVIRONMENT_IS_PTHREAD) __num_logical_cores = PthreadWorkerInit[" __num_logical_cores"] ; else { PthreadWorkerInit[" __num_logical_cores"] = __num_logical_cores = {{{ makeStaticAlloc(4) }}}; HEAPU32[__num_logical_cores>>2] = navigator["hardwareConcurrency"] || ' + { { { PTHREAD_HINT_NUM_CORES } } } + '; }' ,
566
580
567
581
emscripten_has_threading_support : function ( ) {
568
582
return typeof SharedArrayBuffer !== 'undefined' ;
@@ -822,7 +836,7 @@ var LibraryPThread = {
822
836
Atomics . store ( HEAPU32 , ( thread + { { { C_STRUCTS . pthread . detached } } } ) >> 2 , 1 ) ; // Mark the thread as detached.
823
837
824
838
if ( ! ENVIRONMENT_IS_PTHREAD ) __cleanup_thread ( thread ) ;
825
- else postMessage ( { cmd : 'cleanupThread' , thread : thread } ) ;
839
+ else postMessage ( { ' cmd' : 'cleanupThread' , ' thread' : thread } ) ;
826
840
return 0 ;
827
841
}
828
842
// TODO HACK! Replace the _js variant with just _pthread_testcancel:
@@ -854,7 +868,7 @@ var LibraryPThread = {
854
868
}
855
869
if ( signal != 0 ) {
856
870
if ( ! ENVIRONMENT_IS_PTHREAD ) __kill_thread ( thread ) ;
857
- else postMessage ( { cmd : 'killThread ', thread : thread } ) ;
871
+ else postMessage ( { ' cmd ' : 'killThread ', ' thread ' : thread } ) ;
858
872
}
859
873
return 0 ;
860
874
} ,
@@ -876,7 +890,7 @@ var LibraryPThread = {
876
890
}
877
891
Atomics . compareExchange ( HEAPU32 , ( thread + { { { C_STRUCTS . pthread . threadStatus } } } ) >> 2 , 0 , 2 ) ; // Signal the thread that it needs to cancel itself.
878
892
if ( ! ENVIRONMENT_IS_PTHREAD ) __cancel_thread ( thread ) ;
879
- else postMessage ( { cmd : 'cancelThread ', thread : thread } ) ;
893
+ else postMessage ( { ' cmd ' : 'cancelThread ', ' thread ' : thread } ) ;
880
894
return 0 ;
881
895
} ,
882
896
@@ -1068,7 +1082,7 @@ var LibraryPThread = {
1068
1082
} ,
1069
1083
1070
1084
// Stores the memory address that the main thread is waiting on, if any.
1071
- _main_thread_futex_wait_address : '; if (ENVIRONMENT_IS_PTHREAD) __main_thread_futex_wait_address = PthreadWorkerInit. __main_thread_futex_wait_address; else PthreadWorkerInit. __main_thread_futex_wait_address = __main_thread_futex_wait_address = {{{ makeStaticAlloc(4) }}}' ,
1085
+ _main_thread_futex_wait_address : '; if (ENVIRONMENT_IS_PTHREAD) __main_thread_futex_wait_address = PthreadWorkerInit[" __main_thread_futex_wait_address"] ; else PthreadWorkerInit[" __main_thread_futex_wait_address"] = __main_thread_futex_wait_address = {{{ makeStaticAlloc(4) }}}' ,
1072
1086
1073
1087
// Returns 0 on success, or one of the values -ETIMEDOUT, -EWOULDBLOCK or -EINVAL on error.
1074
1088
emscripten_futex_wait__deps : [ '_main_thread_futex_wait_address' , 'emscripten_main_thread_process_queued_calls' ] ,
@@ -1159,7 +1173,7 @@ var LibraryPThread = {
1159
1173
1160
1174
__call_main : function ( argc , argv ) {
1161
1175
var returnCode = _main ( argc , argv ) ;
1162
- if ( ! noExitRuntime ) postMessage ( { cmd : 'exitProcess' , returnCode : returnCode } ) ;
1176
+ if ( ! noExitRuntime ) postMessage ( { ' cmd' : 'exitProcess' , ' returnCode' : returnCode } ) ;
1163
1177
return returnCode ;
1164
1178
} ,
1165
1179
0 commit comments