Skip to content

emscripten/thread_util.h: Utilities for convenient C++ proxying - #14666

Open
kripken wants to merge 30 commits into
mainfrom
threadutil
Open

kripken wants to merge 30 commits into
mainfrom
threadutil

Conversation

@kripken

@kripken kripken commented Jul 15, 2021

Copy link
Copy Markdown
Member

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.

@dschuff

dschuff commented Jul 16, 2021

Copy link
Copy Markdown
Member

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?

@dschuff

dschuff commented Jul 16, 2021

Copy link
Copy Markdown
Member

An approach like this could be combined with an API like in #14676
Then run_on_main_thread could call it and only re-enqueue the event again if necessary.
Originally I was looking for a way to give this capability to the full set of C APIs (which would let you pass arguments, etc), but that seemed likely to be a lot more invasive.

@kripken

kripken commented Jul 22, 2021

Copy link
Copy Markdown
Member Author

@dschuff

I guess this is really 2 utilities at once, right? They don't depend on each other.

Yes, they are for separate use cases that have come up.

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?

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...)

Comment thread tests/core/pthread/sync_to_async.cpp Outdated

std::cout << "Perform another synchronous task.\n";

sync_to_async.invoke([](emscripten::sync_to_async::Callback resume) {

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.

Can the proxying support captured variables? If so, maybe worth demonstrating in tests?

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.

Yes, any callable object works here, including a std::function with capture. I added an example now to the test.

@kripken

kripken commented Sep 3, 2021

Copy link
Copy Markdown
Member Author

Added a commit to make sync_to_async threadsafe.

Comment on lines +187 to +200
// 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;
});

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.

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.

Suggested change
// 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;
});

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.

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 branch has not been deployed

No deployments
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.

4 participants