Skip to content

Commit cd09e37

Browse files
authored
Convert loadWasmModuleToAllWorkers to async. NFC (#24934)
1 parent 96bd326 commit cd09e37

File tree

4 files changed

+31
-25
lines changed

4 files changed

+31
-25
lines changed

src/lib/libpthread.js

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -107,14 +107,18 @@ var LibraryPThread = {
107107
PThread.allocateUnusedWorker();
108108
}
109109
#endif
110-
#if !MINIMAL_RUNTIME
110+
#if !MINIMAL_RUNTIME && PTHREAD_POOL_SIZE
111111
// MINIMAL_RUNTIME takes care of calling loadWasmModuleToAllWorkers
112112
// in postamble_minimal.js
113-
addOnPreRun(() => {
114-
addRunDependency('loading-workers')
115-
PThread.loadWasmModuleToAllWorkers(() => removeRunDependency('loading-workers'));
113+
addOnPreRun(async () => {
114+
var pthreadPoolReady = PThread.loadWasmModuleToAllWorkers();
115+
#if !PTHREAD_POOL_DELAY_LOAD
116+
addRunDependency('loading-workers');
117+
await pthreadPoolReady;
118+
removeRunDependency('loading-workers');
119+
#endif // PTHREAD_POOL_DELAY_LOAD
116120
});
117-
#endif
121+
#endif // !MINIMAL_RUNTIME && PTHREAD_POOL_SIZE
118122
#if MAIN_MODULE
119123
PThread.outstandingPromises = {};
120124
// Finished threads are threads that have finished running but we not yet
@@ -366,18 +370,16 @@ var LibraryPThread = {
366370
});
367371
}),
368372

369-
loadWasmModuleToAllWorkers(onMaybeReady) {
370-
#if !PTHREAD_POOL_SIZE
371-
onMaybeReady();
372-
#else
373+
#if PTHREAD_POOL_SIZE
374+
async loadWasmModuleToAllWorkers() {
373375
// Instantiation is synchronous in pthreads.
374376
if (
375377
ENVIRONMENT_IS_PTHREAD
376378
#if WASM_WORKERS
377379
|| ENVIRONMENT_IS_WASM_WORKER
378380
#endif
379381
) {
380-
return onMaybeReady();
382+
return;
381383
}
382384

383385
let pthreadPoolReady = Promise.all(PThread.unusedWorkers.map(PThread.loadWasmModuleToWorker));
@@ -387,12 +389,11 @@ var LibraryPThread = {
387389
// If the user wants to wait on it elsewhere, they can do so via the
388390
// Module['pthreadPoolReady'] promise.
389391
Module['pthreadPoolReady'] = pthreadPoolReady;
390-
onMaybeReady();
391-
#else
392-
pthreadPoolReady.then(onMaybeReady);
393-
#endif // PTHREAD_POOL_DELAY_LOAD
394-
#endif // PTHREAD_POOL_SIZE
392+
return;
393+
#endif
394+
return pthreadPoolReady;
395395
},
396+
#endif // PTHREAD_POOL_SIZE
396397

397398
// Creates a new web Worker and places it in the unused worker pool to wait for its use.
398399
allocateUnusedWorker() {

src/postamble_minimal.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,13 @@ WebAssembly.instantiate(Module['wasm'], imports).then(/** @suppress {missingProp
218218

219219
initRuntime(wasmExports);
220220

221-
#if PTHREADS
222-
PThread.loadWasmModuleToAllWorkers(ready);
221+
#if PTHREADS && PTHREAD_POOL_SIZE
222+
var workersReady = PThread.loadWasmModuleToAllWorkers();
223+
#if PTHREAD_POOL_DELAY_LOAD
224+
ready();
225+
#else
226+
workersReady.then(ready);
227+
#endif
223228
#else
224229
ready();
225230
#endif

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": 7581,
3-
"a.out.js.gz": 3736,
2+
"a.out.js": 7529,
3+
"a.out.js.gz": 3720,
44
"a.out.nodebug.wasm": 19590,
55
"a.out.nodebug.wasm.gz": 9031,
6-
"total": 27171,
7-
"total_gz": 12767,
6+
"total": 27119,
7+
"total_gz": 12751,
88
"sent": [
99
"a (memory)",
1010
"b (emscripten_get_now)",

test/code_size/test_codesize_minimal_pthreads_memgrowth.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
2-
"a.out.js": 8012,
3-
"a.out.js.gz": 3939,
2+
"a.out.js": 7956,
3+
"a.out.js.gz": 3920,
44
"a.out.nodebug.wasm": 19591,
55
"a.out.nodebug.wasm.gz": 9031,
6-
"total": 27603,
7-
"total_gz": 12970,
6+
"total": 27547,
7+
"total_gz": 12951,
88
"sent": [
99
"a (memory)",
1010
"b (emscripten_get_now)",

0 commit comments

Comments
 (0)