Skip to content

[libc++] Improves _LIBCPP_HAS_NO_THREADS guards. #76624

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

Merged
merged 1 commit into from
Jan 16, 2024

Conversation

mordante
Copy link
Member

Previously the header included several headers, possibly granularized threading headers. This could lead to build errors when these headers were incompatible with threading disabled.

Now test the guard before inclusion. This matches the pattern used for no localization and no wide characters.

Fixes: #76620

Previously the header included several headers, possibly granularized
threading headers. This could lead to build errors when these headers
were incompatible with threading disabled.

Now test the guard before inclusion. This matches the pattern used for
no localization and no wide characters.

Fixes: llvm#76620
@mordante mordante requested a review from a team as a code owner December 30, 2023 18:40
@llvmbot llvmbot added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label Dec 30, 2023
@llvmbot
Copy link
Member

llvmbot commented Dec 30, 2023

@llvm/pr-subscribers-libcxx

Author: Mark de Wever (mordante)

Changes

Previously the header included several headers, possibly granularized threading headers. This could lead to build errors when these headers were incompatible with threading disabled.

Now test the guard before inclusion. This matches the pattern used for no localization and no wide characters.

Fixes: #76620


Full diff: https://github.com/llvm/llvm-project/pull/76624.diff

7 Files Affected:

  • (modified) libcxx/include/barrier (+6-5)
  • (modified) libcxx/include/future (+6-5)
  • (modified) libcxx/include/latch (+6-5)
  • (modified) libcxx/include/semaphore (+6-5)
  • (modified) libcxx/include/shared_mutex (+6-5)
  • (modified) libcxx/include/stop_token (+6-5)
  • (modified) libcxx/include/thread (+6-5)
diff --git a/libcxx/include/barrier b/libcxx/include/barrier
index fcfc96cb0484cf..9e7150fec14802 100644
--- a/libcxx/include/barrier
+++ b/libcxx/include/barrier
@@ -45,11 +45,16 @@ namespace std
 
 */
 
+#include <__config>
+
+#ifdef _LIBCPP_HAS_NO_THREADS
+#  error "<barrier> is not supported since libc++ has been configured without support for threads."
+#endif
+
 #include <__assert> // all public C++ headers provide the assertion handler
 #include <__atomic/atomic_base.h>
 #include <__atomic/memory_order.h>
 #include <__availability>
-#include <__config>
 #include <__memory/unique_ptr.h>
 #include <__thread/poll_with_backoff.h>
 #include <__thread/timed_backoff_policy.h>
@@ -63,10 +68,6 @@ namespace std
 #  pragma GCC system_header
 #endif
 
-#ifdef _LIBCPP_HAS_NO_THREADS
-#  error "<barrier> is not supported since libc++ has been configured without support for threads."
-#endif
-
 _LIBCPP_PUSH_MACROS
 #include <__undef_macros>
 
diff --git a/libcxx/include/future b/libcxx/include/future
index 92ba1882106915..5602ae41c14235 100644
--- a/libcxx/include/future
+++ b/libcxx/include/future
@@ -362,11 +362,16 @@ template <class R, class Alloc> struct uses_allocator<packaged_task<R>, Alloc>;
 
 */
 
+#include <__config>
+
+#ifdef _LIBCPP_HAS_NO_THREADS
+#  error "<future> is not supported since libc++ has been configured without support for threads."
+#endif
+
 #include <__assert> // all public C++ headers provide the assertion handler
 #include <__availability>
 #include <__chrono/duration.h>
 #include <__chrono/time_point.h>
-#include <__config>
 #include <__exception/exception_ptr.h>
 #include <__memory/addressof.h>
 #include <__memory/allocator.h>
@@ -396,10 +401,6 @@ template <class R, class Alloc> struct uses_allocator<packaged_task<R>, Alloc>;
 #  pragma GCC system_header
 #endif
 
-#ifdef _LIBCPP_HAS_NO_THREADS
-#  error "<future> is not supported since libc++ has been configured without support for threads."
-#endif
-
 _LIBCPP_BEGIN_NAMESPACE_STD
 
 // enum class future_errc
diff --git a/libcxx/include/latch b/libcxx/include/latch
index ef52c0562a7c51..742452517ddc5d 100644
--- a/libcxx/include/latch
+++ b/libcxx/include/latch
@@ -40,12 +40,17 @@ namespace std
 
 */
 
+#include <__config>
+
+#ifdef _LIBCPP_HAS_NO_THREADS
+#  error "<latch> is not supported since libc++ has been configured without support for threads."
+#endif
+
 #include <__assert> // all public C++ headers provide the assertion handler
 #include <__atomic/atomic_base.h>
 #include <__atomic/atomic_sync.h>
 #include <__atomic/memory_order.h>
 #include <__availability>
-#include <__config>
 #include <cstddef>
 #include <limits>
 #include <version>
@@ -54,10 +59,6 @@ namespace std
 #  pragma GCC system_header
 #endif
 
-#ifdef _LIBCPP_HAS_NO_THREADS
-#  error "<latch> is not supported since libc++ has been configured without support for threads."
-#endif
-
 _LIBCPP_PUSH_MACROS
 #include <__undef_macros>
 
diff --git a/libcxx/include/semaphore b/libcxx/include/semaphore
index de45b8b5db1014..ca5b2a312433cf 100644
--- a/libcxx/include/semaphore
+++ b/libcxx/include/semaphore
@@ -45,13 +45,18 @@ using binary_semaphore = counting_semaphore<1>;
 
 */
 
