Skip to content

Allow emscripten_fetch in Asyncify #12255

Open

Description

@RReverser

Currently there is a perceived inconsistency between APIs like emscripten_sleep and emscripten_fetch: while both perform sort of an async I/O, one of them works with Asyncify, while another works only by compiling code in a separate thread.

Technically, there is no reason for this discrepancy, and it should be possible to allow APIs like emscripten_fetch with just Asyncify if user wants it to run on the main thread instead of a separate one.

Perhaps this is a more general issue though and there should be some shared mechanism for all such APIs to choose between Asyncify and thread-based blocking depending on compile flags?

Activity

  1. kripken commented on Sep 17, 2020

    @kripken
    Member

    I agree it would be better to support all APIs with Asyncify. It does take a little manual work for each which is why that's not the case yet.

    Also no one is actively working on Fetch atm. @juj has plans to create something lower-level and better. We can add Asyncify support more easily for that API, I expect.

  2. RReverser commented on Sep 19, 2020

    @RReverser
    CollaboratorAuthor

    I'm a bit worried about this getting into https://xkcd.com/927/ territory, where we simply will end up with 3 APIs that work in different modes :)

    We already have emscripten_wget and emscripten_fetch, where both do sorts of network requests, but cover different use-cases, and one works only with Asyncify and another only with proxy-to-thread.

    I think ideally we should still consolidate existing APIs to work under either of those modes, regardless of future additions.

  3. stale commented on Jul 31, 2022

    @stale

    This issue has been automatically marked as stale because there has been no activity in the past year. It will be closed automatically if no further activity occurs in the next 30 days. Feel free to re-open at any time if this issue is still relevant.

  4. sbc100 commented on Nov 28, 2022

    @sbc100
    Collaborator

    We should probably asyncify emscripten_thread_sleep too, and perhaps merge it with emscripten_sleep (there should be no need for both of those functions I think).

  5. RReverser commented on Nov 28, 2022

    @RReverser
    CollaboratorAuthor

    Just need to be careful with various conditions: off the main thread emscripten_thread_sleep is more efficient as it just uses atomic.wait; on the main thread emscripten_sleep is better than busy-loop, but only works when user opted in to Asyncify + when used implicitly, can have some surprising effects due to exports becoming async too now.

  6. RReverser commented on Nov 28, 2022

    @RReverser
    CollaboratorAuthor

    Probably expressed myself in a bit confusing manner, so just to clarify: yes, I certainly agree it would be great for emscripten_thread_sleep / usleep / etc. to use Asyncify, just need to make sure we use it only 1) on the main thread and 2) if the user explicitly opted in to Asyncify.

  7. sbc100 commented on Nov 28, 2022

    @sbc100
    Collaborator

    Yup, thanks for extra details there.. certainly seems more complex than I originally though.

  8. RReverser commented on Nov 29, 2022

    @RReverser
    CollaboratorAuthor

    Heh yeah which brings me back to

    Perhaps this is a more general issue though and there should be some shared mechanism for all such APIs to choose between Asyncify and thread-based blocking depending on compile flags?

    It would be cool to solve this problem once for all APIs to avoid repeating complexity. So that there would be a single internal API which you could ask to do something asynchronous, and it would choose a mechanism between Asyncify / pthread / block on the same thread based on user-provided flags.

    sleep and fetch seem like excellent examples to cover first for a PoC.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions