Skip to content

Sync to Async helper testcase - #13298

Closed
kripken wants to merge 3 commits into
mainfrom
sync_to_async
Closed

kripken wants to merge 3 commits into
mainfrom
sync_to_async

Conversation

@kripken

@kripken kripken commented Jan 21, 2021

Copy link
Copy Markdown
Member

In recent discussions it came up that a generic helper for running async code
on a worker, and waiting for it synchronously, could be useful. It turns out that
as I'd hoped it is possible to do this in 100% userspace. It's mostly
straightforward, but see the notes in both the utility code and the test code
about lifetime handling.

If this is useful, perhaps we could put it under system/include/emscripten/?

@kripken
kripken requested review from dschuff and sbc100 January 21, 2021 17:43

@sbc100 sbc100 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Nice!

Comment thread tests/core/pthread/sync_to_async.cpp Resolved
Comment thread tests/core/pthread/sync_to_async.cpp Resolved
// for a JS event).
class SyncToAsync {
public:
using Callback = std::function<void()>;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It might be cool to make this whole thing a template, so Callback (i.e. the resume function) could take an argument. Then that could be returned by doWork.
I guess that might make the implementation a little uglier (I'm not sure if you'd be able to use a lambda as you do currently on line 88?), but could make it more useful.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I tinkered with this a little, but I'm not sure how this would help - maybe I'm not getting your idea?

This callback is just to notify the "runtime" that we have finished working. There isn't any argument we want to pass along there - the runtime wouldn't know what to do with any extra arguments?

@kripken

kripken commented Jan 22, 2021

Copy link
Copy Markdown
Member Author

cc @RReverser

// This can be useful if you are a location where blocking is possible (like a
// thread, or when using PROXY_TO_PTHREAD), but you have code that is hard to
// refactor to be async, but that requires some async operation (like waiting
// for a JS event).

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Hmm, if it's waiting for a JS event, wouldn't it be more natural for such waiting code to be executed on the main thread instead of another helper thread instead?

In which case we're coming to something like #9709 - execute a piece of async JS and wait until it finishes.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This is trying to help the use case where the current thread must run synchronously - for example, if the codebase cannot be rewritten to be async - and where Asyncify is not being run (because of overhead, build time, or the need for debug info, for example). You're right that not all things can be run on another thread, but at least some can, like a network fetch.

@kripken

kripken commented Feb 9, 2021

Copy link
Copy Markdown
Member Author

Any further thoughts on the threads here? And thoughts on whether this makes sense as an official API, or should just be a test?

@RReverser

Copy link
Copy Markdown
Collaborator

Any further thoughts on the threads here? And thoughts on whether this makes sense as an official API, or should just be a test?

To be honest, I'm not entirely sure about use-cases for this, but it's likely because I never used Emscripten's event loop functions like emscripten_async_call, so I can't comment much.

Maybe people who used it before have more informed opinions.

@benjamind

Copy link
Copy Markdown

FYI we have started using this in an internal project at Adobe. I'm not yet entirely convinced this is the right API we want longterm, but this pattern is certainly useful to us, and I think is worth further development. I'll try and gather some more thoughts on this when time allows.

@kripken

kripken commented Jul 15, 2021

Copy link
Copy Markdown
Member Author

Replaced by #14666 which improves this and adds more utility code.

@kripken kripken closed this Jul 15, 2021
@kripken
kripken deleted the sync_to_async branch July 15, 2021 23:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants