Skip to content

Commit d25afce

Browse files
committed
Enable pthreads in libc++ for THREAD_MODEL=single.
This commit enables thread support in libc++ for all thread models, enabling C++ applications that use threading APIs like `<atomic>` but do not spawn threads (e.g. Clang) to be built with minimal changes. Fixes #546.
1 parent 36e12fd commit d25afce

3 files changed

Lines changed: 14 additions & 3 deletions

File tree

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,10 +214,21 @@ See [C setjmp/longjmp support] about setjmp/longjmp support.
214214
[C setjmp/longjmp support]: SetjmpLongjmp.md
215215

216216
This repository experimentally supports __threads__ with
217-
`--target=wasm32-wasi-threads`. It uses WebAssembly's [threads] primitives
217+
`--target=wasm32-wasip1-threads`. It uses WebAssembly's [threads] primitives
218218
(atomics, `wait`/`notify`, shared memory) and [wasi-threads] for spawning
219219
threads. Note: this is experimental &mdash; do not expect long-term stability!
220220

221+
Note that the `pthread_*` family of functions, as well as C++ threading primitives
222+
such as `<atomic>`, `<mutex>`, and `<thread>` are available on all targets.
223+
Any attempt to spawn a thread will fail on `--target=wasm32-wasip1` or
224+
`--target=wasm32-wasip2`, but other functionality, such as locks, still works.
225+
This makes it easier to port C++ codebases, as only a fraction of code needs
226+
to be modified to build for the single-threaded targets.
227+
228+
Defining a macro `_WASI_STRICT_PTHREAD` will make `pthread_create`,
229+
`pthread_detach`, `pthread_join`, `pthread_tryjoin_np`, and `pthread_timedjoin_np`
230+
fail with a compile time error when building for single-threaded targets.
231+
221232
[threads]: https://github.com/WebAssembly/threads
222233
[wasi-threads]: https://github.com/WebAssembly/wasi-threads
223234

cmake/wasi-sdk-sysroot.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ function(define_libcxx_sub target target_suffix extra_target_flags extra_libdir_
226226
set(pic OFF)
227227
set(target_flags -pthread)
228228
else()
229-
set(threads OFF)
229+
set(threads ON) # uses the thread-stub implementation
230230
set(pic ON)
231231
set(target_flags "")
232232
endif()

src/wasi-libc

Submodule wasi-libc updated 46 files

0 commit comments

Comments
 (0)