Skip to content

[AUDIO_WORKLET] Wait for Wasm instance to start using audio worklet. #24734

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

Closed
Closed
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
2 changes: 2 additions & 0 deletions src/audio_worklet.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ class BootstrapMessages extends AudioWorkletProcessor {
messagePort = this.port;
/** @suppress {checkTypes} */
messagePort.onmessage = async (msg) => {
// Wait for the Wasm module to be instantiated.
await readyPromise;
let d = msg.data;
if (d['_wpn']) {
// '_wpn' is short for 'Worklet Processor Node', using an identifier
Expand Down
4 changes: 2 additions & 2 deletions src/lib/libcore.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ addToLibrary({
// if exit() was called explicitly, warn the user if the runtime isn't actually being shut down
if (keepRuntimeAlive() && !implicit) {
var msg = `program exited (with status: ${status}), but keepRuntimeAlive() is set (counter=${runtimeKeepaliveCounter}) due to an async operation, so halting execution but not exiting the runtime or preventing further async execution (you can use emscripten_force_exit, if you want to force a true shutdown)`;
#if MODULARIZE
#if USE_READY_PROMISE
readyPromiseReject?.(msg);
#endif // MODULARIZE
#endif
err(msg);
}
#endif // ASSERTIONS
Expand Down
4 changes: 2 additions & 2 deletions src/postamble.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ function run() {

#if PTHREADS || WASM_WORKERS
if ({{{ ENVIRONMENT_IS_WORKER_THREAD() }}}) {
#if MODULARIZE
#if USE_READY_PROMISE
readyPromiseResolve?.(Module);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can perhaps remove the question marks here too. i.e. we can make USE_READY_PROMISE imply that readyPromiseResolve exists?

#endif
initRuntime();
Expand Down Expand Up @@ -179,7 +179,7 @@ function run() {
preMain();
#endif

#if MODULARIZE
#if USE_READY_PROMISE
readyPromiseResolve?.(Module);
#endif
#if expectToReceiveOnModule('onRuntimeInitialized')
Expand Down
5 changes: 1 addition & 4 deletions src/postamble_modularize.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ if (runtimeInitialized) {
moduleRtn = Module;
} else {
// Set up the promise that indicates the Module is initialized
moduleRtn = new Promise((resolve, reject) => {
readyPromiseResolve = resolve;
readyPromiseReject = reject;
});
moduleRtn = readyPromise = createReadyPromise();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we just do moduleRtn = readyPromise here and unconditionally call createReadyPromise in runtime_common.js ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think if I do that, I'll have to undo the work you did in https://github.com/emscripten-core/emscripten/pull/24418/files and add back a try/catch.

}
#else
moduleRtn = {};
Expand Down
2 changes: 1 addition & 1 deletion src/preamble.js
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ function abort(what) {
/** @suppress {checkTypes} */
var e = new WebAssembly.RuntimeError(what);

#if MODULARIZE
#if USE_READY_PROMISE
readyPromiseReject?.(e);
#endif
// Throw the error whether or not MODULARIZE is set because abort is used
Expand Down
17 changes: 15 additions & 2 deletions src/runtime_common.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,21 @@
#include "runtime_asan.js"
#endif

#if MODULARIZE && USE_READY_PROMISE
var readyPromiseResolve, readyPromiseReject;
#if USE_READY_PROMISE
var readyPromise, readyPromiseResolve, readyPromiseReject;
function createReadyPromise() {
#if ASSERTIONS
assert(!readyPromise, 'Ready promise already initialized.');
#endif
return new Promise((resolve, reject) => {
readyPromiseResolve = resolve;
readyPromiseReject = reject;
});
}
#if !MODULARIZE
// Modularize mode handles initializing the ready promise.
readyPromise = createReadyPromise();
#endif
#endif

#if PTHREADS || WASM_WORKERS
Expand Down
7 changes: 4 additions & 3 deletions src/settings_internal.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,10 @@ var WASM_EXCEPTIONS = false;
// EXPORTED_FUNCTIONS then this gets set to 0.
var EXPECT_MAIN = true;

// Return a "ready" Promise from the MODULARIZE factory function.
// We disable this under some circumstance if we know its not needed.
var USE_READY_PROMISE = true;
// Create a "ready" Promise that is resolved when the Wasm instance is ready.
// In MODULARIZE mode this Promise is returned from the factory function.
// We omit the Promise under some circumstance if we know it's not needed.
var USE_READY_PROMISE = false;

// If true, building against Emscripten's wasm heap memory profiler.
var MEMORYPROFILER = false;
Expand Down
4 changes: 2 additions & 2 deletions src/shell_minimal.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,9 @@ var err = (...args) => console.error(...args);
// compilation is ready. In that callback, call the function run() to start
// the program.
function ready() {
#if MODULARIZE && USE_READY_PROMISE
#if USE_READY_PROMISE
readyPromiseResolve?.(Module);
#endif // MODULARIZE
#endif
#if INVOKE_RUN && HAS_MAIN
{{{ runIfMainThread("run();") }}}
#elif ASSERTIONS
Expand Down
Loading
Loading