From 0eb8f018a1c8ba1bb22fcc1d070a7a93cda6df1a Mon Sep 17 00:00:00 2001 From: Alexey Samsonov Date: Wed, 25 Mar 2026 10:51:31 -0700 Subject: [PATCH] [libc++] Add CMake plumbing for _LIBCPP_HAS_THREAD_API_C11 mode. (#188414) Introduce CMake option `LIBCXX_HAS_C11_THREAD_API` that explicitly instructs libc++ to use C11 threads. Make sure `_LIBCPP_HAS_THREAD_API_C11` is defined properly in the `<__config_site>` and interacts correctly with platform-based auto-selection of thread API when it's not explicitly set. --- libcxx/CMakeLists.txt | 24 ++++++++++++++++++++++++ libcxx/include/__config | 7 ++++++- libcxx/include/__config_site.in | 2 +- 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/libcxx/CMakeLists.txt b/libcxx/CMakeLists.txt index e1620f91a453..f54c20370c18 100644 --- a/libcxx/CMakeLists.txt +++ b/libcxx/CMakeLists.txt @@ -310,6 +310,7 @@ option(LIBCXX_HAS_WIN32_THREAD_API "Ignore auto-detection and force use of win32 option(LIBCXX_HAS_EXTERNAL_THREAD_API "Build libc++ with an externalized threading API. This option may only be set to ON when LIBCXX_ENABLE_THREADS=ON." OFF) +option(LIBCXX_HAS_C11_THREAD_API "Ignore auto-detection and force use of C11 thread API" OFF) if (LIBCXX_ENABLE_THREADS) set(LIBCXX_PSTL_BACKEND "std_thread" CACHE STRING "Which PSTL backend to use") @@ -354,6 +355,10 @@ if(NOT LIBCXX_ENABLE_THREADS) message(FATAL_ERROR "LIBCXX_HAS_WIN32_THREAD_API can only be set to ON" " when LIBCXX_ENABLE_THREADS is also set to ON.") endif() + if (LIBCXX_HAS_C11_THREAD_API) + message(FATAL_ERROR "LIBCXX_HAS_C11_THREAD_API can only be set to ON" + " when LIBCXX_ENABLE_THREADS is also set to ON.") + endif() endif() @@ -368,6 +373,11 @@ if (LIBCXX_HAS_EXTERNAL_THREAD_API) " and LIBCXX_HAS_WIN32_THREAD_API cannot be both" " set to ON at the same time.") endif() + if (LIBCXX_HAS_C11_THREAD_API) + message(FATAL_ERROR "The options LIBCXX_HAS_EXTERNAL_THREAD_API" + " and LIBCXX_HAS_C11_THREAD_API cannot be both" + " set to ON at the same time.") + endif() endif() if (LIBCXX_HAS_PTHREAD_API) @@ -376,6 +386,19 @@ if (LIBCXX_HAS_PTHREAD_API) " and LIBCXX_HAS_WIN32_THREAD_API cannot be both" " set to ON at the same time.") endif() + if (LIBCXX_HAS_C11_THREAD_API) + message(FATAL_ERROR "The options LIBCXX_HAS_PTHREAD_API" + " and LIBCXX_HAS_C11_THREAD_API cannot be both" + " set to ON at the same time.") + endif() +endif() + +if (LIBCXX_HAS_WIN32_THREAD_API) + if (LIBCXX_HAS_C11_THREAD_API) + message(FATAL_ERROR "The options LIBCXX_HAS_WIN32_THREAD_API" + " and LIBCXX_HAS_C11_THREAD_API cannot be both" + " set to ON at the same time.") + endif() endif() if (NOT LIBCXX_ENABLE_RTTI AND LIBCXX_ENABLE_EXCEPTIONS) @@ -762,6 +785,7 @@ endif() config_define(${LIBCXX_HAS_PTHREAD_API} _LIBCPP_HAS_THREAD_API_PTHREAD) config_define(${LIBCXX_HAS_EXTERNAL_THREAD_API} _LIBCPP_HAS_THREAD_API_EXTERNAL) config_define(${LIBCXX_HAS_WIN32_THREAD_API} _LIBCPP_HAS_THREAD_API_WIN32) +config_define(${LIBCXX_HAS_C11_THREAD_API} _LIBCPP_HAS_THREAD_API_C11) config_define(${LIBCXX_HAS_MUSL_LIBC} _LIBCPP_HAS_MUSL_LIBC) config_define_if(LIBCXX_NO_VCRUNTIME _LIBCPP_NO_VCRUNTIME) config_define(${LIBCXX_ENABLE_FILESYSTEM} _LIBCPP_HAS_FILESYSTEM) diff --git a/libcxx/include/__config b/libcxx/include/__config index 9cb98bbb5934..942b0a9dbefa 100644 --- a/libcxx/include/__config +++ b/libcxx/include/__config @@ -370,7 +370,8 @@ typedef __char32_t char32_t; # if _LIBCPP_HAS_THREADS && \ !_LIBCPP_HAS_THREAD_API_PTHREAD && \ !_LIBCPP_HAS_THREAD_API_WIN32 && \ - !_LIBCPP_HAS_THREAD_API_EXTERNAL + !_LIBCPP_HAS_THREAD_API_EXTERNAL && \ + !_LIBCPP_HAS_THREAD_API_C11 # if defined(__FreeBSD__) || \ defined(__wasi__) || \ @@ -414,6 +415,10 @@ typedef __char32_t char32_t; # error _LIBCPP_HAS_THREAD_API_EXTERNAL may only be true when _LIBCPP_HAS_THREADS is true. # endif +# if !_LIBCPP_HAS_THREADS && _LIBCPP_HAS_THREAD_API_C11 +# error _LIBCPP_HAS_THREAD_API_C11 may only be true when _LIBCPP_HAS_THREADS is true. +# endif + # if !_LIBCPP_HAS_MONOTONIC_CLOCK && _LIBCPP_HAS_THREADS # error _LIBCPP_HAS_MONOTONIC_CLOCK may only be false when _LIBCPP_HAS_THREADS is false. # endif diff --git a/libcxx/include/__config_site.in b/libcxx/include/__config_site.in index eec6cb720ba5..e6e33a2eeb2a 100644 --- a/libcxx/include/__config_site.in +++ b/libcxx/include/__config_site.in @@ -19,7 +19,7 @@ #cmakedefine01 _LIBCPP_HAS_THREAD_API_PTHREAD #cmakedefine01 _LIBCPP_HAS_THREAD_API_EXTERNAL #cmakedefine01 _LIBCPP_HAS_THREAD_API_WIN32 -#define _LIBCPP_HAS_THREAD_API_C11 0 // FIXME: Is this guarding dead code? +#cmakedefine01 _LIBCPP_HAS_THREAD_API_C11 #cmakedefine _LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS #cmakedefine01 _LIBCPP_HAS_VENDOR_AVAILABILITY_ANNOTATIONS #cmakedefine _LIBCPP_NO_VCRUNTIME