From: Jonathan Wakely Date: Thu, 26 Nov 2020 22:23:11 +0000 (+0000) Subject: libstdc++: Define (and use) _GLIBCXX_HAVE_ATOMIC_WAIT X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=61c71a62453e4f2def5d94bdb2b293fcec463c14;p=gcc.git libstdc++: Define (and use) _GLIBCXX_HAVE_ATOMIC_WAIT In order to simplify the preprocessor checks for whether __atomic_wait is available, this commit does: -#if defined _GLIBCXX_HAS_GTHREADS || _GLIBCXX_HAVE_LINUX_FUTEX +#ifdef _GLIBCXX_HAVE_ATOMIC_WAIT The original was wrong anyway, as it should have used 'defined' to check _GLIBCXX_HAVE_LINUX_FUTEX (for consistency with how that's used elsewhere). The new macro is defined in when the file is defines __atomic_wait and related facilities. All other code that depends on those features can just check the one macro. libstdc++-v3/ChangeLog: * include/bits/atomic_wait.h (_GLIBCXX_HAVE_ATOMIC_WAIT): Define. * include/bits/atomic_base.h: Check _GLIBCXX_HAVE_ATOMIC_WAIT. * include/bits/atomic_timed_wait.h: Likewise. * include/bits/semaphore_base.h: Likewise. * include/std/atomic: Likewise. * include/std/latch: Likewise. * include/std/semaphore: Likewise. --- diff --git a/libstdc++-v3/include/bits/atomic_base.h b/libstdc++-v3/include/bits/atomic_base.h index d7db8612889..d0d962d3047 100644 --- a/libstdc++-v3/include/bits/atomic_base.h +++ b/libstdc++-v3/include/bits/atomic_base.h @@ -230,7 +230,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION return __v == __GCC_ATOMIC_TEST_AND_SET_TRUEVAL; } -#if defined _GLIBCXX_HAS_GTHREADS || _GLIBCXX_HAVE_LINUX_FUTEX +#ifdef _GLIBCXX_HAVE_ATOMIC_WAIT _GLIBCXX_ALWAYS_INLINE void wait(bool __old, memory_order __m = memory_order_seq_cst) const noexcept @@ -253,7 +253,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { std::__atomic_notify(&_M_i, true); } // TODO add const volatile overload -#endif // GTHREADS || LINUX_FUTEX +#endif // HAVE_ATOMIC_WAIT #endif // C++20 _GLIBCXX_ALWAYS_INLINE void @@ -604,8 +604,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION __cmpexch_failure_order(__m)); } -#if __cplusplus > 201703L -#if defined _GLIBCXX_HAS_GTHREADS || _GLIBCXX_HAVE_LINUX_FUTEX +#if __cplusplus > 201703L && defined _GLIBCXX_HAVE_ATOMIC_WAIT _GLIBCXX_ALWAYS_INLINE void wait(__int_type __old, memory_order __m = memory_order_seq_cst) const noexcept @@ -628,8 +627,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { std::__atomic_notify(&_M_i, true); } // TODO add const volatile overload -#endif // GTHREADS || LINUX_FUTEX -#endif // C++2a +#endif // C++20 && HAVE_ATOMIC_WAIT _GLIBCXX_ALWAYS_INLINE __int_type fetch_add(__int_type __i, @@ -900,8 +898,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION int(__m1), int(__m2)); } -#if __cplusplus > 201703L -#if defined _GLIBCXX_HAS_GTHREADS || _GLIBCXX_HAVE_LINUX_FUTEX +#if __cplusplus > 201703L && defined _GLIBCXX_HAVE_ATOMIC_WAIT _GLIBCXX_ALWAYS_INLINE void wait(__pointer_type __old, memory_order __m = memory_order_seq_cst) noexcept @@ -924,8 +921,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { std::__atomic_notify(&_M_p, true); } // TODO add const volatile overload -#endif // GTHREADS || LINUX_FUTEX -#endif // C++2a +#endif // C++20 && HAVE_ATOMIC_WAIT _GLIBCXX_ALWAYS_INLINE __pointer_type fetch_add(ptrdiff_t __d, @@ -1015,8 +1011,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION int(__success), int(__failure)); } -#if __cplusplus > 201703L -#if defined _GLIBCXX_HAS_GTHREADS || _GLIBCXX_HAVE_LINUX_FUTEX +#if __cplusplus > 201703L && defined _GLIBCXX_HAVE_ATOMIC_WAIT template _GLIBCXX_ALWAYS_INLINE void wait(const _Tp* __ptr, _Val<_Tp> __old, @@ -1041,8 +1036,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { std::__atomic_notify(__ptr, true); } // TODO add const volatile overload -#endif // GTHREADS || LINUX_FUTEX -#endif // C++2a +#endif // C++20 && HAVE_ATOMIC_WAIT template _GLIBCXX_ALWAYS_INLINE _Tp @@ -1297,7 +1291,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION __cmpexch_failure_order(__order)); } -#if defined _GLIBCXX_HAS_GTHREADS || _GLIBCXX_HAVE_LINUX_FUTEX +#ifdef _GLIBCXX_HAVE_ATOMIC_WAIT _GLIBCXX_ALWAYS_INLINE void wait(_Fp __old, memory_order __m = memory_order_seq_cst) const noexcept { __atomic_impl::wait(&_M_fp, __old, __m); } @@ -1315,7 +1309,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { __atomic_impl::notify_all(&_M_fp); } // TODO add const volatile overload -#endif // GTHREADS || LINUX_FUTEX +#endif // HAVE_ATOMIC_WAIT value_type fetch_add(value_type __i, @@ -1454,7 +1448,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION __cmpexch_failure_order(__order)); } -#if defined _GLIBCXX_HAS_GTHREADS || _GLIBCXX_HAVE_LINUX_FUTEX +#ifdef _GLIBCXX_HAVE_ATOMIC_WAIT _GLIBCXX_ALWAYS_INLINE void wait(_Tp __old, memory_order __m = memory_order_seq_cst) const noexcept { __atomic_impl::wait(_M_ptr, __old, __m); } @@ -1472,7 +1466,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { __atomic_impl::notify_all(_M_ptr); } // TODO add const volatile overload -#endif // GTHREADS || LINUX_FUTEX +#endif // HAVE_ATOMIC_WAIT private: _Tp* _M_ptr; @@ -1569,7 +1563,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION __cmpexch_failure_order(__order)); } -#if defined _GLIBCXX_HAS_GTHREADS || _GLIBCXX_HAVE_LINUX_FUTEX +#ifdef _GLIBCXX_HAVE_ATOMIC_WAIT _GLIBCXX_ALWAYS_INLINE void wait(_Tp __old, memory_order __m = memory_order_seq_cst) const noexcept { __atomic_impl::wait(_M_ptr, __old, __m); } @@ -1587,7 +1581,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { __atomic_impl::notify_all(_M_ptr); } // TODO add const volatile overload -#endif // GTHREADS || LINUX_FUTEX +#endif // HAVE_ATOMIC_WAIT value_type fetch_add(value_type __i, @@ -1744,7 +1738,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION __cmpexch_failure_order(__order)); } -#if defined _GLIBCXX_HAS_GTHREADS || _GLIBCXX_HAVE_LINUX_FUTEX +#ifdef _GLIBCXX_HAVE_ATOMIC_WAIT _GLIBCXX_ALWAYS_INLINE void wait(_Fp __old, memory_order __m = memory_order_seq_cst) const noexcept { __atomic_impl::wait(_M_ptr, __old, __m); } @@ -1762,7 +1756,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { __atomic_impl::notify_all(_M_ptr); } // TODO add const volatile overload -#endif // GTHREADS || LINUX_FUTEX +#endif // HAVE_ATOMIC_WAIT value_type fetch_add(value_type __i, @@ -1873,7 +1867,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION __cmpexch_failure_order(__order)); } -#if defined _GLIBCXX_HAS_GTHREADS || _GLIBCXX_HAVE_LINUX_FUTEX +#ifdef _GLIBCXX_HAVE_ATOMIC_WAIT _GLIBCXX_ALWAYS_INLINE void wait(_Tp __old, memory_order __m = memory_order_seq_cst) const noexcept { __atomic_impl::wait(_M_ptr, __old, __m); } @@ -1891,7 +1885,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { __atomic_impl::notify_all(_M_ptr); } // TODO add const volatile overload -#endif // GTHREADS || LINUX_FUTEX +#endif // HAVE_ATOMIC_WAIT _GLIBCXX_ALWAYS_INLINE value_type fetch_add(difference_type __d, diff --git a/libstdc++-v3/include/bits/atomic_timed_wait.h b/libstdc++-v3/include/bits/atomic_timed_wait.h index 1c91c858ce7..f3a07a36722 100644 --- a/libstdc++-v3/include/bits/atomic_timed_wait.h +++ b/libstdc++-v3/include/bits/atomic_timed_wait.h @@ -32,10 +32,9 @@ #pragma GCC system_header -#include -#if defined _GLIBCXX_HAS_GTHREADS || _GLIBCXX_HAVE_LINUX_FUTEX -#include #include +#ifdef _GLIBCXX_HAVE_ATOMIC_WAIT +#include #include @@ -293,5 +292,5 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION } _GLIBCXX_END_NAMESPACE_VERSION } // namespace std -#endif // GTHREADS || LINUX_FUTEX +#endif // HAVE_ATOMIC_WAIT #endif // _GLIBCXX_ATOMIC_TIMED_WAIT_H diff --git a/libstdc++-v3/include/bits/atomic_wait.h b/libstdc++-v3/include/bits/atomic_wait.h index 3aaaa9aaec3..a59ed8cb418 100644 --- a/libstdc++-v3/include/bits/atomic_wait.h +++ b/libstdc++-v3/include/bits/atomic_wait.h @@ -33,7 +33,7 @@ #pragma GCC system_header #include -#if defined _GLIBCXX_HAS_GTHREADS || _GLIBCXX_HAVE_LINUX_FUTEX +#if defined _GLIBCXX_HAS_GTHREADS || defined _GLIBCXX_HAVE_LINUX_FUTEX #include #include #include @@ -50,6 +50,8 @@ # include // std::mutex, std::__condvar #endif +// Other headers use this to check for the facilities defined in this header. +#define _GLIBCXX_HAVE_ATOMIC_WAIT 1 namespace std _GLIBCXX_VISIBILITY(default) { diff --git a/libstdc++-v3/include/bits/semaphore_base.h b/libstdc++-v3/include/bits/semaphore_base.h index 56333bbbfef..f2984236e03 100644 --- a/libstdc++-v3/include/bits/semaphore_base.h +++ b/libstdc++-v3/include/bits/semaphore_base.h @@ -32,9 +32,8 @@ #pragma GCC system_header -#include -#if defined _GLIBCXX_HAS_GTHREADS || _GLIBCXX_HAVE_LINUX_FUTEX #include +#ifdef _GLIBCXX_HAVE_ATOMIC_WAIT #include #include @@ -298,5 +297,5 @@ template _GLIBCXX_END_NAMESPACE_VERSION } // namespace std -#endif // GTHREADS || LINUX_FUTEX +#endif // HAVE_ATOMIC_WAIT #endif // _GLIBCXX_SEMAPHORE_BASE_H diff --git a/libstdc++-v3/include/std/atomic b/libstdc++-v3/include/std/atomic index fe4de244f85..664af023d77 100644 --- a/libstdc++-v3/include/std/atomic +++ b/libstdc++-v3/include/std/atomic @@ -164,21 +164,21 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION memory_order __m = memory_order_seq_cst) volatile noexcept { return _M_base.compare_exchange_strong(__i1, __i2, __m); } -#if __cplusplus > 201703L -#if defined _GLIBCXX_HAS_GTHREADS || _GLIBCXX_HAVE_LINUX_FUTEX - void wait(bool __old, memory_order __m = memory_order_seq_cst) const - noexcept +#if __cplusplus > 201703L && defined _GLIBCXX_HAVE_ATOMIC_WAIT + void + wait(bool __old, memory_order __m = memory_order_seq_cst) const noexcept { _M_base.wait(__old, __m); } // TODO add const volatile overload - void notify_one() const noexcept + void + notify_one() const noexcept { _M_base.notify_one(); } - void notify_all() const noexcept + void + notify_all() const noexcept { _M_base.notify_all(); } -#endif // GTHREADS || LINUX_FUTEX -#endif +#endif // C++20 && HAVE_ATOMIC_WAIT }; #if __cplusplus <= 201703L @@ -380,9 +380,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { return compare_exchange_strong(__e, __i, __m, __cmpexch_failure_order(__m)); } -#if __cplusplus > 201703L -#if defined _GLIBCXX_HAS_GTHREADS || _GLIBCXX_HAVE_LINUX_FUTEX - void wait(_Tp __old, memory_order __m = memory_order_seq_cst) const noexcept +#if __cplusplus > 201703L && defined _GLIBCXX_HAVE_ATOMIC_WAIT + void + wait(_Tp __old, memory_order __m = memory_order_seq_cst) const noexcept { std::__atomic_wait(&_M_i, __old, [__m, this, __old] @@ -397,13 +397,14 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION // TODO add const volatile overload - void notify_one() const noexcept + void + notify_one() const noexcept { std::__atomic_notify(&_M_i, false); } - void notify_all() const noexcept + void + notify_all() const noexcept { std::__atomic_notify(&_M_i, true); } -#endif // GTHREADS || LINUX_FUTEX -#endif // C++20 +#endif // C++20 && HAVE_ATOMIC_WAIT }; #undef _GLIBCXX20_INIT @@ -643,21 +644,21 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION __cmpexch_failure_order(__m)); } -#if __cplusplus > 201703L -#if defined _GLIBCXX_HAS_GTHREADS || _GLIBCXX_HAVE_LINUX_FUTEX - void wait(__pointer_type __old, - memory_order __m = memory_order_seq_cst) noexcept +#if __cplusplus > 201703L && _GLIBCXX_HAVE_ATOMIC_WAIT + void + wait(__pointer_type __old, memory_order __m = memory_order_seq_cst) noexcept { _M_b.wait(__old, __m); } // TODO add const volatile overload - void notify_one() const noexcept + void + notify_one() const noexcept { _M_b.notify_one(); } - void notify_all() const noexcept + void + notify_all() const noexcept { _M_b.notify_all(); } -#endif // GTHREADS || LINUX_FUTEX -#endif +#endif // C++20 && HAVE_ATOMIC_WAIT __pointer_type fetch_add(ptrdiff_t __d, memory_order __m = memory_order_seq_cst) noexcept @@ -1411,8 +1412,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION } -#if __cplusplus > 201703L -#if defined _GLIBCXX_HAS_GTHREADS || _GLIBCXX_HAVE_LINUX_FUTEX +#if __cplusplus > 201703L && _GLIBCXX_HAVE_ATOMIC_WAIT template inline void atomic_wait(const atomic<_Tp>* __a, @@ -1435,9 +1435,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION inline void atomic_notify_all(atomic<_Tp>* __a) noexcept { __a->notify_all(); } - -#endif // GTHREADS || LINUX_FUTEX -#endif // C++2a +#endif // C++20 && HAVE_ATOMIC_WAIT // Function templates for atomic_integral and atomic_pointer operations only. // Some operations (and, or, xor) are only available for atomic integrals, diff --git a/libstdc++-v3/include/std/latch b/libstdc++-v3/include/std/latch index 3af295b4eb4..2684b72288f 100644 --- a/libstdc++-v3/include/std/latch +++ b/libstdc++-v3/include/std/latch @@ -36,7 +36,7 @@ #include #include -#if defined _GLIBCXX_HAS_GTHREADS || _GLIBCXX_HAVE_LINUX_FUTEX +#ifdef _GLIBCXX_HAVE_ATOMIC_WAIT namespace std _GLIBCXX_VISIBILITY(default) { _GLIBCXX_BEGIN_NAMESPACE_VERSION @@ -89,6 +89,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION }; _GLIBCXX_END_NAMESPACE_VERSION } // namespace -#endif // GTHREADS || LINUX_FUTEX +#endif // HAVE_ATOMIC_WAIT #endif // __cplusplus > 201703L #endif // _GLIBCXX_LATCH diff --git a/libstdc++-v3/include/std/semaphore b/libstdc++-v3/include/std/semaphore index f4b83ab6ae3..184c7c2867c 100644 --- a/libstdc++-v3/include/std/semaphore +++ b/libstdc++-v3/include/std/semaphore @@ -27,13 +27,13 @@ */ #ifndef _GLIBCXX_SEMAPHORE -#define _GLIBCXX_SEMAPHORE +#define _GLIBCXX_SEMAPHORE 1 #pragma GCC system_header #if __cplusplus > 201703L #include -#if defined _GLIBCXX_HAS_GTHREADS || _GLIBCXX_HAVE_LINUX_FUTEX +#ifdef _GLIBCXX_HAVE_ATOMIC_WAIT #include namespace std _GLIBCXX_VISIBILITY(default) @@ -90,6 +90,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION using binary_semaphore = std::counting_semaphore<1>; _GLIBCXX_END_NAMESPACE_VERSION } // namespace -#endif // GTHREADS || LINUX_FUTEX +#endif // HAVE_ATOMIC_WAIT #endif // C++20 #endif // _GLIBCXX_SEMAPHORE