Skip to content

Commit

Permalink
Refactor startup code to avoid duplication between wasm workers and p…
Browse files Browse the repository at this point in the history
…threads. NFC

Split out from emscripten-core#23106
  • Loading branch information
sbc100 committed Dec 9, 2024
1 parent 19345a7 commit 1120417
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 38 deletions.
26 changes: 11 additions & 15 deletions src/parseTools.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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 '';
}
Expand Down Expand Up @@ -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({
Expand All @@ -1120,6 +1115,7 @@ addToCompileTimeContext({
TARGET_NOT_SUPPORTED,
WASM_PAGE_SIZE,
ENVIRONMENT_IS_MAIN_THREAD,
ENVIRONMENT_IS_WORKER_THREAD,
addAtExit,
addAtInit,
addReadyPromiseAssertions,
Expand Down
17 changes: 2 additions & 15 deletions src/postamble.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/preamble.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/code_size/hello_wasm_worker_wasm.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,5 @@ WebAssembly.instantiate(b.wasm, {
h = a.h;
c ? (a = b, q(a.sb, a.sz), removeEventListener("message", l), g = g.forEach(k),
addEventListener("message", k)) : a.f();
c || p();
p();
}));
8 changes: 4 additions & 4 deletions test/code_size/hello_wasm_worker_wasm.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"a.html": 618,
"a.html.gz": 384,
"a.js": 665,
"a.js.gz": 455,
"a.js": 662,
"a.js.gz": 452,
"a.ww.js": 115,
"a.ww.js.gz": 127,
"a.wasm": 1881,
"a.wasm.gz": 1065,
"total": 3279,
"total_gz": 2031
"total": 3276,
"total_gz": 2028
}
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_minimal_pthreads.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4208
4209
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_minimal_pthreads.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8703
8716

0 comments on commit 1120417

Please sign in to comment.