Repository navigation
Conversation
| // for a JS event). | ||
| class SyncToAsync { | ||
| public: | ||
| using Callback = std::function<void()>; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
|
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). |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
|
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 Maybe people who used it before have more informed opinions. |
|
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. |
|
Replaced by #14666 which improves this and adds more utility code. |
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/?