Repository navigation
Conversation
|
I guess this is really 2 utilities at once, right? They don't depend on each other. Aren't we still missing the original use case, which is to synchronously run something on the main thread from a worker (i.e. the worker blocks, but the job runs on the main thread, with the guarantee that it only enters directly from the event queue)? It's not clear to my why the invoke_on_main_thread part is required to be async? |
|
An approach like this could be combined with an API like in #14676 |
Yes, they are for separate use cases that have come up.
I believe that use case from Slack was for async execution on the main thread. (Handling sync in this way would be much harder, and I'm not sure how - we couldn't use the extra async indirection trick...) |
|
|
||
| std::cout << "Perform another synchronous task.\n"; | ||
|
|
||
| sync_to_async.invoke([](emscripten::sync_to_async::Callback resume) { |
There was a problem hiding this comment.
Can the proxying support captured variables? If so, maybe worth demonstrating in tests?
There was a problem hiding this comment.
Yes, any callable object works here, including a std::function with capture. I added an example now to the test.
|
Added a commit to make |
| // Send the work over. | ||
| { | ||
| std::lock_guard<std::mutex> lock(mutex); | ||
| work = newWork; | ||
| finishedWork = false; | ||
| readyToWork = true; | ||
| } | ||
| condition.notify_one(); | ||
|
|
||
| // Wait for it to be complete. | ||
| std::unique_lock<std::mutex> lock(mutex); | ||
| condition.wait(lock, [&]() { | ||
| return finishedWork; | ||
| }); |
There was a problem hiding this comment.
Releasing the lock before notifying the worker thread makes it possible for the worker to respond before we can re-acquire the thread and wait for its response. Missing that response hangs the invoking thread.
| // Send the work over. | |
| { | |
| std::lock_guard<std::mutex> lock(mutex); | |
| work = newWork; | |
| finishedWork = false; | |
| readyToWork = true; | |
| } | |
| condition.notify_one(); | |
| // Wait for it to be complete. | |
| std::unique_lock<std::mutex> lock(mutex); | |
| condition.wait(lock, [&]() { | |
| return finishedWork; | |
| }); | |
| // Send the work over. | |
| std::lock_guard<std::mutex> lock(mutex); | |
| work = newWork; | |
| finishedWork = false; | |
| readyToWork = true; | |
| condition.notify_one(); | |
| // Wait for it to be complete. | |
| condition.wait(lock, [&]() { | |
| return finishedWork; | |
| }); |
There was a problem hiding this comment.
Ah, I just saw this now after opening #15611. Looks like I still take the lock two times there, although it should at least be safe now, I think? But it probably makes sense to lock once, yes...
This adds a utility to proxy code to the main thread in a fully async manner
(where no two async events can end up running in a nested manner due to
the proxy event queue), and a utility to proxy code to a helper thread that can
do async events while we wait synchronously.
This replaces #13298 ; reviving the topic after requests.