Skip to content

Comments

WASIP3 Locking/Waiting Primitives#746

Open
TartanLlama wants to merge 30 commits intoWebAssembly:mainfrom
TartanLlama:sy/wasip3-locks
Open

WASIP3 Locking/Waiting Primitives#746
TartanLlama wants to merge 30 commits intoWebAssembly:mainfrom
TartanLlama:sy/wasip3-locks

Conversation

@TartanLlama
Copy link
Contributor

@TartanLlama TartanLlama commented Feb 20, 2026

Includes #744

The first batch of changes for cooperative threading. This PR introduces the core locking and waiting primitives that will be used to implement many of the pthreads features, and reimplements the existing locks used throughout the codebase in terms of them.

  • Implements sched_yield in terms of wasip3_thread_yield
  • Adds a guest-space waitlist implementation in __wait.c consisting of:
    • struct __waitlist_node: a singly-linked list of TIDs
    • __waitlist_wait_on: adds the current thread to the waitlist, allocating the node on the stack
    • __waitlist_wake_(one,all): yields directly to one thread in the waitlist and unsuspends the rest
  • Adds a cooperative lock implementation implemented in terms of the waitlist to __lock.c
    • Possible re-entrance of libc function is somewhat complicated by cooperative threading, because there are three relevant classes of sections of code:
      • Sections that may be executed concurrently
      • Sections that may not be executed concurrently, but that contain no context-switch points (yields, import calls, indirect calls)
      • Sections that may not be executed concurrently, and that do contain context-switch points
    • To reflect this and avoid unnecessary locking/unlocking for sections in the second class, this PR adds a WEAK_(UN)LOCK macro in addition to the existing (UN)LOCK, where the former is a no-op in co-operative threading builds
    • I'm not married to these names, and we may decide that the added complexity is not worth it, but I've left this in for now
  • Existing uses of futexes are rewritten to use the cooperative lock when building for WASIP3

Because these patches do not yet include the ability to spawn new threads, which will require llvm/llvm-project#175800, there is no additional testing added yet. Testing will be added as part of integration of the Open POSIX Test Suite, which will be coming in future PRs.

@TartanLlama
Copy link
Contributor Author

Needs a new release of wasm-component-ld to pull in wit-component changes for intrinsics, PR: bytecodealliance/wasm-component-ld#91

@TartanLlama
Copy link
Contributor Author

TartanLlama commented Feb 21, 2026

Strange, these test failures don't repro for me locally on an M3 Mac

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant