@@ -31,6 +31,9 @@ var LibraryPThread = {
31
31
$PThread__deps : [ '_emscripten_thread_init' ,
32
32
'$killThread' ,
33
33
'$cancelThread' , '$cleanupThread' , '$zeroMemory' ,
34
+ #if MAIN_MODULE
35
+ '$markAsFinshed' ,
36
+ #endif
34
37
'$spawnThread' ,
35
38
'_emscripten_thread_free_data' ,
36
39
'exit' ,
@@ -97,6 +100,12 @@ var LibraryPThread = {
97
100
while ( pthreadPoolSize -- ) {
98
101
PThread . allocateUnusedWorker ( ) ;
99
102
}
103
+ #endif
104
+ #if MAIN_MODULE
105
+ PThread . outstandingPromises = { } ;
106
+ // Finished threads are threads that have finished running but we not yet
107
+ // joined.
108
+ PThread . finishedThreads = new Set ( ) ;
100
109
#endif
101
110
} ,
102
111
@@ -269,6 +278,10 @@ var LibraryPThread = {
269
278
spawnThread ( d ) ;
270
279
} else if ( cmd === 'cleanupThread' ) {
271
280
cleanupThread ( d [ 'thread' ] ) ;
281
+ #if MAIN_MODULE
282
+ } else if ( cmd === 'markAsFinshed' ) {
283
+ markAsFinshed ( d [ 'thread' ] ) ;
284
+ #endif
272
285
} else if ( cmd === 'killThread' ) {
273
286
killThread ( d [ 'thread' ] ) ;
274
287
} else if ( cmd === 'cancelThread' ) {
@@ -540,11 +553,20 @@ var LibraryPThread = {
540
553
} ,
541
554
542
555
$cleanupThread : function ( pthread_ptr ) {
556
+ #if PTHREADS_DEBUG
557
+ dbg ( 'cleanupThread: ' + ptrToString ( pthread_ptr ) )
558
+ #endif
543
559
#if ASSERTIONS
544
560
assert ( ! ENVIRONMENT_IS_PTHREAD , 'Internal Error! cleanupThread() can only ever be called from main application thread!' ) ;
545
561
assert ( pthread_ptr , 'Internal Error! Null pthread_ptr in cleanupThread!' ) ;
546
562
#endif
547
563
var worker = PThread . pthreads [ pthread_ptr ] ;
564
+ #if MAIN_MODULE
565
+ PThread . finishedThreads . delete ( pthread_ptr ) ;
566
+ if ( pthread_ptr in PThread . outstandingPromises ) {
567
+ PThread . outstandingPromises [ pthread_ptr ] . resolve ( ) ;
568
+ }
569
+ #endif
548
570
assert ( worker ) ;
549
571
PThread . returnWorkerToPool ( worker ) ;
550
572
} ,
@@ -1048,7 +1070,7 @@ var LibraryPThread = {
1048
1070
// Before we call the thread entry point, make sure any shared libraries
1049
1071
// have been loaded on this there. Otherwise our table migth be not be
1050
1072
// in sync and might not contain the function pointer `ptr` at all.
1051
- __emscripten_thread_sync_code ( ) ;
1073
+ __emscripten_dlsync_self ( ) ;
1052
1074
#endif
1053
1075
// pthread entry points are always of signature 'void *ThreadMain(void *arg)'
1054
1076
// Native codebases sometimes spawn threads with other thread entry point
@@ -1077,6 +1099,99 @@ var LibraryPThread = {
1077
1099
#endif
1078
1100
} ,
1079
1101
1102
+ #if MAIN_MODULE
1103
+ _emscripten_thread_exit_joinable : function ( thread ) {
1104
+ // Called when a thread exits and is joinable. We mark these threads
1105
+ // as finished, which means that are in state where are no longer actually
1106
+ // runnning, but remain around waiting to be joined. In this state they
1107
+ // cannot run any more proxied work.
1108
+ if ( ! ENVIRONMENT_IS_PTHREAD ) markAsFinshed ( thread ) ;
1109
+ else postMessage ( { 'cmd' : 'markAsFinshed' , 'thread' : thread } ) ;
1110
+ } ,
1111
+
1112
+ $markAsFinshed : function ( pthread_ptr ) {
1113
+ #if PTHREADS_DEBUG
1114
+ dbg ( 'markAsFinshed: ' + ptrToString ( pthread_ptr ) ) ;
1115
+ #endif
1116
+ PThread . finishedThreads . add ( pthread_ptr ) ;
1117
+ if ( pthread_ptr in PThread . outstandingPromises ) {
1118
+ PThread . outstandingPromises [ pthread_ptr ] . resolve ( ) ;
1119
+ }
1120
+ } ,
1121
+
1122
+ // Asynchronous version dlsync_threads. Always run on the main thread.
1123
+ // This work happens asynchronously. The `callback` is called once this work
1124
+ // is completed, passing the ctx.
1125
+ // TODO(sbc): Should we make a new form of __proxy attribute for JS library
1126
+ // function that run asynchronously like but blocks the caller until they are
1127
+ // done. Perhaps "sync_with_ctx"?
1128
+ _emscripten_dlsync_threads_async__sig : 'vppp ',
1129
+ _emscripten_dlsync_threads_async__deps : [ '_emscripten_proxy_dlsync_async' , 'emscripten_promise_create' , '$getPromise' ] ,
1130
+ _emscripten_dlsync_threads_async : function ( caller , callback , ctx ) {
1131
+ #if PTHREADS_DEBUG
1132
+ dbg ( "_emscripten_dlsync_threads_async caller=" + ptrToString ( caller ) ) ;
1133
+ #endif
1134
+ #if ASSERTIONS
1135
+ assert ( ! ENVIRONMENT_IS_PTHREAD , 'Internal Error! _emscripten_dlsync_threads_async() can only ever be called from main thread' ) ;
1136
+ #endif
1137
+
1138
+ const promises = [ ] ;
1139
+ assert ( Object . keys ( PThread . outstandingPromises ) . length === 0 ) ;
1140
+
1141
+ // This first promise resolves once the main thread has loaded all modules.
1142
+ var info = makePromise ( ) ;
1143
+ promises . push ( info . promise ) ;
1144
+ __emscripten_dlsync_self_async ( info . id ) ;
1145
+
1146
+
1147
+ // We then create a sequence of promises, one per thread, that resolve once
1148
+ // each thread has performed its sync using _emscripten_proxy_dlsync.
1149
+ // Any new threads that are created after this call will automaticaly be
1150
+ // in sync because we call `__emscripten_dlsync_self` in
1151
+ // invokeEntryPoint before the threads entry point is called.
1152
+ for ( const ptr of Object . keys ( PThread . pthreads ) ) {
1153
+ const pthread_ptr = Number ( ptr ) ;
1154
+ if ( pthread_ptr !== caller && ! PThread . finishedThreads . has ( pthread_ptr ) ) {
1155
+ info = makePromise ( ) ;
1156
+ __emscripten_proxy_dlsync_async ( pthread_ptr , info . id ) ;
1157
+ PThread . outstandingPromises [ pthread_ptr ] = info ;
1158
+ promises . push ( info . promise ) ;
1159
+ }
1160
+ }
1161
+
1162
+ #if PTHREADS_DEBUG
1163
+ dbg ( '_emscripten_dlsync_threads_async: waiting on ' + promises . length + ' promises' ) ;
1164
+ #endif
1165
+ // Once all promises are resolved then we know all threads are in sync and
1166
+ // we can call the callback.
1167
+ Promise . all ( promises ) . then ( ( ) => {
1168
+ PThread . outstandingPromises = { } ;
1169
+ #if PTHREADS_DEBUG
1170
+ dbg ( '_emscripten_dlsync_threads_async done: calling callback' ) ;
1171
+ #endif
1172
+ { { { makeDynCall ( 'vp' , 'callback' ) } } } ( ctx ) ;
1173
+ } ) ;
1174
+ } ,
1175
+
1176
+ // Synchronous version dlsync_threads. This is only needed for the case then
1177
+ // the main thread call dlopen and in that case we have not choice but to
1178
+ // synchronously block the main thread until all other threads are in sync.
1179
+ // When `dlopen` is called from a worker, the worker itself is blocked but
1180
+ // the operation its waiting on (on the main thread) can be async.
1181
+ _emscripten_dlsync_threads__deps : [ '_emscripten_proxy_dlsync' ] ,
1182
+ _emscripten_dlsync_threads : function ( ) {
1183
+ #if ASSERTIONS
1184
+ assert ( ! ENVIRONMENT_IS_PTHREAD , 'Internal Error! _emscripten_dlsync_threads() can only ever be called from main thread' ) ;
1185
+ #endif
1186
+ for ( const ptr of Object . keys ( PThread . pthreads ) ) {
1187
+ const pthread_ptr = Number ( ptr ) ;
1188
+ if ( ! PThread . finishedThreads . has ( pthread_ptr ) ) {
1189
+ __emscripten_proxy_dlsync ( pthread_ptr ) ;
1190
+ }
1191
+ }
1192
+ } ,
1193
+ #endif // MAIN_MODULE
1194
+
1080
1195
$executeNotifiedProxyingQueue : function ( queue ) {
1081
1196
// Set the notification state to processing.
1082
1197
Atomics . store ( HEAP32 , queue >> 2 , { { { cDefine ( 'NOTIFICATION_RECEIVED' ) } } } ) ;
0 commit comments