Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.
Sign upNative WebAssembly threads #8
Comments
binji
added
the
question
label
May 23, 2017
This comment has been minimized.
This comment has been minimized.
lars-t-hansen
commented
May 30, 2017
|
If we have threads then we also need a thread representation. We can make do with integer handles or other semi-transparent types; the question is really whether we would be better off waiting with threads until we have better object representations. (Assuming we can afford to wait that long.) And if we do wait for objects the question is whether code cross-compiled from eg C++ can use those fancy representations. I sense a substantial topic here. |
This comment has been minimized.
This comment has been minimized.
|
I don't think you want forgeable thread ids. Depending on what primitives are available for them, that would preclude secure abstractions. |
This comment has been minimized.
This comment has been minimized.
|
An idea I was proposing earlier was that we:
And this gives you the unforgeable thread-id that is also not tied to any particluar instance and thus compatible with dynamic linking. |
This comment has been minimized.
This comment has been minimized.
|
Extending tables to support threads sounds good, though I'm a bit uneasy about tying threads to tables. What would be the forward compatibility story? When we add a more flexible opaque thread type eventually, would we add another I'm wondering whether it wouldn't make sense to extend Wasm's type system with the notion of opaque types independent of GC types. Or would thread ids need to be GC'ed? |
This comment has been minimized.
This comment has been minimized.
I think you could do this in a forward compatible way by adding an |
This comment has been minimized.
This comment has been minimized.
|
@AndrewScheidecker, I am wondering more about the instruction set. With a more general thread type potentially added later it would be silly to still require a table just to create a thread. |
This comment has been minimized.
This comment has been minimized.
I see what you mean, and it would be unfortunate if we ended up with table and non-table versions of the thread operators. However, I do think in almost all cases you will want to put the thread in a table, because that seems like the best way to get a "handle" to store in linear memory. |
This comment has been minimized.
This comment has been minimized.
|
@AndrewScheidecker, a program using a future GC extension, for example, probably has little reason to even define a linear memory. |
This comment has been minimized.
This comment has been minimized.
|
@rossberg-chromium That's a good question and symmetric to the earlier question we had of whether, instead of |
This comment has been minimized.
This comment has been minimized.
|
@lukewagner, right, and I agree with the constraint, so I was wondering if it is or is not an issue for thread ids. Off-hand I don't see a need for GCing thread ids, but maybe there is some possible thread feature (present or future) I am overlooking that might necessitate it? |
This comment has been minimized.
This comment has been minimized.
|
@rossberg-chromium Oh, I get what you're saying. Even if we can immediately free the OS thread when the startfunc returns (or the thread traps or if we add a
|
This comment has been minimized.
This comment has been minimized.
|
@lukewagner, the first item doesn't seem tied to thread ids, does it? The data would be needed regardless of whether somebody retains an id. So I think it would be owned by the thread, and be freed when the thread terminates. The second item seems more tricky. So far I was assuming that we'd simply use the underlying thread ids of the OS, but yeah, those may be reused too early. |
This comment has been minimized.
This comment has been minimized.
|
@rossberg-chromium For the first item, if the thread terminates and there is no other thread waiting on a We also need to define what happens if |
This comment has been minimized.
This comment has been minimized.
|
Side comment for those who may be following the issues but didn't read the CG meeting notes: We agreed that native threads are not required for the v1 thread proposal. |
referenced
this issue
in opennars/opennars
Oct 24, 2017
This comment has been minimized.
This comment has been minimized.
aardappel
commented
Dec 1, 2017
|
I think native threads are important not just for speed, but for convenience, and for non-web uses. With the current proposal, setting up a threaded application requires a fair bit of JS glue, that has knowledge of the threading requirements of the module. What if I am compiling a bunch of C++ that may or may not spin up threads internally to do work, and I have no idea of them? Ideally in the future this could compile to a single module that would "just work". Even more so in a non-web embedding that does not use JS for dynamic linking / instantiation code. |
This comment has been minimized.
This comment has been minimized.
Agreed, but reusing workers is a simpler target than adding native threads, and allows us reuse functionality we already have ( For a non-web host, I think you could do something like:
Agreed it's not as satisfying as having native wasm threads, though. |
This comment has been minimized.
This comment has been minimized.
aardappel
commented
Dec 1, 2017
|
@binji sure, the host can always take care of it, but I would hope that in the future I can compile C++ code that uses std::thread deep down in a library, and that the resulting wasm can be loaded equally in various embeddings. |
This comment has been minimized.
This comment has been minimized.
binary132
commented
Aug 27, 2018
•
|
I think the population of people who are interested in WebASM iff it trivially supports direct compilation from portable C or C++, with minimal glue, is significant. Personally, if I need to make non-trivial platform-specific code in order to target WebASM, I simply won't target WebASM. Edit: Sounds like this concern is not on point. Thanks for clarifying, @jayphelps! |
This comment has been minimized.
This comment has been minimized.
|
You absolutely can transparently use pthreads or std::thread in C++ using emscripten. https://kripken.github.io/emscripten-site/docs/porting/pthreads.html |
binji commentedMay 23, 2017
The current proposal has no mechanism for creating or joining threads, relying on the embedder to provide these operations. This allows for simple integration with SharedArrayBuffer, where a dedicated worker is used to create a new thread.
There is a drawback for WebAssembly, as a worker is a relatively heavyweight construct, and a native thread would be considerably more lean.
We are confident we'll want support for "native" WebAssembly threads eventually, so the question is whether the current proposal is sufficient for the initial implementation.