Skip to content
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

Merged
merged 9 commits into from
Jun 27, 2023

Commits on Jun 26, 2023

  1. wasm-ld: implement --export-memory flag

    This 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.
    Luukdegram committed Jun 26, 2023
    Configuration menu
    Copy the full SHA
    3819371 View commit details
    Browse the repository at this point in the history
  2. 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.
    Luukdegram committed Jun 26, 2023
    Configuration menu
    Copy the full SHA
    062eb6f View commit details
    Browse the repository at this point in the history
  3. std: implement Futex for WebAssembly

    Implements 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.
    Luukdegram committed Jun 26, 2023
    Configuration menu
    Copy the full SHA
    ea0d4c8 View commit details
    Browse the repository at this point in the history
  4. std: implement Thread spawn for WASI

    This 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.
    Luukdegram committed Jun 26, 2023
    Configuration menu
    Copy the full SHA
    a97dbdf View commit details
    Browse the repository at this point in the history
  5. 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.
    Luukdegram committed Jun 26, 2023
    Configuration menu
    Copy the full SHA
    10bf58b View commit details
    Browse the repository at this point in the history
  6. std: implement join for WASI-threads

    We 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).
    Luukdegram committed Jun 26, 2023
    Configuration menu
    Copy the full SHA
    8346090 View commit details
    Browse the repository at this point in the history
  7. 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.
    Luukdegram committed Jun 26, 2023
    Configuration menu
    Copy the full SHA
    622b7c4 View commit details
    Browse the repository at this point in the history
  8. std: implement detach for WASI-threads

    When 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.
    Luukdegram committed Jun 26, 2023
    Configuration menu
    Copy the full SHA
    e06ab1b View commit details
    Browse the repository at this point in the history
  9. 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.
    Luukdegram committed Jun 26, 2023
    Configuration menu
    Copy the full SHA
    87b8a05 View commit details
    Browse the repository at this point in the history