emscripten icon indicating copy to clipboard operation
emscripten copied to clipboard

Support preallocated pthread & wasm worker from blob

Open ifiddynine opened this issue 1 year ago • 0 comments

This amends existing mainScriptUrlOrBlob logic to make sure that we can preload JS for pthread and wasm workers reliably.

On the pthread side, this amends PTHREAD_POOL_SIZE warmup logic to allow waiting for a promise on top of existing logic in PThread.allocateUnusedWorker. Otherwise, there would be a race which would probably mean the JS file would be loaded several times anyways.

On the wasm worker side, simply fixes the ability to provide a blob - previously it would only accept a javascript string. We use WASM_WORKERS=2 to keep this working, mixing wasm workers + pthread.

See below for example of how it's used. I considered just refactoring the scriptUrlOrBlobPromise logic to automatically cache all worker startups, but figured that would be harder to get integrated since it would break backwards compatibility & tests that use it in emscripten main.

Module['mainScriptUrlOrBlobPromise'] = fetch('myscript.js', { priority: 'high' })
    .then(response => response.text())
    .then(workerScript => {
    Module['mainScriptUrlOrBlob'] = new Blob([workerScript], { type: 'application/javascript' });
});

ifiddynine avatar Sep 13 '24 00:09 ifiddynine