@Dan Gohman In another thread you mentioned thread-local storage.
Thinking about that, on the one hand it would be possible for the code inside the sandbox to handle that completely itself if it allocates thread indexes and reserves memory for TLS. On the other hand, given support for multiple linear memories, it'd be fun if each thread could simply have its own small linear memory in addition to the main shared linear memory. Then it'd be straightforward for each thread to have a "TLS memory".
One complication with that other hand is that linear memories are not first-class. Each load and store has to decide which memory it's referencing in a static immediate field.
That would preclude taking the address of a thread-local variable and doing anything non-local with it.
Ah, I forgot it had to be an immediate.
(If we had a mechanism by which you could have a linear memory that is actually overlapping with the main shared linear memory, that could potentially help with that.)
That aside, would something go horribly wrong if there were a way to have a first-class linear memory object?
Linear memories being non-first-class allows JITs to generate specialized code in several situations.
Fair.
I think we could do this without first-class linear memories though.
Declare a linear memory that consists of a reference to a range of an existing linear memory, and when starting a thread have the ability to say "when this thread references memory N that's this new linear memory".
Then if you want to reference TLS as a generic pointer you translate to the shared linear memory.
All that aside, in the absence of first-class linear memory, it might well be easier and more efficient to just give threads a base address in the main linear memory.
In the instance-per-thread model, this is done with wasm globals, which are per-instance and thus per-thread. In the multiple-threads-in-a-single-instance model, this is expected to be done by having a flag which indicates which globals are thread-local.
Thread-local wasm globals act much like "tls registers" in many platform ABIs. They're basically per-thread pointers.
That allows them to point to a thread's TLS data in a shared address space, which is a simpler model than threads having different base addresses in the address space.
Last updated: Nov 22 2024 at 17:03 UTC