Skip to content

Commit dadbd94

Browse files
authored
Enable pthreads in libc++ for THREAD_MODEL=single (#548)
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. Depends on WebAssembly/wasi-libc#602.
1 parent 36e12fd commit dadbd94

File tree

3 files changed

+17
-8
lines changed

3 files changed

+17
-8
lines changed

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: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -222,11 +222,9 @@ execute_process(
222222

223223
function(define_libcxx_sub target target_suffix extra_target_flags extra_libdir_suffix)
224224
if(${target} MATCHES threads)
225-
set(threads ON)
226225
set(pic OFF)
227226
set(target_flags -pthread)
228227
else()
229-
set(threads OFF)
230228
set(pic ON)
231229
set(target_flags "")
232230
endif()
@@ -262,8 +260,8 @@ function(define_libcxx_sub target target_suffix extra_target_flags extra_libdir_
262260
-DCMAKE_STAGING_PREFIX=${wasi_sysroot}
263261
-DCMAKE_POSITION_INDEPENDENT_CODE=${pic}
264262
-DCXX_SUPPORTS_CXX11=ON
265-
-DLIBCXX_ENABLE_THREADS:BOOL=${threads}
266-
-DLIBCXX_HAS_PTHREAD_API:BOOL=${threads}
263+
-DLIBCXX_ENABLE_THREADS:BOOL=ON
264+
-DLIBCXX_HAS_PTHREAD_API:BOOL=ON
267265
-DLIBCXX_HAS_EXTERNAL_THREAD_API:BOOL=OFF
268266
-DLIBCXX_BUILD_EXTERNAL_THREAD_LIBRARY:BOOL=OFF
269267
-DLIBCXX_HAS_WIN32_THREAD_API:BOOL=OFF
@@ -280,8 +278,8 @@ function(define_libcxx_sub target target_suffix extra_target_flags extra_libdir_
280278
-DLIBCXXABI_ENABLE_EXCEPTIONS:BOOL=OFF
281279
-DLIBCXXABI_ENABLE_SHARED:BOOL=${pic}
282280
-DLIBCXXABI_SILENT_TERMINATE:BOOL=ON
283-
-DLIBCXXABI_ENABLE_THREADS:BOOL=${threads}
284-
-DLIBCXXABI_HAS_PTHREAD_API:BOOL=${threads}
281+
-DLIBCXXABI_ENABLE_THREADS:BOOL=ON
282+
-DLIBCXXABI_HAS_PTHREAD_API:BOOL=ON
285283
-DLIBCXXABI_HAS_EXTERNAL_THREAD_API:BOOL=OFF
286284
-DLIBCXXABI_BUILD_EXTERNAL_THREAD_LIBRARY:BOOL=OFF
287285
-DLIBCXXABI_HAS_WIN32_THREAD_API:BOOL=OFF

src/wasi-libc

Submodule wasi-libc updated 52 files

0 commit comments

Comments
 (0)