Skip to content

Commit d1ea4c3

Browse files
authored
Refactor startup code to avoid duplication between wasm workers and pthreads. NFC (#23110)
Split out from #23106
1 parent 03c29b2 commit d1ea4c3

File tree

5 files changed

+16
-33
lines changed

5 files changed

+16
-33
lines changed

src/parseTools.mjs

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -785,24 +785,16 @@ export function modifyJSFunction(text, func) {
785785
}
786786

787787
export function runIfMainThread(text) {
788-
if (WASM_WORKERS && PTHREADS) {
789-
return `if (!ENVIRONMENT_IS_WASM_WORKER && !ENVIRONMENT_IS_PTHREAD) { ${text} }`;
790-
} else if (WASM_WORKERS) {
791-
return `if (!ENVIRONMENT_IS_WASM_WORKER) { ${text} }`;
792-
} else if (PTHREADS) {
793-
return `if (!ENVIRONMENT_IS_PTHREAD) { ${text} }`;
788+
if (WASM_WORKERS || PTHREADS) {
789+
return `if (${ENVIRONMENT_IS_MAIN_THREAD()}) { ${text} }`;
794790
} else {
795791
return text;
796792
}
797793
}
798794

799795
function runIfWorkerThread(text) {
800-
if (WASM_WORKERS && PTHREADS) {
801-
return `if (ENVIRONMENT_IS_WASM_WORKER || ENVIRONMENT_IS_PTHREAD) { ${text} }`;
802-
} else if (WASM_WORKERS) {
803-
return `if (ENVIRONMENT_IS_WASM_WORKER) { ${text} }`;
804-
} else if (PTHREADS) {
805-
return `if (ENVIRONMENT_IS_PTHREAD) { ${text} }`;
796+
if (WASM_WORKERS || PTHREADS) {
797+
return `if (${ENVIRONMENT_IS_WORKER_THREAD()}) { ${text} }`;
806798
} else {
807799
return '';
808800
}
@@ -1094,12 +1086,15 @@ function implicitSelf() {
10941086
}
10951087

10961088
function ENVIRONMENT_IS_MAIN_THREAD() {
1089+
return `(!${ENVIRONMENT_IS_WORKER_THREAD()})`;
1090+
}
1091+
1092+
function ENVIRONMENT_IS_WORKER_THREAD() {
1093+
assert(PTHREADS || WASM_WORKERS);
10971094
var envs = [];
10981095
if (PTHREADS) envs.push('ENVIRONMENT_IS_PTHREAD');
10991096
if (WASM_WORKERS) envs.push('ENVIRONMENT_IS_WASM_WORKER');
1100-
if (AUDIO_WORKLET) envs.push('ENVIRONMENT_IS_AUDIO_WORKLET');
1101-
if (envs.length == 0) return 'true';
1102-
return '(!(' + envs.join('||') + '))';
1097+
return '(' + envs.join('||') + ')';
11031098
}
11041099

11051100
addToCompileTimeContext({
@@ -1120,6 +1115,7 @@ addToCompileTimeContext({
11201115
TARGET_NOT_SUPPORTED,
11211116
WASM_PAGE_SIZE,
11221117
ENVIRONMENT_IS_MAIN_THREAD,
1118+
ENVIRONMENT_IS_WORKER_THREAD,
11231119
addAtExit,
11241120
addAtInit,
11251121
addReadyPromiseAssertions,

src/postamble.js

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -161,25 +161,12 @@ function run() {
161161
return;
162162
}
163163

164-
#if WASM_WORKERS
165-
if (ENVIRONMENT_IS_WASM_WORKER) {
164+
#if PTHREADS || WASM_WORKERS
165+
if ({{{ ENVIRONMENT_IS_WORKER_THREAD() }}}) {
166166
#if MODULARIZE
167167
readyPromiseResolve(Module);
168-
#endif // MODULARIZE
169-
return initRuntime();
170-
}
171168
#endif
172-
173-
#if PTHREADS
174-
if (ENVIRONMENT_IS_PTHREAD) {
175-
#if MODULARIZE
176-
// The promise resolve function typically gets called as part of the execution
177-
// of `doRun` below. The workers/pthreads don't execute `doRun` so the
178-
// creation promise can be resolved, marking the pthread-Module as initialized.
179-
readyPromiseResolve(Module);
180-
#endif // MODULARIZE
181169
initRuntime();
182-
startWorker(Module);
183170
return;
184171
}
185172
#endif

src/preamble.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ function initRuntime() {
219219
#endif
220220

221221
#if PTHREADS
222-
if (ENVIRONMENT_IS_PTHREAD) return;
222+
if (ENVIRONMENT_IS_PTHREAD) return startWorker(Module);
223223
#endif
224224

225225
#if STACK_OVERFLOW_CHECK
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4208
1+
4209
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8703
1+
8716

0 commit comments

Comments
 (0)