Skip to content

Extract startWorker helper. NFC #18313

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/closure-externs/closure-externs.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ var EMSCRIPTEN$AWAIT$IMPORT;
// Don't minify createRequire
var createRequire;

// Don't minify startWorker which we use to start workers once the runtime is ready.
/**
* @param {Object} Module
*/
var startWorker = function(Module) {};

// Closure externs used by library_sockfs.js

/**
Expand Down
2 changes: 1 addition & 1 deletion src/postamble.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ function run(args) {
readyPromiseResolve(Module);
#endif // MODULARIZE
initRuntime();
postMessage({ 'cmd': 'loaded' });
startWorker(Module);
return;
}
#endif
Expand Down
7 changes: 0 additions & 7 deletions src/postamble_minimal.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,13 +246,6 @@ WebAssembly.instantiate(Module['wasm'], imports).then(function(output) {
#else
ready();
#endif

#if USE_PTHREADS
// This Worker is now ready to host pthreads, tell the main thread we can proceed.
if (ENVIRONMENT_IS_PTHREAD) {
postMessage({ 'cmd': 'loaded' });
}
#endif
}

#if ASSERTIONS || WASM == 2
Expand Down
6 changes: 6 additions & 0 deletions src/shell_minimal.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,12 @@ function ready() {
#elif ASSERTIONS
out('ready() called, and INVOKE_RUN=0. The runtime is now ready for you to call run() to invoke application _main(). You can also override ready() in a --pre-js file to get this signal as a callback')
#endif
#if USE_PTHREADS
// This Worker is now ready to host pthreads, tell the main thread we can proceed.
if (ENVIRONMENT_IS_PTHREAD) {
startWorker(Module);
}
#endif
}

#if POLYFILL
Expand Down
24 changes: 13 additions & 11 deletions src/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,15 @@ self.onunhandledrejection = (e) => {
throw e.reason ?? e;
};

// Add a callback for when the runtime is initialized.
self.startWorker = (instance) => {
#if MODULARIZE
Module = instance;
#endif
// Notify the main thread that this thread has loaded.
postMessage({ 'cmd': 'loaded' });
};

self.onmessage = (e) => {
try {
if (e.data.cmd === 'load') { // Preload command that is called once per worker to parse and load the Emscripten code.
Expand Down Expand Up @@ -167,11 +176,8 @@ self.onmessage = (e) => {
#endif

#if MODULARIZE && EXPORT_ES6
(e.data.urlOrBlob ? import(e.data.urlOrBlob) : import('./{{{ TARGET_JS_NAME }}}')).then(function(exports) {
return exports.default(Module);
}).then(function(instance) {
Module = instance;
});
(e.data.urlOrBlob ? import(e.data.urlOrBlob) : import('./{{{ TARGET_JS_NAME }}}'))
.then(exports => exports.default(Module));
#else
if (typeof e.data.urlOrBlob == 'string') {
#if TRUSTED_TYPES
Expand All @@ -194,13 +200,9 @@ self.onmessage = (e) => {
}
#if MODULARIZE
#if MINIMAL_RUNTIME
{{{ EXPORT_NAME }}}(imports).then(function (instance) {
Module = instance;
});
{{{ EXPORT_NAME }}}(imports);
#else
{{{ EXPORT_NAME }}}(Module).then(function (instance) {
Module = instance;
});
{{{ EXPORT_NAME }}}(Module);
#endif
#endif
#endif // MODULARIZE && EXPORT_ES6
Expand Down