+#include <__config>
+
+#ifdef _LIBCPP_HAS_NO_THREADS
+#  error "<semaphore> is not supported since libc++ has been configured without support for threads."
+#endif
+
 #include <__assert> // all public C++ headers provide the assertion handler
 #include <__atomic/atomic_base.h>
 #include <__atomic/atomic_sync.h>
 #include <__atomic/memory_order.h>
 #include <__availability>
 #include <__chrono/time_point.h>
-#include <__config>
 #include <__thread/poll_with_backoff.h>
 #include <__thread/timed_backoff_policy.h>
 #include <__threading_support>
@@ -63,10 +68,6 @@ using binary_semaphore = counting_semaphore<1>;
 #  pragma GCC system_header
 #endif
 
-#ifdef _LIBCPP_HAS_NO_THREADS
-#  error "<semaphore> is not supported since libc++ has been configured without support for threads."
-#endif
-
 _LIBCPP_PUSH_MACROS
 #include <__undef_macros>
 
diff --git a/libcxx/include/shared_mutex b/libcxx/include/shared_mutex
index 1528d108d7493b..ac66b3a568bf2d 100644
--- a/libcxx/include/shared_mutex
+++ b/libcxx/include/shared_mutex
@@ -122,13 +122,18 @@ template <class Mutex>
 
 */
 
+#include <__config>
+
+#  ifdef _LIBCPP_HAS_NO_THREADS
+#    error "<shared_mutex> is not supported since libc++ has been configured without support for threads."
+#  endif
+
 #include <__assert> // all public C++ headers provide the assertion handler
 #include <__availability>
 #include <__chrono/duration.h>
 #include <__chrono/steady_clock.h>
 #include <__chrono/time_point.h>
 #include <__condition_variable/condition_variable.h>
-#include <__config>
 #include <__memory/addressof.h>
 #include <__mutex/mutex.h>
 #include <__mutex/tag_types.h>
@@ -147,10 +152,6 @@ _LIBCPP_PUSH_MACROS
 #    pragma GCC system_header
 #  endif
 
-#  ifdef _LIBCPP_HAS_NO_THREADS
-#    error "<shared_mutex> is not supported since libc++ has been configured without support for threads."
-#  endif
-
 _LIBCPP_BEGIN_NAMESPACE_STD
 
 struct _LIBCPP_EXPORTED_FROM_ABI __shared_mutex_base {
diff --git a/libcxx/include/stop_token b/libcxx/include/stop_token
index b223ceb27f0d24..66c7a6ab5996c1 100644
--- a/libcxx/include/stop_token
+++ b/libcxx/include/stop_token
@@ -31,8 +31,13 @@ namespace std {
 
 */
 
-#include <__assert> // all public C++ headers provide the assertion handler
 #include <__config>
+
+#ifdef _LIBCPP_HAS_NO_THREADS
+#  error "<stop_token> is not supported since libc++ has been configured without support for threads."
+#endif
+
+#include <__assert> // all public C++ headers provide the assertion handler
 #include <__stop_token/stop_callback.h>
 #include <__stop_token/stop_source.h>
 #include <__stop_token/stop_token.h>
@@ -42,10 +47,6 @@ namespace std {
 #  pragma GCC system_header
 #endif
 
-#ifdef _LIBCPP_HAS_NO_THREADS
-#  error "<stop_token> is not supported since libc++ has been configured without support for threads."
-#endif
-
 #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
 #  include <iosfwd>
 #endif
diff --git a/libcxx/include/thread b/libcxx/include/thread
index 1cf22bf6aaf9d4..84c80d04cf03c9 100644
--- a/libcxx/include/thread
+++ b/libcxx/include/thread
@@ -86,9 +86,14 @@ void sleep_for(const chrono::duration<Rep, Period>& rel_time);
 
 */
 
+#include <__config>
+
+#ifdef _LIBCPP_HAS_NO_THREADS
+#  error "<thread> is not supported since libc++ has been configured without support for threads."
+#endif
+
 #include <__assert> // all public C++ headers provide the assertion handler
 #include <__availability>
-#include <__config>
 #include <__thread/formatter.h>
 #include <__thread/jthread.h>
 #include <__thread/this_thread.h>
@@ -105,10 +110,6 @@ void sleep_for(const chrono::duration<Rep, Period>& rel_time);
 #  pragma GCC system_header
 #endif
 
-#ifdef _LIBCPP_HAS_NO_THREADS
-#  error "<thread> is not supported since libc++ has been configured without support for threads."
-#endif
-
 #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES)
 #  include <cstddef>
 #  include <ctime>

Copy link
Member

@ldionne ldionne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This LGTM. However, at some point I think we'll need to revisit how we handle _LIBCPP_HAS_NO_THREADS and _LIBCPP_HAS_NO_LOCALIZATION. I think we'll want to make the contents of headers empty in the case where they're not supported, like we do for e.g. _LIBCPP_HAS_NO_RANDOM_DEVICE. This will merit more discussion anyway, and doesn't impact whether this patch should land.

@mordante mordante merged commit 34933d1 into llvm:main Jan 16, 2024
@mordante mordante deleted the improve_libcpp_no_thread_guards branch January 16, 2024 18:13
justinfargnoli pushed a commit to justinfargnoli/llvm-project that referenced this pull request Jan 28, 2024
Previously the header included several headers, possibly granularized
threading headers. This could lead to build errors when these headers
were incompatible with threading disabled.

Now test the guard before inclusion. This matches the pattern used for
no localization and no wide characters.

Fixes: llvm#76620
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[libc++] Feature guards are checked too late
3 participants