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 (#23110)

Split out from #23106
  • Loading branch information
sbc100 authored Dec 9, 2024
1 parent 03c29b2 commit d1ea4c3
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 33 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/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 d1ea4c3

Please sign in to comment.