@@ -22,9 +22,6 @@ var LibraryPThread = {
22
22
unusedWorkers : [ ] ,
23
23
// Contains all Workers that are currently hosting an active pthread.
24
24
runningWorkers : [ ] ,
25
- // Points to a pthread_t structure in the Emscripten main heap, allocated on
26
- // demand if/when first needed.
27
- // mainThreadBlock: undefined,
28
25
initMainThreadBlock : function ( ) {
29
26
#if ASSERTIONS
30
27
assert ( ! ENVIRONMENT_IS_PTHREAD ) ;
@@ -47,41 +44,41 @@ var LibraryPThread = {
47
44
withBuiltinMalloc ( function ( ) {
48
45
#endif
49
46
50
- PThread . mainThreadBlock = _malloc ( { { { C_STRUCTS . pthread . __size__ } } } ) ;
47
+ var tb = _malloc ( { { { C_STRUCTS . pthread . __size__ } } } ) ;
51
48
52
- for ( var i = 0 ; i < { { { C_STRUCTS . pthread . __size__ } } } / 4 ; ++ i ) HEAPU32 [ PThread . mainThreadBlock / 4 + i ] = 0 ;
49
+ for ( var i = 0 ; i < { { { C_STRUCTS . pthread . __size__ } } } / 4 ; ++ i ) HEAPU32 [ tb / 4 + i ] = 0 ;
53
50
54
51
// The pthread struct has a field that points to itself - this is used as
55
52
// a magic ID to detect whether the pthread_t structure is 'alive'.
56
- { { { makeSetValue ( 'PThread.mainThreadBlock ' , C_STRUCTS . pthread . self , 'PThread.mainThreadBlock ' , 'i32' ) } } } ;
53
+ { { { makeSetValue ( 'tb ' , C_STRUCTS . pthread . self , 'tb ' , 'i32' ) } } } ;
57
54
58
55
// pthread struct robust_list head should point to itself.
59
- var headPtr = PThread . mainThreadBlock + { { { C_STRUCTS . pthread . robust_list } } } ;
56
+ var headPtr = tb + { { { C_STRUCTS . pthread . robust_list } } } ;
60
57
{ { { makeSetValue ( 'headPtr' , 0 , 'headPtr' , 'i32' ) } } } ;
61
58
62
59
// Allocate memory for thread-local storage.
63
60
var tlsMemory = _malloc ( { { { cDefine ( 'PTHREAD_KEYS_MAX' ) * 4 } } } ) ;
64
61
for ( var i = 0 ; i < { { { cDefine ( 'PTHREAD_KEYS_MAX' ) } } } ; ++ i ) HEAPU32 [ tlsMemory / 4 + i ] = 0 ;
65
- Atomics . store ( HEAPU32 , ( PThread . mainThreadBlock + { { { C_STRUCTS . pthread . tsd } } } ) >> 2 , tlsMemory ) ; // Init thread-local-storage memory array.
66
- Atomics . store ( HEAPU32 , ( PThread . mainThreadBlock + { { { C_STRUCTS . pthread . tid } } } ) >> 2 , PThread . mainThreadBlock ) ; // Main thread ID.
62
+ Atomics . store ( HEAPU32 , ( tb + { { { C_STRUCTS . pthread . tsd } } } ) >> 2 , tlsMemory ) ; // Init thread-local-storage memory array.
63
+ Atomics . store ( HEAPU32 , ( tb + { { { C_STRUCTS . pthread . tid } } } ) >> 2 , tb ) ; // Main thread ID.
67
64
68
65
PThread . initShared ( ) ;
69
66
70
67
#if PTHREADS_PROFILING
71
- PThread . createProfilerBlock ( PThread . mainThreadBlock ) ;
72
- PThread . setThreadName ( PThread . mainThreadBlock , "Browser main thread" ) ;
73
- PThread . setThreadStatus ( PThread . mainThreadBlock , { { { cDefine ( 'EM_THREAD_STATUS_RUNNING' ) } } } ) ;
74
- #endif
75
-
76
- #if USE_ASAN || USE_LSAN
77
- } ) ;
68
+ PThread . createProfilerBlock ( tb ) ;
69
+ PThread . setThreadName ( tb , "Browser main thread" ) ;
70
+ PThread . setThreadStatus ( tb , { { { cDefine ( 'EM_THREAD_STATUS_RUNNING' ) } } } ) ;
78
71
#endif
79
72
80
73
// Pass the thread address to the native code where they stored in wasm
81
74
// globals which act as a form of TLS. Global constructors trying
82
75
// to access this value will read the wrong value, but that is UB anyway.
83
- __emscripten_thread_init ( PThread . mainThreadBlock , /*isMainBrowserThread=*/ ! ENVIRONMENT_IS_WORKER , /*isMainRuntimeThread=*/ 1 ) ;
84
- _emscripten_register_main_browser_thread_id ( PThread . mainThreadBlock ) ;
76
+ __emscripten_thread_init ( tb , /*isMainBrowserThread=*/ ! ENVIRONMENT_IS_WORKER , /*isMainRuntimeThread=*/ 1 ) ;
77
+ _emscripten_register_main_browser_thread_id ( tb ) ;
78
+
79
+ #if USE_ASAN || USE_LSAN
80
+ } ) ;
81
+ #endif
85
82
} ,
86
83
initWorker: function ( ) {
87
84
PThread . initShared ( ) ;
@@ -848,7 +845,7 @@ var LibraryPThread = {
848
845
#endif
849
846
} ,
850
847
851
- _emscripten_do_pthread_join__deps: [ '$cleanupThread' , '_pthread_testcancel_js' , 'emscripten_main_thread_process_queued_calls' , 'emscripten_futex_wait' , 'pthread_self' ,
848
+ _emscripten_do_pthread_join__deps: [ '$cleanupThread' , '_pthread_testcancel_js' , 'emscripten_main_thread_process_queued_calls' , 'emscripten_futex_wait' , 'pthread_self' , 'emscripten_main_browser_thread_id' ,
852
849
#if ASSERTIONS || IN_TEST_HARNESS || ! MINIMAL_RUNTIME || ! ALLOW_BLOCKING_ON_MAIN_THREAD
853
850
'emscripten_check_blocking_allowed'
854
851
#endif
@@ -862,7 +859,7 @@ var LibraryPThread = {
862
859
err ( 'PThread ' + thread + ' is attempting to join to itself!' ) ;
863
860
return ERRNO_CODES . EDEADLK ;
864
861
}
865
- else if ( ! ENVIRONMENT_IS_PTHREAD && PThread . mainThreadBlock == thread ) {
862
+ else if ( ! ENVIRONMENT_IS_PTHREAD && _emscripten_main_browser_thread_id ( ) == thread ) {
866
863
err ( 'Main thread ' + thread + ' is attempting to join to itself!' ) ;
867
864
return ERRNO_CODES . EDEADLK ;
868
865
}
0 commit comments