From 6d4f66b48cc0378ee9c85a1ae698cf77f4c49add Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Thu, 21 May 2026 14:32:00 -0700 Subject: [PATCH] [pthreads] Simplify message relaying code Historically, `targetThread` relaying was used to pass `objectTransfer` messages (containing `OffscreenCanvas` objects) back to parent threads on worker exit. This was introduced in commit 1b278917cd (2018/2019) and required supporting `transferList` in the relaying code. However, automatic canvas return on exit was later disabled in 6dcf7298e8 (2019) and the `objectTransfer` command was fully removed in 0f536c60bb (2021). These days, the relaying mechanism is only used by `CMD_CHECK_MAILBOX` (via `_emscripten_notify_mailbox_postmessage`) to send simple mailbox notifications, which do not require a transfer list. --- src/lib/libpthread.js | 18 +++++++++++------- .../test_codesize_minimal_pthreads.json | 8 ++++---- ...st_codesize_minimal_pthreads_memgrowth.json | 8 ++++---- 3 files changed, 19 insertions(+), 15 deletions(-) diff --git a/src/lib/libpthread.js b/src/lib/libpthread.js index c9ba01e4820d6..ac112b6a6dc80 100644 --- a/src/lib/libpthread.js +++ b/src/lib/libpthread.js @@ -278,14 +278,18 @@ var LibraryPThread = { #endif // If this message is intended to a recipient that is not the main - // thread, forward it to the target thread. - if (d.targetThread && d.targetThread != _pthread_self()) { + // thread, forward it to the target thread. This is currently only + // used by `CMD_CHECK_MAILBOX`. + if (d.targetThread) { +#if ASSERTIONS + // pthreads should not be relaying messages to themselves. + assert(d.targetThread != _pthread_self()); +#endif var targetWorker = PThread.pthreads[d.targetThread]; - if (targetWorker) { - targetWorker.postMessage(d, d.transferList); - } else { - err(`worker sent message (${cmd}) to pthread (${d.targetThread}) that no longer exists`); - } +#if ASSERTIONS + if (!targetWorker) err(`worker sent message (${cmd}) to pthread (${d.targetThread}) that no longer exists`); +#endif + targetWorker?.postMessage(d); return; } diff --git a/test/codesize/test_codesize_minimal_pthreads.json b/test/codesize/test_codesize_minimal_pthreads.json index 851b17340ee79..7843e557c59d8 100644 --- a/test/codesize/test_codesize_minimal_pthreads.json +++ b/test/codesize/test_codesize_minimal_pthreads.json @@ -1,10 +1,10 @@ { - "a.out.js": 7110, - "a.out.js.gz": 3524, + "a.out.js": 7012, + "a.out.js.gz": 3472, "a.out.nodebug.wasm": 19037, "a.out.nodebug.wasm.gz": 8787, - "total": 26147, - "total_gz": 12311, + "total": 26049, + "total_gz": 12259, "sent": [ "a (memory)", "b (exit)", diff --git a/test/codesize/test_codesize_minimal_pthreads_memgrowth.json b/test/codesize/test_codesize_minimal_pthreads_memgrowth.json index 9dcfca60775f6..079165820445c 100644 --- a/test/codesize/test_codesize_minimal_pthreads_memgrowth.json +++ b/test/codesize/test_codesize_minimal_pthreads_memgrowth.json @@ -1,10 +1,10 @@ { - "a.out.js": 7518, - "a.out.js.gz": 3728, + "a.out.js": 7420, + "a.out.js.gz": 3672, "a.out.nodebug.wasm": 19038, "a.out.nodebug.wasm.gz": 8788, - "total": 26556, - "total_gz": 12516, + "total": 26458, + "total_gz": 12460, "sent": [ "a (memory)", "b (exit)",