From d1ea4c36e9c4c80e3782fc7890c8496e2fba93fc Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Mon, 9 Dec 2024 14:06:10 -0800 Subject: [PATCH] Refactor startup code to avoid duplication between wasm workers and pthreads. NFC (#23110) Split out from #23106 --- src/parseTools.mjs | 26 ++++++++----------- src/postamble.js | 17 ++---------- src/preamble.js | 2 +- .../test_codesize_minimal_pthreads.gzsize | 2 +- .../test_codesize_minimal_pthreads.jssize | 2 +- 5 files changed, 16 insertions(+), 33 deletions(-) diff --git a/src/parseTools.mjs b/src/parseTools.mjs index ca474affa3978..fa6da8556e314 100644 --- a/src/parseTools.mjs +++ b/src/parseTools.mjs @@ -785,24 +785,16 @@ export function modifyJSFunction(text, func) { } export function runIfMainThread(text) { - if (WASM_WORKERS && PTHREADS) { - return `if (!ENVIRONMENT_IS_WASM_WORKER && !ENVIRONMENT_IS_PTHREAD) { ${text} }`; - } else if (WASM_WORKERS) { - return `if (!ENVIRONMENT_IS_WASM_WORKER) { ${text} }`; - } else if (PTHREADS) { - return `if (!ENVIRONMENT_IS_PTHREAD) { ${text} }`; + if (WASM_WORKERS || PTHREADS) { + return `if (${ENVIRONMENT_IS_MAIN_THREAD()}) { ${text} }`; } else { return text; } } function runIfWorkerThread(text) { - if (WASM_WORKERS && PTHREADS) { - return `if (ENVIRONMENT_IS_WASM_WORKER || ENVIRONMENT_IS_PTHREAD) { ${text} }`; - } else if (WASM_WORKERS) { - return `if (ENVIRONMENT_IS_WASM_WORKER) { ${text} }`; - } else if (PTHREADS) { - return `if (ENVIRONMENT_IS_PTHREAD) { ${text} }`; + if (WASM_WORKERS || PTHREADS) { + return `if (${ENVIRONMENT_IS_WORKER_THREAD()}) { ${text} }`; } else { return ''; } @@ -1094,12 +1086,15 @@ function implicitSelf() { } function ENVIRONMENT_IS_MAIN_THREAD() { + return `(!${ENVIRONMENT_IS_WORKER_THREAD()})`; +} + +function ENVIRONMENT_IS_WORKER_THREAD() { + assert(PTHREADS || WASM_WORKERS); var envs = []; if (PTHREADS) envs.push('ENVIRONMENT_IS_PTHREAD'); if (WASM_WORKERS) envs.push('ENVIRONMENT_IS_WASM_WORKER'); - if (AUDIO_WORKLET) envs.push('ENVIRONMENT_IS_AUDIO_WORKLET'); - if (envs.length == 0) return 'true'; - return '(!(' + envs.join('||') + '))'; + return '(' + envs.join('||') + ')'; } addToCompileTimeContext({ @@ -1120,6 +1115,7 @@ addToCompileTimeContext({ TARGET_NOT_SUPPORTED, WASM_PAGE_SIZE, ENVIRONMENT_IS_MAIN_THREAD, + ENVIRONMENT_IS_WORKER_THREAD, addAtExit, addAtInit, addReadyPromiseAssertions, diff --git a/src/postamble.js b/src/postamble.js index 075ed1048e48d..c4c25cfb4a8d8 100644 --- a/src/postamble.js +++ b/src/postamble.js @@ -161,25 +161,12 @@ function run() { return; } -#if WASM_WORKERS - if (ENVIRONMENT_IS_WASM_WORKER) { +#if PTHREADS || WASM_WORKERS + if ({{{ ENVIRONMENT_IS_WORKER_THREAD() }}}) { #if MODULARIZE readyPromiseResolve(Module); -#endif // MODULARIZE - return initRuntime(); - } #endif - -#if PTHREADS - if (ENVIRONMENT_IS_PTHREAD) { -#if MODULARIZE - // The promise resolve function typically gets called as part of the execution - // of `doRun` below. The workers/pthreads don't execute `doRun` so the - // creation promise can be resolved, marking the pthread-Module as initialized. - readyPromiseResolve(Module); -#endif // MODULARIZE initRuntime(); - startWorker(Module); return; } #endif diff --git a/src/preamble.js b/src/preamble.js index 23ee003bb5592..1ca3da0389a93 100644 --- a/src/preamble.js +++ b/src/preamble.js @@ -219,7 +219,7 @@ function initRuntime() { #endif #if PTHREADS - if (ENVIRONMENT_IS_PTHREAD) return; + if (ENVIRONMENT_IS_PTHREAD) return startWorker(Module); #endif #if STACK_OVERFLOW_CHECK diff --git a/test/other/codesize/test_codesize_minimal_pthreads.gzsize b/test/other/codesize/test_codesize_minimal_pthreads.gzsize index 0f12704884a5d..e47cad5f248ac 100644 --- a/test/other/codesize/test_codesize_minimal_pthreads.gzsize +++ b/test/other/codesize/test_codesize_minimal_pthreads.gzsize @@ -1 +1 @@ -4208 +4209 diff --git a/test/other/codesize/test_codesize_minimal_pthreads.jssize b/test/other/codesize/test_codesize_minimal_pthreads.jssize index db6dfb1df8651..7db41a45918a0 100644 --- a/test/other/codesize/test_codesize_minimal_pthreads.jssize +++ b/test/other/codesize/test_codesize_minimal_pthreads.jssize @@ -1 +1 @@ -8703 +8716