Skip to content

Commit d581c72

Browse files
authored
[NFC] Add more comments to pthread mailbox infrastructure (#20511)
Also make a tiny simplification to a use of `callUserCallback`.
1 parent d5e710c commit d581c72

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

src/library_pthread.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1226,16 +1226,21 @@ var LibraryPThread = {
12261226
// pthread_self to return 0 if there is no live runtime.
12271227
var pthread_ptr = _pthread_self();
12281228
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.
12311233
__emscripten_thread_mailbox_await(pthread_ptr);
1232-
callUserCallback(() => __emscripten_check_mailbox());
1234+
callUserCallback(__emscripten_check_mailbox);
12331235
}
12341236
},
12351237

12361238
_emscripten_thread_mailbox_await__deps: ['$checkMailbox'],
12371239
_emscripten_thread_mailbox_await: (pthread_ptr) => {
12381240
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.
12391244
// TODO: How to make this work with wasm64?
12401245
var wait = Atomics.waitAsync(HEAP32, {{{ getHeapOffset('pthread_ptr', 'i32') }}}, pthread_ptr);
12411246
#if ASSERTIONS
@@ -1245,8 +1250,14 @@ var LibraryPThread = {
12451250
var waitingAsync = pthread_ptr + {{{ C_STRUCTS.pthread.waiting_async }}};
12461251
Atomics.store(HEAP32, {{{ getHeapOffset('waitingAsync', 'i32') }}}, 1);
12471252
}
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.
12481255
},
12491256

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.
12501261
_emscripten_notify_mailbox_postmessage__deps: ['$checkMailbox'],
12511262
_emscripten_notify_mailbox_postmessage: (targetThreadId, currThreadId, mainThreadId) => {
12521263
if (targetThreadId == currThreadId) {

system/lib/libc/musl/src/internal/pthread_impl.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,13 @@ struct pthread {
9797
// wait until it reaches 0, at which point the mailbox is considered
9898
// closed and no further messages will be enqueued.
9999
_Atomic int mailbox_refcount;
100-
// Whether the thread has executed a `waitAsync` on this pthread struct
101-
// and can be notified of new mailbox messages via `Atomics.notify`.
102-
// Otherwise the notification has to fall back to the postMessage path.
100+
// Whether the thread has executed an `Atomics.waitAsync` on this
101+
// pthread struct and can be notified of new mailbox messages via
102+
// `Atomics.notify`. Otherwise, such as when the environment does not
103+
// implement `Atomics.waitAsync` or when the thread has not had a chance
104+
// to initialize itself yet, the notification has to fall back to the
105+
// postMessage path. Once this becomes true, it remains true so we never
106+
// fall back to postMessage unnecessarily.
103107
_Atomic int waiting_async;
104108
#endif
105109
#ifdef EMSCRIPTEN_DYNAMIC_LINKING

0 commit comments

Comments
 (0)