Repository navigation
[WasmFS] Replace SyncToAsync with the proxying.h API - #16469
Conversation
Add a thin C++ wrapper API to proxying.h that wraps the underlying C API in the same file. The C++ wrapper is defined only when using C++ at least as recent as C++11. The wrapper handles creating and destroying the underlying em_proxying_queue objects automatically, defines proxying methods that take `std::function` rather than void*, and wraps `em_proxying_ctx` in a class with a `finish()` method.
Create a `ProxyWorker` utility that owns a dedicated worker thread and a ProxyingQueue used to send work to it, then use it to replace the previous bespoke SyncToAsync utility. This should save code size by reusing the same underlying proxying API already used by Emscripten's low-level pthread runtime. It also improves performance scaling by allowing multiple asynchronous tasks to be in flight at once on the worker thread; the previous utility serialized all proxied tasks, even if they were asynchronous.
|
|
||
| // Kill the worker thread. | ||
| ~ProxyWorker() { | ||
| pthread_cancel(thread.native_handle()); |
There was a problem hiding this comment.
I would prefer to avoid using pthread_cancel if at all possible.
Can we come up with standard mechanism for sending some kind shutdown message to the queue?
There was a problem hiding this comment.
One option would be to have the worker thread wait for work inside a loop using a traditional condition variable. Then it could exit the loop and shut itself down normally once we signal it to do so. But it's kind of nice to be able to have it so simply wait in the JS event loop instead. Given the need to tell another thread sitting in its event loop to shut down, wouldn't any mechanism for doing so basically be identical to what pthread_cancel does in that case?
| cflags = ['-fno-exceptions', '-std=c++17'] | ||
|
|
||
| includes = ['system/lib/wasmfs'] | ||
| includes = ['system/lib/wasmfs', 'system/lib/pthread'] |
There was a problem hiding this comment.
This won't be needed once we make the header public so should we wait for that change?
There was a problem hiding this comment.
I don't think it makes too much difference either way.
There was a problem hiding this comment.
Agreed, as long as we remember to clean this up if it does land.
| sched_yield(); | ||
| } | ||
|
|
||
| test_proxy_async(); |
There was a problem hiding this comment.
Why has this test changed?
There was a problem hiding this comment.
I realized that this new simpler pattern should work when implementing the new utility, so I changed the test to make sure that pattern was being directly tested.
| em_proxying_ctx* ctx; | ||
|
|
||
| public: | ||
| struct ProxyingCtx { |
There was a problem hiding this comment.
I guess you are using struct here because all things are public? Is that common/accepted practice. My intuition is that struct is makes sense for things that are more like POD data structures... and I'm not sure this is. But maybe my intuition is out of date.
There was a problem hiding this comment.
Yes, I want the ctx member to be public and this seemed like the simplest way to do it. I don't think we have a strong guideline about when to use class or struct, but maybe we should. I can change this to a class if you prefer.
|
@kripken, the problem here is that the proxying queue API is not available when -pthread is not used, which happens to cause link errors in the dlfcn_self test because it exports wasmfs_create_proxied_backend, which depends on the proxying queue API after this change. The obvious fix is to provide a stub of the proxying queue API for when threads are not available, but what do we want the proxying backend to do in the absence of threads? There's no way it can work correctly in that situation. |
|
How about ifdefing out (was that comment meant for someone else perhaps? I don't see a comment of mine that it refers to.) |
|
That sounds reasonable, and we'll ifdef out the OPFS backend, too, since it will internally require threads to bridge the sync-async gap. (I wasn't replying to a previous comment, sorry for the confusion!) |
|
Actually ifdef doesn't work too well in this situation. The problem is that the failing test ends up including all system libraries, inluding all of wasmfs, into the main module (since it doesn't know what system libraries will be needed by side modules). Since part of wasmfs now depends on the proxying API, that means we need the proxying API to be defined as part of the link. One solution would be to ifdef out every use of the proxying API in wasmfs, but that's messy and fragile. Instead I've included a stub implementation of the proxying API into the single threaded link. We should still make backends that depend on threads unavailable to users at a higher level, though. |
|
|
||
| em_proxying_queue* em_proxying_queue_create() { | ||
| return NULL; | ||
| } |
There was a problem hiding this comment.
lgtm with that (seems safer to avoid confusion later), or lgtm if there's a good reason not to as well.
There was a problem hiding this comment.
Where exactly are the references coming from? Perhaps we can instead use the musl trick of using weak aliases to dummy functions?
Alternately, what happens if we just include the regular version and let it call the stub pthreads function?
There was a problem hiding this comment.
That would almost work out-of-the-box, but it does call out to _emscripten_notify_proxying_queue in library_pthread.js, so we'd at least need to supply a stub for that. But it would still be a user error to use any of the proxying functions except for async proxying to self, which could still work.
There was a problem hiding this comment.
Where exactly are the references coming from? Perhaps we can instead use the musl trick of using weak aliases to dummy functions?
The references are coming from various WasmFS backends that get pulled in by --whole-archive. If we used weak aliases, how would that differ from the stub solution here. Would it be cleaner?
There was a problem hiding this comment.
Very little difference I guess. Using the weak aliases has the downside that it could hide real link failures by failling back to dummy functions if the real library is missing/invalid for some other reason.
The fact the some of these tests/setting cause all library to be included in with |
|
I updated all the functions that do anything with proxying queues to abort. This allows single-threaded applications to safely try to create or destroy queues as long as they don't try to proxy any work, which seems like a sweet spot for portability. |
|
Hmm, that |
|
@sbc100 do you know what happened to |
Its a library function so any callers need to explicitly include it in their |
|
Yep, adding the deps fixed it locally. Thanks! |
I accidentally included this file in #16469.
This file was accidentally included in #16469.
Create a
ProxyWorkerutility that owns a dedicated worker thread and aProxyingQueue used to send work to it, then use it to replace the previous
bespoke SyncToAsync utility. This should save code size by reusing the same
underlying proxying API already used by Emscripten's low-level pthread runtime.
It also improves performance scaling by allowing multiple asynchronous tasks to
be in flight at once on the worker thread; the previous utility serialized all
proxied tasks, even if they were asynchronous.