Skip to content

Commit c7c84b4

Browse files
authored
Refactor pthread and wasm worker init sequence (#24949)
Rather than running `createWasm` and then returning a promise based on the `wasmModuleReceived` we now do something closer to the way it works under `MINIMUL_RUNTIME`: We delay both `createWasm` and `run` calls until the module is received via postMessage.
1 parent 14dcab4 commit c7c84b4

10 files changed

+68
-53
lines changed

src/postamble.js

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,38 @@
66

77
// === Auto-generated postamble setup entry stuff ===
88

9-
#if MODULARIZE == 'instance'
109
var wasmExports;
11-
#elif WASM_ASYNC_COMPILATION
10+
11+
#if MODULARIZE != 'instance'
12+
13+
#if WASM_WORKERS || PTHREADS
14+
if ({{{ ENVIRONMENT_IS_MAIN_THREAD() }}}) {
15+
// Call createWasm on startup if we are the main thread.
16+
// Worker threads call this once they receive the module via postMessage
17+
#endif
18+
19+
#if WASM_ASYNC_COMPILATION
1220

1321
#if MODULARIZE
1422
// In modularize mode the generated code is within a factory function so we
1523
// can use await here (since it's not top-level-await).
16-
var wasmExports = await createWasm();
24+
wasmExports = await createWasm();
1725
#else
18-
// With async instantation wasmExports is assigned asyncronously when the
19-
// the instance is received.
20-
var wasmExports;
26+
// With async instantation wasmExports is assigned asynchronously when the
27+
// instance is received.
2128
createWasm();
2229
#endif
2330

2431
#else
25-
// With sync compilation wasmExports is directly returned from createWasm()
26-
var wasmExports = createWasm();
32+
wasmExports = createWasm();
33+
#endif
34+
35+
#if WASM_WORKERS || PTHREADS
36+
}
2737
#endif
2838

39+
#endif // MODULARIZE != 'instance'
40+
2941
#if PROXY_TO_WORKER
3042
if (ENVIRONMENT_IS_WORKER) {
3143
#include "webGLWorker.js'
@@ -341,11 +353,6 @@ export default async function init(moduleArg = {}) {
341353
run();
342354
}
343355

344-
#if (WASM_WORKERS || PTHREADS) && !WASM_ESM_INTEGRATION
345-
// When run as a worker thread run `init` immediately.
346-
if ({{{ ENVIRONMENT_IS_WORKER_THREAD() }}}) await init()
347-
#endif
348-
349356
#if ENVIRONMENT_MAY_BE_NODE
350357
// When run as the main script under node we run `init` immediately.
351358
if (ENVIRONMENT_IS_NODE
@@ -367,10 +374,12 @@ if (ENVIRONMENT_IS_SHELL) {
367374
}
368375
#endif
369376

370-
#else
377+
#else // MODULARIZE=instance
378+
371379
preInit();
372-
run();
373-
#endif
380+
{{{ runIfMainThread('run();') }}}
381+
382+
#endif // MODULARIZE=instance
374383

375384
#if BUILD_AS_WORKER
376385

src/preamble.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -959,14 +959,13 @@ function getWasmImports() {
959959

960960
#if PTHREADS || WASM_WORKERS
961961
if ({{{ ENVIRONMENT_IS_WORKER_THREAD() }}}) {
962-
return new Promise((resolve) => {
963-
wasmModuleReceived = (module) => {
964-
// Instantiate from the module posted from the main thread.
965-
// We can just use sync instantiation in the worker.
966-
var instance = new WebAssembly.Instance(module, getWasmImports());
967-
resolve(receiveInstance(instance, module));
968-
};
969-
});
962+
// Instantiate from the module that was recieved via postMessage from
963+
// the main thread. We can just use sync instantiation in the worker.
964+
#if ASSERTIONS
965+
assert(wasmModule, "wasmModule should have been received via postMessage");
966+
#endif
967+
var instance = new WebAssembly.Instance(wasmModule, getWasmImports());
968+
return receiveInstance(instance, wasmModule);
970969
}
971970
#endif
972971

src/runtime_common.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,7 @@ function growMemViews() {
3131
var readyPromiseResolve, readyPromiseReject;
3232
#endif
3333

34-
#if PTHREADS || WASM_WORKERS
35-
#if !MINIMAL_RUNTIME
36-
var wasmModuleReceived;
37-
#endif
38-
39-
#if ENVIRONMENT_MAY_BE_NODE && !WASM_ESM_INTEGRATION
34+
#if (PTHREADS || WASM_WORKERS) && (ENVIRONMENT_MAY_BE_NODE && !WASM_ESM_INTEGRATION)
4035
if (ENVIRONMENT_IS_NODE && {{{ ENVIRONMENT_IS_WORKER_THREAD() }}}) {
4136
// Create as web-worker-like an environment as we can.
4237
var parentPort = worker_threads['parentPort'];
@@ -46,8 +41,7 @@ if (ENVIRONMENT_IS_NODE && {{{ ENVIRONMENT_IS_WORKER_THREAD() }}}) {
4641
postMessage: (msg) => parentPort['postMessage'](msg),
4742
});
4843
}
49-
#endif // ENVIRONMENT_MAY_BE_NODE && !WASM_ESM_INTEGRATION
50-
#endif
44+
#endif // (PTHREADS || WASM_WORKERS) && (ENVIRONMENT_MAY_BE_NODE && !WASM_ESM_INTEGRATION)
5145

5246
#if PTHREADS
5347
#include "runtime_pthread.js"

src/runtime_pthread.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,13 @@ if (ENVIRONMENT_IS_PTHREAD) {
122122
Module['wasm'] = msgData.wasmModule;
123123
loadModule();
124124
#else
125-
wasmModuleReceived(msgData.wasmModule);
125+
wasmModule = msgData.wasmModule;
126+
#if MODULARIZE == 'instance'
127+
init();
128+
#else
129+
createWasm();
130+
run();
131+
#endif
126132
#endif // MINIMAL_RUNTIME
127133
#endif
128134
} else if (cmd === 'run') {

src/wasm_worker.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,13 @@ function startWasmWorker(props) {
1818
Module['wasm'] = props.wasm;
1919
loadModule()
2020
#else
21-
wasmModuleReceived(props.wasm);
21+
wasmModule = props.wasm;
22+
#if MODULARIZE == 'instance'
23+
init();
24+
#else
25+
createWasm();
26+
run();
27+
#endif
2228
#endif
2329
// Drop now unneeded references to from the Module object in this Worker,
2430
// these are not needed anymore.

test/code_size/test_codesize_minimal_esm.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
2-
"a.out.js": 2513,
3-
"a.out.js.gz": 1229,
2+
"a.out.js": 2515,
3+
"a.out.js.gz": 1230,
44
"a.out.nodebug.wasm": 62,
55
"a.out.nodebug.wasm.gz": 76,
6-
"total": 2575,
7-
"total_gz": 1305,
6+
"total": 2577,
7+
"total_gz": 1306,
88
"sent": [],
99
"imports": [],
1010
"exports": [

test/code_size/test_codesize_minimal_pthreads.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
2-
"a.out.js": 7529,
3-
"a.out.js.gz": 3720,
2+
"a.out.js": 7499,
3+
"a.out.js.gz": 3719,
44
"a.out.nodebug.wasm": 19588,
55
"a.out.nodebug.wasm.gz": 9025,
6-
"total": 27117,
7-
"total_gz": 12745,
6+
"total": 27087,
7+
"total_gz": 12744,
88
"sent": [
99
"a (memory)",
1010
"b (emscripten_get_now)",

test/code_size/test_codesize_minimal_pthreads_memgrowth.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
2-
"a.out.js": 7956,
2+
"a.out.js": 7922,
33
"a.out.js.gz": 3920,
44
"a.out.nodebug.wasm": 19589,
55
"a.out.nodebug.wasm.gz": 9025,
6-
"total": 27545,
6+
"total": 27511,
77
"total_gz": 12945,
88
"sent": [
99
"a (memory)",
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
{
2-
"hello_world.js": 54085,
3-
"hello_world.js.gz": 17171,
2+
"hello_world.js": 54086,
3+
"hello_world.js.gz": 17172,
44
"hello_world.wasm": 15143,
55
"hello_world.wasm.gz": 7452,
6-
"no_asserts.js": 26818,
7-
"no_asserts.js.gz": 8989,
6+
"no_asserts.js": 26819,
7+
"no_asserts.js.gz": 8991,
88
"no_asserts.wasm": 12227,
99
"no_asserts.wasm.gz": 6008,
10-
"strict.js": 52135,
11-
"strict.js.gz": 16503,
10+
"strict.js": 52136,
11+
"strict.js.gz": 16504,
1212
"strict.wasm": 15143,
1313
"strict.wasm.gz": 7438,
14-
"total": 175551,
15-
"total_gz": 63561
14+
"total": 175554,
15+
"total_gz": 63565
1616
}

test/other/codesize/test_codesize_minimal_O0.expected.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1309,9 +1309,10 @@ var _global_val = Module['_global_val'] = 65536;var wasmImports = {
13091309
// include: postamble.js
13101310
// === Auto-generated postamble setup entry stuff ===
13111311

1312+
var wasmExports;
1313+
13121314
// With async instantation wasmExports is assigned asyncronously when the
13131315
// the instance is received.
1314-
var wasmExports;
13151316
createWasm();
13161317

13171318
var calledRun;

0 commit comments

Comments
 (0)