-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WASI: Implement experimental threading support #16207
Commits on Jun 26, 2023
-
wasm-ld: implement
--export-memory
flagThis flag allows the user to force export the memory to the host environment. This is useful when the memory is imported from the host but must also be exported. This is (currently) required to pass the memory validation for runtimes when using threads. In this future this may become an error instead.
Configuration menu - View commit details
-
Copy full SHA for 3819371 - Browse repository at this point
Copy the full SHA 3819371View commit details -
Compilation: allow threads for Wasm when shared-memory is enabled
When the user enabled the linker-feature 'shared-memory' we do not force a singlethreaded build. The linker already verifies all other CPU features required for threads are enabled. This is true for both WASI and freestanding.
Configuration menu - View commit details
-
Copy full SHA for 062eb6f - Browse repository at this point
Copy the full SHA 062eb6fView commit details -
std: implement
Futex
for WebAssemblyImplements std's `Futex` for the WebAssembly target using Wasm's `atomics` instruction set. When the `atomics` cpu feature is disabled we emit a compile-error.
Configuration menu - View commit details
-
Copy full SHA for ea0d4c8 - Browse repository at this point
Copy the full SHA ea0d4c8View commit details -
std: implement
Thread
spawn
for WASIThis implements a first version to spawn a WASI-thread. For a new thread to be created, we calculate the size required to store TLS, the new stack, and metadata. This size is then allocated using a user-provided allocator. After a new thread is spawn, the HOST will call into our bootstrap procedure. This bootstrap procedure will then initialize the TLS segment and set the newly spawned thread's TID. It will also set the stack pointer to the newly created stack to ensure we do not clobber the main thread's stack. When bootstrapping the thread is completed, we will call the user's function on this new thread.
Configuration menu - View commit details
-
Copy full SHA for a97dbdf - Browse repository at this point
Copy the full SHA a97dbdfView commit details -
store allocator & remove global assembly
We now store the original allocator that was used to allocate the memory required for the thread. This allocator can then be used in any cleanup functionality to ensure the memory is freed correctly. Secondly, we now use a function to set the stack pointer instead of generating a function using global assembly. This is a lot cleaner and more readable.
Configuration menu - View commit details
-
Copy full SHA for 10bf58b - Browse repository at this point
Copy the full SHA 10bf58bView commit details -
std: implement
join
for WASI-threadsWe now reset the Thread ID to 0 and wake up the main thread listening for the thread to finish. We use inline assembly as we cannot use the stack to set the thread ID as it could possibly clobber any of the memory. Currently, we leak the memory that was allocated for the thread. We need to implement a way where we can clean up the memory without using the stack (as the stack is stored inside this same memory).
Configuration menu - View commit details
-
Copy full SHA for 8346090 - Browse repository at this point
Copy the full SHA 8346090View commit details -
free allocated memory upon call
join
When `join` detects a thread has completed, it will free the allocated memory of the thread. For this we must first copy the allocator. This is required as the allocated memory holds a reference to the original allocator. If we free the memory, we would end up with UB as the allocator would free itself.
Configuration menu - View commit details
-
Copy full SHA for 622b7c4 - Browse repository at this point
Copy the full SHA 622b7c4View commit details -
std: implement
detach
for WASI-threadsWhen a thread is detached from the main thread, we automatically cleanup any allocated memory. For this we first reset the stack-pointer to the original stack-pointer of the main-thread so we can safely clear the memory which also contains the thread's stack.
Configuration menu - View commit details
-
Copy full SHA for e06ab1b - Browse repository at this point
Copy the full SHA e06ab1bView commit details -
default to single-threaded for WebAssembly
When targeting WebAssembly, we default to building a single-threaded build as threads are still experimental. The user however can enable a multi- threaded build by specifying '-fno-single-threaded'. It's a compile-error to enable this flag, but not also enable shared-memory.
Configuration menu - View commit details
-
Copy full SHA for 87b8a05 - Browse repository at this point
Copy the full SHA 87b8a05View commit details