[libcxx-commits] [libcxx] [libc++] refactor `cxx_atomic_wait` to make it reusable for atomic_ref (PR #81427)
via libcxx-commits
libcxx-commits at lists.llvm.org
Fri Feb 16 11:58:55 PST 2024
https://github.com/huixie90 updated https://github.com/llvm/llvm-project/pull/81427
>From 6a4f73d6a144c7e3649c0efd3f4e0a68552fb1b8 Mon Sep 17 00:00:00 2001
From: Hui <hui.xie0621 at gmail.com>
Date: Sun, 11 Feb 2024 19:11:28 +0000
Subject: [PATCH 1/6] [libc++] refactor `cxx_atomic_wait` to make it reusable
for atomic_ref
---
libcxx/include/__atomic/atomic_base.h | 36 ++++--
libcxx/include/__atomic/atomic_flag.h | 25 +++-
libcxx/include/__atomic/atomic_sync.h | 173 +++++++++++++++++++-------
libcxx/include/latch | 11 +-
libcxx/include/semaphore | 13 +-
5 files changed, 190 insertions(+), 68 deletions(-)
diff --git a/libcxx/include/__atomic/atomic_base.h b/libcxx/include/__atomic/atomic_base.h
index 3ad3b562c59805..b7eebcb7651aea 100644
--- a/libcxx/include/__atomic/atomic_base.h
+++ b/libcxx/include/__atomic/atomic_base.h
@@ -102,26 +102,42 @@ struct __atomic_base // false
return std::__cxx_atomic_compare_exchange_strong(std::addressof(__a_), std::addressof(__e), __d, __m, __m);
}
+ friend _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI _Tp
+ __tag_invoke(__atomic_load_cpo, const __atomic_base& __this, memory_order __order) {
+ return __this.load(__order);
+ }
+
+ friend _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI _Tp
+ __tag_invoke(__atomic_load_cpo, const volatile __atomic_base& __this, memory_order __order) {
+ return __this.load(__order);
+ }
+
+ friend _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI __cxx_atomic_impl<_Tp> const*
+ __tag_invoke(__atomic_contention_address_cpo, const __atomic_base& __this) {
+ return std::addressof(__this.__a_);
+ }
+
+ friend _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI __cxx_atomic_impl<_Tp> const volatile*
+ __tag_invoke(__atomic_contention_address_cpo, const volatile __atomic_base& __this) {
+ return std::addressof(__this.__a_);
+ }
+
_LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void wait(_Tp __v, memory_order __m = memory_order_seq_cst) const
volatile _NOEXCEPT {
- std::__cxx_atomic_wait(std::addressof(__a_), __v, __m);
+ std::__atomic_wait(*this, __v, __m);
}
_LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void
wait(_Tp __v, memory_order __m = memory_order_seq_cst) const _NOEXCEPT {
- std::__cxx_atomic_wait(std::addressof(__a_), __v, __m);
+ std::__atomic_wait(*this, __v, __m);
}
_LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void notify_one() volatile _NOEXCEPT {
- std::__cxx_atomic_notify_one(std::addressof(__a_));
- }
- _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void notify_one() _NOEXCEPT {
- std::__cxx_atomic_notify_one(std::addressof(__a_));
+ std::__atomic_notify_one(*this);
}
+ _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void notify_one() _NOEXCEPT { std::__atomic_notify_one(*this); }
_LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void notify_all() volatile _NOEXCEPT {
- std::__cxx_atomic_notify_all(std::addressof(__a_));
- }
- _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void notify_all() _NOEXCEPT {
- std::__cxx_atomic_notify_all(std::addressof(__a_));
+ std::__atomic_notify_all(*this);
}
+ _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void notify_all() _NOEXCEPT { std::__atomic_notify_all(*this); }
#if _LIBCPP_STD_VER >= 20
_LIBCPP_HIDE_FROM_ABI constexpr __atomic_base() noexcept(is_nothrow_default_constructible_v<_Tp>) : __a_(_Tp()) {}
diff --git a/libcxx/include/__atomic/atomic_flag.h b/libcxx/include/__atomic/atomic_flag.h
index a45a7183547726..f66caf7d1f5217 100644
--- a/libcxx/include/__atomic/atomic_flag.h
+++ b/libcxx/include/__atomic/atomic_flag.h
@@ -15,6 +15,7 @@
#include <__atomic/memory_order.h>
#include <__chrono/duration.h>
#include <__config>
+#include <__memory/addressof.h>
#include <__thread/support.h>
#include <cstdint>
@@ -47,13 +48,33 @@ struct atomic_flag {
__cxx_atomic_store(&__a_, _LIBCPP_ATOMIC_FLAG_TYPE(false), __m);
}
+ friend _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI _LIBCPP_ATOMIC_FLAG_TYPE
+ __tag_invoke(__atomic_load_cpo, const atomic_flag& __this, memory_order __order) {
+ return std::__cxx_atomic_load(&__this.__a_, __order);
+ }
+
+ friend _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI _LIBCPP_ATOMIC_FLAG_TYPE
+ __tag_invoke(__atomic_load_cpo, const volatile atomic_flag& __this, memory_order __order) {
+ return std::__cxx_atomic_load(&__this.__a_, __order);
+ }
+
+ friend _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI __cxx_atomic_impl<_LIBCPP_ATOMIC_FLAG_TYPE> const*
+ __tag_invoke(__atomic_contention_address_cpo, const atomic_flag& __this) {
+ return std::addressof(__this.__a_);
+ }
+
+ friend _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI __cxx_atomic_impl<_LIBCPP_ATOMIC_FLAG_TYPE> const volatile*
+ __tag_invoke(__atomic_contention_address_cpo, const volatile atomic_flag& __this) {
+ return std::addressof(__this.__a_);
+ }
+
_LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void wait(bool __v, memory_order __m = memory_order_seq_cst) const
volatile _NOEXCEPT {
- __cxx_atomic_wait(&__a_, _LIBCPP_ATOMIC_FLAG_TYPE(__v), __m);
+ std::__atomic_wait(*this, _LIBCPP_ATOMIC_FLAG_TYPE(__v), __m);
}
_LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void
wait(bool __v, memory_order __m = memory_order_seq_cst) const _NOEXCEPT {
- __cxx_atomic_wait(&__a_, _LIBCPP_ATOMIC_FLAG_TYPE(__v), __m);
+ std::__atomic_wait(*this, _LIBCPP_ATOMIC_FLAG_TYPE(__v), __m);
}
_LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void notify_one() volatile _NOEXCEPT {
__cxx_atomic_notify_one(&__a_);
diff --git a/libcxx/include/__atomic/atomic_sync.h b/libcxx/include/__atomic/atomic_sync.h
index e1994ddde86c19..12f70f9a936590 100644
--- a/libcxx/include/__atomic/atomic_sync.h
+++ b/libcxx/include/__atomic/atomic_sync.h
@@ -18,7 +18,10 @@
#include <__memory/addressof.h>
#include <__thread/poll_with_backoff.h>
#include <__thread/support.h>
+#include <__type_traits/conjunction.h>
#include <__type_traits/decay.h>
+#include <__type_traits/invoke.h>
+#include <__utility/declval.h>
#include <cstring>
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
@@ -27,6 +30,57 @@
_LIBCPP_BEGIN_NAMESPACE_STD
+// The customisation points to enable the following functions:
+// - __atomic_wait
+// - __atomic_wait_unless
+// - __atomic_notify_one
+// - __atomic_notify_all
+// Note that std::atomic<T>::wait was back-ported to C++03
+// there the below implementations look ugly to support C++03
+
+struct __atomic_load_cpo {
+ template <class _Tp>
+ using _Ret = decltype(__tag_invoke(
+ std::declval<__atomic_load_cpo>(), std::declval<const _Tp&>(), std::declval<memory_order>()));
+
+ template <class _Tp>
+ _Ret<_Tp> operator()(const _Tp& __t, memory_order __order) const _NOEXCEPT {
+ return __tag_invoke(*this, __t, __order);
+ }
+};
+// TODO: if we can deprecate std::atomic<T>::wait before c++17, we could add
+// inline constexpr __atomic_load_cpo __atomic_load{};
+
+struct __atomic_contention_address_cpo {
+ template <class _Tp>
+ using _Ret = decltype(__tag_invoke(std::declval<__atomic_contention_address_cpo>(), std::declval<const _Tp&>()));
+
+ template <class _Tp>
+ _Ret<_Tp> operator()(const _Tp& __t) const _NOEXCEPT {
+ return __tag_invoke(*this, __t);
+ }
+};
+// TODO: if we can deprecate std::atomic<T>::wait before c++17, we could add
+// inline constexpr __atomic_contention_address_cpo __atomic_contention_address{};
+
+template <class _Tp>
+using __atomic_waitable =
+ _And<__invokable<__atomic_load_cpo, const _Tp&, memory_order>,
+ __invokable<__atomic_contention_address_cpo, const _Tp&> >;
+
+template <class _AtomicWaitable, class _Poll>
+struct __atomic_wait_poll_impl {
+ const _AtomicWaitable& __a_;
+ _Poll __poll_;
+ memory_order __order_;
+
+ _LIBCPP_HIDE_FROM_ABI bool operator()() const {
+ __atomic_load_cpo __atomic_load = {};
+ auto __current_val = __atomic_load(__a_, __order_);
+ return __poll_(__current_val);
+ }
+};
+
#ifndef _LIBCPP_HAS_NO_THREADS
_LIBCPP_AVAILABILITY_SYNC _LIBCPP_EXPORTED_FROM_ABI void __cxx_atomic_notify_one(void const volatile*);
@@ -43,17 +97,42 @@ __libcpp_atomic_monitor(__cxx_atomic_contention_t const volatile*);
_LIBCPP_AVAILABILITY_SYNC _LIBCPP_EXPORTED_FROM_ABI void
__libcpp_atomic_wait(__cxx_atomic_contention_t const volatile*, __cxx_contention_t);
-template <class _Atp, class _BackoffTest>
-struct __libcpp_atomic_wait_backoff_impl {
- _Atp* __a_;
- _BackoffTest __backoff_test_;
+template <class _AtomicWaitable, class _Poll>
+struct __atomic_wait_backoff_impl {
+ const _AtomicWaitable& __a_;
+ _Poll __poll_;
+ memory_order __order_;
+
+ _LIBCPP_AVAILABILITY_SYNC
+ _LIBCPP_HIDE_FROM_ABI bool
+ __update_monitor_val_and_poll(__cxx_atomic_contention_t const volatile*, __cxx_contention_t& __monitor_val) const {
+ // In case the contention type happens to be __cxx_atomic_contention_t, i.e. __cxx_atomic_impl<int64_t>,
+ // the platform wait is directly monitoring the atomic value itself.
+ __atomic_load_cpo __atomic_load = {};
+ __monitor_val = __atomic_load(__a_, __order_);
+ return __poll_(__monitor_val);
+ }
+
+ _LIBCPP_AVAILABILITY_SYNC
+ _LIBCPP_HIDE_FROM_ABI bool
+ __update_monitor_val_and_poll(void const volatile* __contention_address, __cxx_contention_t& __monitor_val) const {
+ // In case the contention type is anything else, platform wait is monitoring a __cxx_atomic_contention_t
+ // from the global pool, the monitor comes from __libcpp_atomic_monitor
+ __monitor_val = std::__libcpp_atomic_monitor(__contention_address);
+ __atomic_load_cpo __atomic_load = {};
+ auto __current_val = __atomic_load(__a_, __order_);
+ return __poll_(__current_val);
+ }
+
_LIBCPP_AVAILABILITY_SYNC
_LIBCPP_HIDE_FROM_ABI bool operator()(chrono::nanoseconds __elapsed) const {
if (__elapsed > chrono::microseconds(64)) {
- auto __monitor = std::__libcpp_atomic_monitor(__a_);
- if (__backoff_test_(__monitor))
+ __atomic_contention_address_cpo __atomic_contention_address = {};
+ auto __contention_address = __atomic_contention_address(__a_);
+ __cxx_contention_t __monitor_val;
+ if (__update_monitor_val_and_poll(__contention_address, __monitor_val))
return true;
- std::__libcpp_atomic_wait(__a_, __monitor);
+ std::__libcpp_atomic_wait(__contention_address, __monitor_val);
} else if (__elapsed > chrono::microseconds(4))
__libcpp_thread_yield();
else {
@@ -62,37 +141,46 @@ struct __libcpp_atomic_wait_backoff_impl {
}
};
-template <class _Atp, class _Poll, class _BackoffTest>
-_LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI bool
-__cxx_atomic_wait(_Atp* __a, _Poll&& __poll, _BackoffTest&& __backoff_test) {
- __libcpp_atomic_wait_backoff_impl<_Atp, __decay_t<_BackoffTest> > __backoff_fn = {__a, __backoff_test};
- return std::__libcpp_thread_poll_with_backoff(__poll, __backoff_fn);
+template <class _AtomicWaitable, class _Poll>
+_LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void
+__atomic_wait_unless(const _AtomicWaitable& __a, _Poll&& __poll, memory_order __order) {
+ static_assert(__atomic_waitable<_AtomicWaitable>::value, "");
+ __atomic_wait_poll_impl<_AtomicWaitable, __decay_t<_Poll> > __poll_impl = {__a, __poll, __order};
+ __atomic_wait_backoff_impl<_AtomicWaitable, __decay_t<_Poll> > __backoff_fn = {__a, __poll, __order};
+ std::__libcpp_thread_poll_with_backoff(__poll_impl, __backoff_fn);
}
-template <class _Poll>
-struct __libcpp_atomic_wait_poll_as_backoff_test {
- _Poll __poll_;
-
- _LIBCPP_AVAILABILITY_SYNC
- _LIBCPP_HIDE_FROM_ABI bool operator()(__cxx_contention_t&) const { return __poll_(); }
-};
+template <class _AtomicWaitable>
+_LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void __atomic_notify_one(const _AtomicWaitable& __a) {
+ static_assert(__atomic_waitable<_AtomicWaitable>::value, "");
+ __atomic_contention_address_cpo __atomic_contention_address = {};
+ std::__cxx_atomic_notify_one(__atomic_contention_address(__a));
+}
-template <class _Atp, class _Poll>
-_LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI bool __cxx_atomic_wait(_Atp* __a, _Poll&& __poll) {
- __libcpp_atomic_wait_backoff_impl<_Atp, __libcpp_atomic_wait_poll_as_backoff_test<_Poll&> > __backoff_fn = {
- __a, {__poll}};
- return std::__libcpp_thread_poll_with_backoff(__poll, __backoff_fn);
+template <class _AtomicWaitable>
+_LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void __atomic_notify_all(const _AtomicWaitable& __a) {
+ static_assert(__atomic_waitable<_AtomicWaitable>::value, "");
+ __atomic_contention_address_cpo __atomic_contention_address = {};
+ std::__cxx_atomic_notify_all(__atomic_contention_address(__a));
}
#else // _LIBCPP_HAS_NO_THREADS
-template <class _Tp>
-_LIBCPP_HIDE_FROM_ABI void __cxx_atomic_notify_all(__cxx_atomic_impl<_Tp> const volatile*) {}
-template <class _Tp>
-_LIBCPP_HIDE_FROM_ABI void __cxx_atomic_notify_one(__cxx_atomic_impl<_Tp> const volatile*) {}
-template <class _Atp, class _Fn>
-_LIBCPP_HIDE_FROM_ABI bool __cxx_atomic_wait(_Atp*, _Fn&& __test_fn) {
- return std::__libcpp_thread_poll_with_backoff(__test_fn, __spinning_backoff_policy());
+template <class _AtomicWaitable, class _Poll>
+_LIBCPP_HIDE_FROM_ABI void __atomic_wait_unless(const _AtomicWaitable& __a, _Poll&& __poll, memory_order __order) {
+ static_assert(__atomic_waitable<_AtomicWaitable>::value, "");
+ __atomic_wait_poll_impl<_AtomicWaitable, __decay_t<_Poll> > __poll_fn = {__a, __poll, __order};
+ return std::__libcpp_thread_poll_with_backoff(__poll_fn, __spinning_backoff_policy());
+}
+
+template <class _AtomicWaitable>
+_LIBCPP_HIDE_FROM_ABI void __atomic_notify_one(const _AtomicWaitable&) {
+ static_assert(__atomic_waitable<_AtomicWaitable>::value, "");
+}
+
+template <class _AtomicWaitable>
+_LIBCPP_HIDE_FROM_ABI void __atomic_notify_all(const _AtomicWaitable&) {
+ static_assert(__atomic_waitable<_AtomicWaitable>::value, "");
}
#endif // _LIBCPP_HAS_NO_THREADS
@@ -102,21 +190,20 @@ _LIBCPP_HIDE_FROM_ABI bool __cxx_nonatomic_compare_equal(_Tp const& __lhs, _Tp c
return std::memcmp(std::addressof(__lhs), std::addressof(__rhs), sizeof(_Tp)) == 0;
}
-template <class _Atp, class _Tp>
-struct __cxx_atomic_wait_test_fn_impl {
- _Atp* __a;
- _Tp __val;
- memory_order __order;
- _LIBCPP_HIDE_FROM_ABI bool operator()() const {
- return !std::__cxx_nonatomic_compare_equal(std::__cxx_atomic_load(__a, __order), __val);
+template <class _Tp>
+struct __bind_nonatomic_equal {
+ _Tp __val_;
+ _LIBCPP_HIDE_FROM_ABI bool operator()(const _Tp& __arg) const {
+ return !std::__cxx_nonatomic_compare_equal(__arg, __val_);
}
};
-template <class _Atp, class _Tp>
-_LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI bool
-__cxx_atomic_wait(_Atp* __a, _Tp const __val, memory_order __order) {
- __cxx_atomic_wait_test_fn_impl<_Atp, _Tp> __test_fn = {__a, __val, __order};
- return std::__cxx_atomic_wait(__a, __test_fn);
+template <class _AtomicWaitable, class _Up>
+_LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void
+__atomic_wait(_AtomicWaitable& __a, _Up __val, memory_order __order) {
+ static_assert(__atomic_waitable<_AtomicWaitable>::value, "");
+ __bind_nonatomic_equal<_Up> __nonatomic_equal = {__val};
+ std::__atomic_wait_unless(__a, __nonatomic_equal, __order);
}
_LIBCPP_END_NAMESPACE_STD
diff --git a/libcxx/include/latch b/libcxx/include/latch
index ad7b35579913b3..684244baaaf1e1 100644
--- a/libcxx/include/latch
+++ b/libcxx/include/latch
@@ -97,9 +97,13 @@ public:
if (__old == __update)
__a_.notify_all();
}
- inline _LIBCPP_HIDE_FROM_ABI bool try_wait() const noexcept { return 0 == __a_.load(memory_order_acquire); }
+ inline _LIBCPP_HIDE_FROM_ABI bool try_wait() const noexcept {
+ auto __value = __a_.load(memory_order_acquire);
+ return try_wait_impl(__value);
+ }
inline _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void wait() const {
- __cxx_atomic_wait(&__a_.__a_, [this]() -> bool { return try_wait(); });
+ std::__atomic_wait_unless(
+ __a_, [this](ptrdiff_t& __value) -> bool { return try_wait_impl(__value); }, memory_order_acquire);
}
inline _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void arrive_and_wait(ptrdiff_t __update = 1) {
_LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN(__update >= 0, "latch::arrive_and_wait called with a negative value");
@@ -108,6 +112,9 @@ public:
count_down(__update);
wait();
}
+
+private:
+ _LIBCPP_HIDE_FROM_ABI bool try_wait_impl(ptrdiff_t& __value) const noexcept { return __value == 0; }
};
_LIBCPP_END_NAMESPACE_STD
diff --git a/libcxx/include/semaphore b/libcxx/include/semaphore
index 5235d720bf6fee..a49a888d907227 100644
--- a/libcxx/include/semaphore
+++ b/libcxx/include/semaphore
@@ -100,17 +100,8 @@ public:
}
}
_LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void acquire() {
- auto const __poll_fn = [this]() -> bool {
- auto __old = __a_.load(memory_order_relaxed);
- return (__old != 0) && __a_.compare_exchange_strong(__old, __old - 1, memory_order_acquire, memory_order_relaxed);
- };
- auto const __backoff_test = [this](__cxx_contention_t& __monitor) -> bool {
- ptrdiff_t __old = __monitor;
- bool __r = __try_acquire_impl(__old);
- __monitor = static_cast<__cxx_contention_t>(__old);
- return __r;
- };
- __cxx_atomic_wait(&__a_.__a_, __poll_fn, __backoff_test);
+ std::__atomic_wait_unless(
+ __a_, [this](ptrdiff_t& __old) { return __try_acquire_impl(__old); }, memory_order_relaxed);
}
template <class _Rep, class _Period>
_LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI bool
>From 35f01cd28f09e2f437966c39a8c4fce6b840fa58 Mon Sep 17 00:00:00 2001
From: Hui <hui.xie0621 at gmail.com>
Date: Sun, 11 Feb 2024 20:20:12 +0000
Subject: [PATCH 2/6] adl lint
---
libcxx/include/__atomic/atomic_sync.h | 7 +++++--
1 file changed, 5 insertions(+), 2 deletions(-)
diff --git a/libcxx/include/__atomic/atomic_sync.h b/libcxx/include/__atomic/atomic_sync.h
index 12f70f9a936590..9adcbaa133ca1b 100644
--- a/libcxx/include/__atomic/atomic_sync.h
+++ b/libcxx/include/__atomic/atomic_sync.h
@@ -38,13 +38,14 @@ _LIBCPP_BEGIN_NAMESPACE_STD
// Note that std::atomic<T>::wait was back-ported to C++03
// there the below implementations look ugly to support C++03
+// NOLINTBEGIN(libcpp-robust-against-adl)
struct __atomic_load_cpo {
template <class _Tp>
using _Ret = decltype(__tag_invoke(
std::declval<__atomic_load_cpo>(), std::declval<const _Tp&>(), std::declval<memory_order>()));
template <class _Tp>
- _Ret<_Tp> operator()(const _Tp& __t, memory_order __order) const _NOEXCEPT {
+ _LIBCPP_HIDE_FROM_ABI _Ret<_Tp> operator()(const _Tp& __t, memory_order __order) const _NOEXCEPT {
return __tag_invoke(*this, __t, __order);
}
};
@@ -56,13 +57,15 @@ struct __atomic_contention_address_cpo {
using _Ret = decltype(__tag_invoke(std::declval<__atomic_contention_address_cpo>(), std::declval<const _Tp&>()));
template <class _Tp>
- _Ret<_Tp> operator()(const _Tp& __t) const _NOEXCEPT {
+ _LIBCPP_HIDE_FROM_ABI _Ret<_Tp> operator()(const _Tp& __t) const _NOEXCEPT {
return __tag_invoke(*this, __t);
}
};
// TODO: if we can deprecate std::atomic<T>::wait before c++17, we could add
// inline constexpr __atomic_contention_address_cpo __atomic_contention_address{};
+// NOLINTEND(libcpp-robust-against-adl)
+
template <class _Tp>
using __atomic_waitable =
_And<__invokable<__atomic_load_cpo, const _Tp&, memory_order>,
>From 5fda0a21581005fe00caaa05d34cf536f3831ceb Mon Sep 17 00:00:00 2001
From: Hui <hui.xie0621 at gmail.com>
Date: Mon, 12 Feb 2024 12:11:56 +0000
Subject: [PATCH 3/6] ci
---
libcxx/include/__atomic/atomic_flag.h | 8 ++++----
libcxx/include/__atomic/atomic_sync.h | 2 +-
2 files changed, 5 insertions(+), 5 deletions(-)
diff --git a/libcxx/include/__atomic/atomic_flag.h b/libcxx/include/__atomic/atomic_flag.h
index f66caf7d1f5217..a8fac3344a3c21 100644
--- a/libcxx/include/__atomic/atomic_flag.h
+++ b/libcxx/include/__atomic/atomic_flag.h
@@ -77,13 +77,13 @@ struct atomic_flag {
std::__atomic_wait(*this, _LIBCPP_ATOMIC_FLAG_TYPE(__v), __m);
}
_LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void notify_one() volatile _NOEXCEPT {
- __cxx_atomic_notify_one(&__a_);
+ std::__atomic_notify_one(*this);
}
- _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void notify_one() _NOEXCEPT { __cxx_atomic_notify_one(&__a_); }
+ _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void notify_one() _NOEXCEPT { std::__atomic_notify_one(*this); }
_LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void notify_all() volatile _NOEXCEPT {
- __cxx_atomic_notify_all(&__a_);
+ std::__atomic_notify_all(*this);
}
- _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void notify_all() _NOEXCEPT { __cxx_atomic_notify_all(&__a_); }
+ _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void notify_all() _NOEXCEPT { std::__atomic_notify_all(*this); }
#if _LIBCPP_STD_VER >= 20
_LIBCPP_HIDE_FROM_ABI constexpr atomic_flag() _NOEXCEPT : __a_(false) {}
diff --git a/libcxx/include/__atomic/atomic_sync.h b/libcxx/include/__atomic/atomic_sync.h
index 9adcbaa133ca1b..8c830aa7b0bd43 100644
--- a/libcxx/include/__atomic/atomic_sync.h
+++ b/libcxx/include/__atomic/atomic_sync.h
@@ -173,7 +173,7 @@ template <class _AtomicWaitable, class _Poll>
_LIBCPP_HIDE_FROM_ABI void __atomic_wait_unless(const _AtomicWaitable& __a, _Poll&& __poll, memory_order __order) {
static_assert(__atomic_waitable<_AtomicWaitable>::value, "");
__atomic_wait_poll_impl<_AtomicWaitable, __decay_t<_Poll> > __poll_fn = {__a, __poll, __order};
- return std::__libcpp_thread_poll_with_backoff(__poll_fn, __spinning_backoff_policy());
+ std::__libcpp_thread_poll_with_backoff(__poll_fn, __spinning_backoff_policy());
}
template <class _AtomicWaitable>
>From 877bc78068e1337dc5f6ced0b62a50da4b886110 Mon Sep 17 00:00:00 2001
From: Hui <hui.xie0621 at gmail.com>
Date: Mon, 12 Feb 2024 17:50:15 +0000
Subject: [PATCH 4/6] try again
---
libcxx/include/__atomic/atomic_sync.h | 9 ++-------
1 file changed, 2 insertions(+), 7 deletions(-)
diff --git a/libcxx/include/__atomic/atomic_sync.h b/libcxx/include/__atomic/atomic_sync.h
index 8c830aa7b0bd43..b641d442cd734b 100644
--- a/libcxx/include/__atomic/atomic_sync.h
+++ b/libcxx/include/__atomic/atomic_sync.h
@@ -171,20 +171,15 @@ _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void __atomic_notify_all(const _
template <class _AtomicWaitable, class _Poll>
_LIBCPP_HIDE_FROM_ABI void __atomic_wait_unless(const _AtomicWaitable& __a, _Poll&& __poll, memory_order __order) {
- static_assert(__atomic_waitable<_AtomicWaitable>::value, "");
__atomic_wait_poll_impl<_AtomicWaitable, __decay_t<_Poll> > __poll_fn = {__a, __poll, __order};
std::__libcpp_thread_poll_with_backoff(__poll_fn, __spinning_backoff_policy());
}
template <class _AtomicWaitable>
-_LIBCPP_HIDE_FROM_ABI void __atomic_notify_one(const _AtomicWaitable&) {
- static_assert(__atomic_waitable<_AtomicWaitable>::value, "");
-}
+_LIBCPP_HIDE_FROM_ABI void __atomic_notify_one(const _AtomicWaitable&) {}
template <class _AtomicWaitable>
-_LIBCPP_HIDE_FROM_ABI void __atomic_notify_all(const _AtomicWaitable&) {
- static_assert(__atomic_waitable<_AtomicWaitable>::value, "");
-}
+_LIBCPP_HIDE_FROM_ABI void __atomic_notify_all(const _AtomicWaitable&) {}
#endif // _LIBCPP_HAS_NO_THREADS
>From b2ebd2ab89bd7854637653de1647444034890485 Mon Sep 17 00:00:00 2001
From: Hui <hui.xie0621 at gmail.com>
Date: Mon, 12 Feb 2024 19:59:06 +0000
Subject: [PATCH 5/6] try again
---
libcxx/include/__atomic/atomic_sync.h | 1 -
1 file changed, 1 deletion(-)
diff --git a/libcxx/include/__atomic/atomic_sync.h b/libcxx/include/__atomic/atomic_sync.h
index b641d442cd734b..ea48435d1d8cfa 100644
--- a/libcxx/include/__atomic/atomic_sync.h
+++ b/libcxx/include/__atomic/atomic_sync.h
@@ -199,7 +199,6 @@ struct __bind_nonatomic_equal {
template <class _AtomicWaitable, class _Up>
_LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void
__atomic_wait(_AtomicWaitable& __a, _Up __val, memory_order __order) {
- static_assert(__atomic_waitable<_AtomicWaitable>::value, "");
__bind_nonatomic_equal<_Up> __nonatomic_equal = {__val};
std::__atomic_wait_unless(__a, __nonatomic_equal, __order);
}
>From fc0403aa39e76f6b1b793210329d36c8f099c9f2 Mon Sep 17 00:00:00 2001
From: Hui <hui.xie0621 at gmail.com>
Date: Fri, 16 Feb 2024 19:58:40 +0000
Subject: [PATCH 6/6] address feedback
---
libcxx/include/__atomic/atomic_base.h | 46 ++++++++++--------
libcxx/include/__atomic/atomic_flag.h | 42 ++++++++--------
libcxx/include/__atomic/atomic_sync.h | 70 +++++++++++----------------
3 files changed, 76 insertions(+), 82 deletions(-)
diff --git a/libcxx/include/__atomic/atomic_base.h b/libcxx/include/__atomic/atomic_base.h
index b7eebcb7651aea..4b08cfaf5e592c 100644
--- a/libcxx/include/__atomic/atomic_base.h
+++ b/libcxx/include/__atomic/atomic_base.h
@@ -102,26 +102,6 @@ struct __atomic_base // false
return std::__cxx_atomic_compare_exchange_strong(std::addressof(__a_), std::addressof(__e), __d, __m, __m);
}
- friend _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI _Tp
- __tag_invoke(__atomic_load_cpo, const __atomic_base& __this, memory_order __order) {
- return __this.load(__order);
- }
-
- friend _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI _Tp
- __tag_invoke(__atomic_load_cpo, const volatile __atomic_base& __this, memory_order __order) {
- return __this.load(__order);
- }
-
- friend _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI __cxx_atomic_impl<_Tp> const*
- __tag_invoke(__atomic_contention_address_cpo, const __atomic_base& __this) {
- return std::addressof(__this.__a_);
- }
-
- friend _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI __cxx_atomic_impl<_Tp> const volatile*
- __tag_invoke(__atomic_contention_address_cpo, const volatile __atomic_base& __this) {
- return std::addressof(__this.__a_);
- }
-
_LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void wait(_Tp __v, memory_order __m = memory_order_seq_cst) const
volatile _NOEXCEPT {
std::__atomic_wait(*this, __v, __m);
@@ -216,6 +196,32 @@ struct __atomic_base<_Tp, true> : public __atomic_base<_Tp, false> {
_LIBCPP_HIDE_FROM_ABI _Tp operator^=(_Tp __op) _NOEXCEPT { return fetch_xor(__op) ^ __op; }
};
+// here we need _IsIntegral because the default template argument is not enough
+// e.g __atomic_base<int> is __atomic_base<int, true>, which inherits from
+// __atomic_base<int, false> and the caller of the wait function is
+// __atomic_base<int, false>. So specializing __atomic_base<_Tp> does not work
+template <class _Tp, bool _IsIntegral>
+struct __atomic_waitable_customisations<__atomic_base<_Tp, _IsIntegral> > {
+ static _LIBCPP_HIDE_FROM_ABI _Tp __atomic_load(const __atomic_base<_Tp, _IsIntegral>& __a, memory_order __order) {
+ return __a.load(__order);
+ }
+
+ static _LIBCPP_HIDE_FROM_ABI _Tp
+ __atomic_load(const volatile __atomic_base<_Tp, _IsIntegral>& __this, memory_order __order) {
+ return __this.load(__order);
+ }
+
+ static _LIBCPP_HIDE_FROM_ABI const __cxx_atomic_impl<_Tp>*
+ __atomic_contention_address(const __atomic_base<_Tp, _IsIntegral>& __a) {
+ return std::addressof(__a.__a_);
+ }
+
+ static _LIBCPP_HIDE_FROM_ABI const volatile __cxx_atomic_impl<_Tp>*
+ __atomic_contention_address(const volatile __atomic_base<_Tp, _IsIntegral>& __this) {
+ return std::addressof(__this.__a_);
+ }
+};
+
_LIBCPP_END_NAMESPACE_STD
#endif // _LIBCPP___ATOMIC_ATOMIC_BASE_H
diff --git a/libcxx/include/__atomic/atomic_flag.h b/libcxx/include/__atomic/atomic_flag.h
index a8fac3344a3c21..7c67da28234541 100644
--- a/libcxx/include/__atomic/atomic_flag.h
+++ b/libcxx/include/__atomic/atomic_flag.h
@@ -48,26 +48,6 @@ struct atomic_flag {
__cxx_atomic_store(&__a_, _LIBCPP_ATOMIC_FLAG_TYPE(false), __m);
}
- friend _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI _LIBCPP_ATOMIC_FLAG_TYPE
- __tag_invoke(__atomic_load_cpo, const atomic_flag& __this, memory_order __order) {
- return std::__cxx_atomic_load(&__this.__a_, __order);
- }
-
- friend _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI _LIBCPP_ATOMIC_FLAG_TYPE
- __tag_invoke(__atomic_load_cpo, const volatile atomic_flag& __this, memory_order __order) {
- return std::__cxx_atomic_load(&__this.__a_, __order);
- }
-
- friend _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI __cxx_atomic_impl<_LIBCPP_ATOMIC_FLAG_TYPE> const*
- __tag_invoke(__atomic_contention_address_cpo, const atomic_flag& __this) {
- return std::addressof(__this.__a_);
- }
-
- friend _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI __cxx_atomic_impl<_LIBCPP_ATOMIC_FLAG_TYPE> const volatile*
- __tag_invoke(__atomic_contention_address_cpo, const volatile atomic_flag& __this) {
- return std::addressof(__this.__a_);
- }
-
_LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void wait(bool __v, memory_order __m = memory_order_seq_cst) const
volatile _NOEXCEPT {
std::__atomic_wait(*this, _LIBCPP_ATOMIC_FLAG_TYPE(__v), __m);
@@ -98,6 +78,28 @@ struct atomic_flag {
atomic_flag& operator=(const atomic_flag&) volatile = delete;
};
+template <>
+struct __atomic_waitable_customisations<atomic_flag> {
+ static _LIBCPP_HIDE_FROM_ABI _LIBCPP_ATOMIC_FLAG_TYPE __atomic_load(const atomic_flag& __a, memory_order __order) {
+ return std::__cxx_atomic_load(&__a.__a_, __order);
+ }
+
+ static _LIBCPP_HIDE_FROM_ABI _LIBCPP_ATOMIC_FLAG_TYPE
+ __atomic_load(const volatile atomic_flag& __a, memory_order __order) {
+ return std::__cxx_atomic_load(&__a.__a_, __order);
+ }
+
+ static _LIBCPP_HIDE_FROM_ABI const __cxx_atomic_impl<_LIBCPP_ATOMIC_FLAG_TYPE>*
+ __atomic_contention_address(const atomic_flag& __a) {
+ return std::addressof(__a.__a_);
+ }
+
+ static _LIBCPP_HIDE_FROM_ABI const volatile __cxx_atomic_impl<_LIBCPP_ATOMIC_FLAG_TYPE>*
+ __atomic_contention_address(const volatile atomic_flag& __a) {
+ return std::addressof(__a.__a_);
+ }
+};
+
inline _LIBCPP_HIDE_FROM_ABI bool atomic_flag_test(const volatile atomic_flag* __o) _NOEXCEPT { return __o->test(); }
inline _LIBCPP_HIDE_FROM_ABI bool atomic_flag_test(const atomic_flag* __o) _NOEXCEPT { return __o->test(); }
diff --git a/libcxx/include/__atomic/atomic_sync.h b/libcxx/include/__atomic/atomic_sync.h
index ea48435d1d8cfa..b9d8a547a94649 100644
--- a/libcxx/include/__atomic/atomic_sync.h
+++ b/libcxx/include/__atomic/atomic_sync.h
@@ -21,6 +21,7 @@
#include <__type_traits/conjunction.h>
#include <__type_traits/decay.h>
#include <__type_traits/invoke.h>
+#include <__type_traits/void_t.h>
#include <__utility/declval.h>
#include <cstring>
@@ -36,40 +37,30 @@ _LIBCPP_BEGIN_NAMESPACE_STD
// - __atomic_notify_one
// - __atomic_notify_all
// Note that std::atomic<T>::wait was back-ported to C++03
-// there the below implementations look ugly to support C++03
-
-// NOLINTBEGIN(libcpp-robust-against-adl)
-struct __atomic_load_cpo {
- template <class _Tp>
- using _Ret = decltype(__tag_invoke(
- std::declval<__atomic_load_cpo>(), std::declval<const _Tp&>(), std::declval<memory_order>()));
-
- template <class _Tp>
- _LIBCPP_HIDE_FROM_ABI _Ret<_Tp> operator()(const _Tp& __t, memory_order __order) const _NOEXCEPT {
- return __tag_invoke(*this, __t, __order);
- }
+// The below implementations look ugly to support C++03
+template <class _Tp, class = void>
+struct __atomic_waitable_customisations {
+ template <class _AtomicWaitable>
+ static void __atomic_load(_AtomicWaitable&&, memory_order) = delete;
+
+ template <class _AtomicWaitable>
+ static void __atomic_contention_address(_AtomicWaitable&&) = delete;
};
-// TODO: if we can deprecate std::atomic<T>::wait before c++17, we could add
-// inline constexpr __atomic_load_cpo __atomic_load{};
-
-struct __atomic_contention_address_cpo {
- template <class _Tp>
- using _Ret = decltype(__tag_invoke(std::declval<__atomic_contention_address_cpo>(), std::declval<const _Tp&>()));
- template <class _Tp>
- _LIBCPP_HIDE_FROM_ABI _Ret<_Tp> operator()(const _Tp& __t) const _NOEXCEPT {
- return __tag_invoke(*this, __t);
- }
-};
-// TODO: if we can deprecate std::atomic<T>::wait before c++17, we could add
-// inline constexpr __atomic_contention_address_cpo __atomic_contention_address{};
+template <class _Tp>
+struct __atomic_waitable_customisations<_Tp, __enable_if_t<!__is_same(_Tp, __decay_t<_Tp>)> >
+ : __atomic_waitable_customisations<__decay_t<_Tp> > {};
-// NOLINTEND(libcpp-robust-against-adl)
+template <class _Tp, class = void>
+struct __atomic_waitable : false_type {};
template <class _Tp>
-using __atomic_waitable =
- _And<__invokable<__atomic_load_cpo, const _Tp&, memory_order>,
- __invokable<__atomic_contention_address_cpo, const _Tp&> >;
+struct __atomic_waitable<
+ _Tp,
+ __void_t<decltype(__atomic_waitable_customisations<_Tp>::__atomic_load(
+ std::declval<const _Tp&>(), std::declval<memory_order>())),
+ decltype(__atomic_waitable_customisations<_Tp>::__atomic_contention_address(std::declval<const _Tp&>()))> >
+ : true_type {};
template <class _AtomicWaitable, class _Poll>
struct __atomic_wait_poll_impl {
@@ -78,8 +69,7 @@ struct __atomic_wait_poll_impl {
memory_order __order_;
_LIBCPP_HIDE_FROM_ABI bool operator()() const {
- __atomic_load_cpo __atomic_load = {};
- auto __current_val = __atomic_load(__a_, __order_);
+ auto __current_val = __atomic_waitable_customisations<_AtomicWaitable>::__atomic_load(__a_, __order_);
return __poll_(__current_val);
}
};
@@ -111,8 +101,7 @@ struct __atomic_wait_backoff_impl {
__update_monitor_val_and_poll(__cxx_atomic_contention_t const volatile*, __cxx_contention_t& __monitor_val) const {
// In case the contention type happens to be __cxx_atomic_contention_t, i.e. __cxx_atomic_impl<int64_t>,
// the platform wait is directly monitoring the atomic value itself.
- __atomic_load_cpo __atomic_load = {};
- __monitor_val = __atomic_load(__a_, __order_);
+ __monitor_val = __atomic_waitable_customisations<_AtomicWaitable>::__atomic_load(__a_, __order_);
return __poll_(__monitor_val);
}
@@ -121,17 +110,15 @@ struct __atomic_wait_backoff_impl {
__update_monitor_val_and_poll(void const volatile* __contention_address, __cxx_contention_t& __monitor_val) const {
// In case the contention type is anything else, platform wait is monitoring a __cxx_atomic_contention_t
// from the global pool, the monitor comes from __libcpp_atomic_monitor
- __monitor_val = std::__libcpp_atomic_monitor(__contention_address);
- __atomic_load_cpo __atomic_load = {};
- auto __current_val = __atomic_load(__a_, __order_);
+ __monitor_val = std::__libcpp_atomic_monitor(__contention_address);
+ auto __current_val = __atomic_waitable_customisations<_AtomicWaitable>::__atomic_load(__a_, __order_);
return __poll_(__current_val);
}
_LIBCPP_AVAILABILITY_SYNC
_LIBCPP_HIDE_FROM_ABI bool operator()(chrono::nanoseconds __elapsed) const {
if (__elapsed > chrono::microseconds(64)) {
- __atomic_contention_address_cpo __atomic_contention_address = {};
- auto __contention_address = __atomic_contention_address(__a_);
+ auto __contention_address = __atomic_waitable_customisations<_AtomicWaitable>::__atomic_contention_address(__a_);
__cxx_contention_t __monitor_val;
if (__update_monitor_val_and_poll(__contention_address, __monitor_val))
return true;
@@ -156,15 +143,13 @@ __atomic_wait_unless(const _AtomicWaitable& __a, _Poll&& __poll, memory_order __
template <class _AtomicWaitable>
_LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void __atomic_notify_one(const _AtomicWaitable& __a) {
static_assert(__atomic_waitable<_AtomicWaitable>::value, "");
- __atomic_contention_address_cpo __atomic_contention_address = {};
- std::__cxx_atomic_notify_one(__atomic_contention_address(__a));
+ std::__cxx_atomic_notify_one(__atomic_waitable_customisations<_AtomicWaitable>::__atomic_contention_address(__a));
}
template <class _AtomicWaitable>
_LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void __atomic_notify_all(const _AtomicWaitable& __a) {
static_assert(__atomic_waitable<_AtomicWaitable>::value, "");
- __atomic_contention_address_cpo __atomic_contention_address = {};
- std::__cxx_atomic_notify_all(__atomic_contention_address(__a));
+ std::__cxx_atomic_notify_all(__atomic_waitable_customisations<_AtomicWaitable>::__atomic_contention_address(__a));
}
#else // _LIBCPP_HAS_NO_THREADS
@@ -199,6 +184,7 @@ struct __bind_nonatomic_equal {
template <class _AtomicWaitable, class _Up>
_LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void
__atomic_wait(_AtomicWaitable& __a, _Up __val, memory_order __order) {
+ static_assert(__atomic_waitable<_AtomicWaitable>::value, "");
__bind_nonatomic_equal<_Up> __nonatomic_equal = {__val};
std::__atomic_wait_unless(__a, __nonatomic_equal, __order);
}
More information about the libcxx-commits
mailing list