@@ -1226,16 +1226,21 @@ var LibraryPThread = {
1226
1226
// pthread_self to return 0 if there is no live runtime.
1227
1227
var pthread_ptr = _pthread_self ( ) ;
1228
1228
if ( pthread_ptr ) {
1229
- // If we are using Atomics.waitAsync as our notification mechanism, wait for
1230
- // a notification before processing the mailbox to avoid missing any work.
1229
+ // If we are using Atomics.waitAsync as our notification mechanism, wait
1230
+ // for a notification before processing the mailbox to avoid missing any
1231
+ // work that could otherwise arrive after we've finished processing the
1232
+ // mailbox and before we're ready for the next notification.
1231
1233
__emscripten_thread_mailbox_await ( pthread_ptr ) ;
1232
- callUserCallback ( ( ) => __emscripten_check_mailbox ( ) ) ;
1234
+ callUserCallback ( __emscripten_check_mailbox ) ;
1233
1235
}
1234
1236
} ,
1235
1237
1236
1238
_emscripten_thread_mailbox_await__deps : [ '$checkMailbox '] ,
1237
1239
_emscripten_thread_mailbox_await : ( pthread_ptr ) => {
1238
1240
if ( typeof Atomics . waitAsync === 'function' ) {
1241
+ // Wait on the pthread's initial self-pointer field because it is easy and
1242
+ // safe to access from sending threads that need to notify the waiting
1243
+ // thread.
1239
1244
// TODO: How to make this work with wasm64?
1240
1245
var wait = Atomics . waitAsync ( HEAP32 , { { { getHeapOffset ( 'pthread_ptr' , 'i32' ) } } } , pthread_ptr ) ;
1241
1246
#if ASSERTIONS
@@ -1245,8 +1250,14 @@ var LibraryPThread = {
1245
1250
var waitingAsync = pthread_ptr + { { { C_STRUCTS . pthread . waiting_async } } } ;
1246
1251
Atomics . store ( HEAP32 , { { { getHeapOffset ( 'waitingAsync' , 'i32' ) } } } , 1 ) ;
1247
1252
}
1253
+ // If `Atomics.waitAsync` is not implemented, then we will always fall back
1254
+ // to postMessage and there is no need to do anything here.
1248
1255
} ,
1249
1256
1257
+ // PostMessage is used to notify threads instead of Atomics.notify whenever
1258
+ // the environment does not implement Atomics.waitAsync or when messaging a
1259
+ // new thread that has not had a chance to initialize itself and execute
1260
+ // Atomics.waitAsync to prepare for the notification.
1250
1261
_emscripten_notify_mailbox_postmessage__deps : [ '$checkMailbox' ] ,
1251
1262
_emscripten_notify_mailbox_postmessage : ( targetThreadId , currThreadId , mainThreadId ) = > {
1252
1263
if ( targetThreadId == currThreadId ) {
0 commit comments