Skip to content

Similarity to Emscripten's proxying API #1

Description

@tlively

The pattern of sending asynchronous work to another thread and synchronously waiting for it to complete is well supported by emscripten_proxy_sync_with_ctx from Emscripten's proxying.h library. The main idea appears to be the same as the POC in this repo. That API and the others in proxying.h are implemented on top of low-level task queues (header, implementation) that ultimately use Atomics.waitAsync and __builtin_wasm_memory_atomic_notify if possible or fall back to postMessage if necessary to send work to other threads.

There's also an Emscripten contributor currently working on adding an MAIN_THREAD_EM_ASM_PROMISE_AWAIT macro built on top of the proxying.h APIs to make synchronously waiting for some promise to be resolved on the main thread very easy.

Despite there being very mature support for this proxying pattern today, it does not solve all the problems JSPI solves and it introduces new problems of its own.

  • Deploying threaded Wasm applications is still a pain due to COOP and COEP requirements, making it a non-starter for many users, especially library developers that have no control over the serving infrastructure that will eventually be used. JSPI does not have these onerous deployment requirements.
  • JS run on a dedicated worker thread cannot interact with the rest of the JS in an application. This may be fine for isolated JS used only to bridge C/C++ libraries to Web APIs, but this is a problem when the Wasm must interact with nontrivial application state held in JS. JSPI does not have this problem because all code is run on the same thread.
    • Moving application JS state to the worker thread is generally a non-starter.
    • Moving Wasm execution to the worker thread and proxying asynchronous work back to the main thread is generally more feasible, but introduces complexity and overhead when calling into Wasm.
  • When running application logic on the main thread, synchronously waiting for another thread to complete a long-running asynchronous operation may introduce jank or even prompt an unresponsive tab dialogue. In contrast, JSPI presents the same observed behavior to the application without ever blocking the main thread.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions