Skip to content

Commit

Permalink
[libc++] constexpr atomic and atomic_ref
Browse files Browse the repository at this point in the history
  • Loading branch information
hanickadot committed Jul 14, 2024
1 parent 125f277 commit 8eb0435
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 13 deletions.
2 changes: 2 additions & 0 deletions libcxx/docs/FeatureTestMacroTable.rst
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,8 @@ Status
---------------------------------------------------------- -----------------
``__cpp_lib_bitset`` ``202306L``
---------------------------------------------------------- -----------------
``__cpp_lib_constexpr_atomic`` ``202406L``
---------------------------------------------------------- -----------------
``__cpp_lib_constexpr_new`` *unimplemented*
---------------------------------------------------------- -----------------
``__cpp_lib_constrained_equality`` *unimplemented*
Expand Down
6 changes: 3 additions & 3 deletions libcxx/include/__atomic/atomic_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,23 +122,23 @@ struct __atomic_base // false
std::__atomic_notify_one(*this);
}
_LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 void notify_one() _NOEXCEPT {
if !consteval {
if (!__libcpp_is_constant_evaluated()) {
std::__atomic_notify_one(*this);
}
}
_LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void notify_all() volatile _NOEXCEPT {
std::__atomic_notify_all(*this);
}
_LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 void notify_all() _NOEXCEPT {
if !consteval {
if (!__libcpp_is_constant_evaluated()) {
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()) {}
#else
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR __atomic_base() _NOEXCEPT = default;
_LIBCPP_HIDE_FROM_ABI __atomic_base() _NOEXCEPT = default;
#endif

_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR __atomic_base(_Tp __d) _NOEXCEPT : __a_(__d) {}
Expand Down
9 changes: 4 additions & 5 deletions libcxx/include/__atomic/atomic_ref.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <__memory/addressof.h>
#include <__type_traits/has_unique_object_representation.h>
#include <__type_traits/is_trivially_copyable.h>
#include <__utility/exchange.h>
#include <cstddef>
#include <cstdint>
#include <cstring>
Expand Down Expand Up @@ -148,9 +149,7 @@ struct __atomic_ref_base {
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 _Tp
exchange(_Tp __desired, memory_order __order = memory_order::seq_cst) const noexcept {
if (__libcpp_is_constant_evaluated()) {
_Tp tmp = *__ptr_;
*__ptr_ = __desired;
return tmp;
return std::exchange(*__ptr_, __desired);
} else {
alignas(_Tp) byte __mem[sizeof(_Tp)];
auto* __ret = reinterpret_cast<_Tp*>(__mem);
Expand Down Expand Up @@ -273,12 +272,12 @@ struct __atomic_ref_base {
}
}
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 void notify_one() const noexcept {
if !consteval {
if (!__libcpp_is_constant_evaluated()) {
std::__atomic_notify_one(*this);
}
}
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 void notify_all() const noexcept {
if !consteval {
if (!__libcpp_is_constant_evaluated()) {
std::__atomic_notify_all(*this);
}
}
Expand Down
9 changes: 7 additions & 2 deletions libcxx/include/__atomic/cxx_atomic_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <__config>
#include <__memory/addressof.h>
#include <__type_traits/is_assignable.h>
#include <__type_traits/is_constant_evaluated.h>
#include <__type_traits/is_trivially_copyable.h>
#include <__type_traits/remove_const.h>
#include <cstddef>
Expand Down Expand Up @@ -283,11 +284,15 @@ struct __cxx_atomic_base_impl {
# define __cxx_atomic_is_lock_free(__s) __c11_atomic_is_lock_free(__s)

_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR inline void __cxx_atomic_thread_fence(memory_order __order) _NOEXCEPT {
__c11_atomic_thread_fence(static_cast<__memory_order_underlying_t>(__order));
if (!__libcpp_is_constant_evaluated()) {
__c11_atomic_thread_fence(static_cast<__memory_order_underlying_t>(__order));
}
}

_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR inline void __cxx_atomic_signal_fence(memory_order __order) _NOEXCEPT {
__c11_atomic_signal_fence(static_cast<__memory_order_underlying_t>(__order));
if (!__libcpp_is_constant_evaluated()) {
__c11_atomic_signal_fence(static_cast<__memory_order_underlying_t>(__order));
}
}

template <class _Tp>
Expand Down
7 changes: 4 additions & 3 deletions libcxx/include/version
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ __cpp_lib_clamp 201603L <algorithm>
__cpp_lib_complex_udls 201309L <complex>
__cpp_lib_concepts 202002L <concepts>
__cpp_lib_constexpr_algorithms 201806L <algorithm> <utility>
__cpp_lib_constexpr_atomic 202406L <atomic>
__cpp_lib_constexpr_bitset 202207L <bitset>
__cpp_lib_constexpr_charconv 202207L <charconv>
__cpp_lib_constexpr_cmath 202202L <cmath> <cstdlib>
Expand Down Expand Up @@ -384,9 +385,6 @@ __cpp_lib_void_t 201411L <type_traits>
# endif
# define __cpp_lib_concepts 202002L
# define __cpp_lib_constexpr_algorithms 201806L
# if __has_constexpr_builtin(__c11_atomic_load)
# define __cpp_constexpr_atomic 202406L
# endif
# define __cpp_lib_constexpr_complex 201711L
# define __cpp_lib_constexpr_dynamic_alloc 201907L
# define __cpp_lib_constexpr_functional 201907L
Expand Down Expand Up @@ -509,6 +507,9 @@ __cpp_lib_void_t 201411L <type_traits>
# undef __cpp_lib_bind_front
# define __cpp_lib_bind_front 202306L
# define __cpp_lib_bitset 202306L
# if __has_constexpr_builtin(__c11_atomic_load)
# define __cpp_lib_constexpr_atomic 202406L
# endif
// # define __cpp_lib_constexpr_new 202406L
// # define __cpp_lib_constrained_equality 202403L
// # define __cpp_lib_copyable_function 202306L
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
__cpp_lib_atomic_value_initialization 201911L [C++20]
__cpp_lib_atomic_wait 201907L [C++20]
__cpp_lib_char8_t 201907L [C++20]
__cpp_lib_constexpr_atomic 202406L [C++26]
*/

#include <atomic>
Expand Down Expand Up @@ -73,6 +74,10 @@
# error "__cpp_lib_char8_t should not be defined before c++20"
# endif

# ifdef __cpp_lib_constexpr_atomic
# error "__cpp_lib_constexpr_atomic should not be defined before c++26"
# endif

#elif TEST_STD_VER == 14

# ifdef __cpp_lib_atomic_flag_test
Expand Down Expand Up @@ -115,6 +120,10 @@
# error "__cpp_lib_char8_t should not be defined before c++20"
# endif

# ifdef __cpp_lib_constexpr_atomic
# error "__cpp_lib_constexpr_atomic should not be defined before c++26"
# endif

#elif TEST_STD_VER == 17

# ifdef __cpp_lib_atomic_flag_test
Expand Down Expand Up @@ -160,6 +169,10 @@
# error "__cpp_lib_char8_t should not be defined before c++20"
# endif

# ifdef __cpp_lib_constexpr_atomic
# error "__cpp_lib_constexpr_atomic should not be defined before c++26"
# endif

#elif TEST_STD_VER == 20

# ifndef __cpp_lib_atomic_flag_test
Expand Down Expand Up @@ -253,6 +266,10 @@
# endif
# endif

# ifdef __cpp_lib_constexpr_atomic
# error "__cpp_lib_constexpr_atomic should not be defined before c++26"
# endif

#elif TEST_STD_VER == 23

# ifndef __cpp_lib_atomic_flag_test
Expand Down Expand Up @@ -346,6 +363,10 @@
# endif
# endif

# ifdef __cpp_lib_constexpr_atomic
# error "__cpp_lib_constexpr_atomic should not be defined before c++26"
# endif

#elif TEST_STD_VER > 23

# ifndef __cpp_lib_atomic_flag_test
Expand Down Expand Up @@ -448,5 +469,18 @@
# endif
# endif

# if __has_constexpr_builtin(__c11_atomic_load)
# ifndef __cpp_lib_constexpr_atomic
# error "__cpp_lib_constexpr_atomic should be defined in c++26"
# endif
# if __cpp_lib_constexpr_atomic != 202406L
# error "__cpp_lib_constexpr_atomic should have the value 202406L in c++26"
# endif
# else
# ifdef __cpp_lib_constexpr_atomic
# error "__cpp_lib_constexpr_atomic should not be defined when the requirement '__has_constexpr_builtin(__c11_atomic_load)' is not met!"
# endif
# endif

#endif // TEST_STD_VER > 23

Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
__cpp_lib_complex_udls 201309L [C++14]
__cpp_lib_concepts 202002L [C++20]
__cpp_lib_constexpr_algorithms 201806L [C++20]
__cpp_lib_constexpr_atomic 202406L [C++26]
__cpp_lib_constexpr_bitset 202207L [C++23]
__cpp_lib_constexpr_charconv 202207L [C++23]
__cpp_lib_constexpr_cmath 202202L [C++23]
Expand Down Expand Up @@ -399,6 +400,10 @@
# error "__cpp_lib_constexpr_algorithms should not be defined before c++20"
# endif

# ifdef __cpp_lib_constexpr_atomic
# error "__cpp_lib_constexpr_atomic should not be defined before c++26"
# endif

# ifdef __cpp_lib_constexpr_bitset
# error "__cpp_lib_constexpr_bitset should not be defined before c++23"
# endif
Expand Down Expand Up @@ -1259,6 +1264,10 @@
# error "__cpp_lib_constexpr_algorithms should not be defined before c++20"
# endif

# ifdef __cpp_lib_constexpr_atomic
# error "__cpp_lib_constexpr_atomic should not be defined before c++26"
# endif

# ifdef __cpp_lib_constexpr_bitset
# error "__cpp_lib_constexpr_bitset should not be defined before c++23"
# endif
Expand Down Expand Up @@ -2221,6 +2230,10 @@
# error "__cpp_lib_constexpr_algorithms should not be defined before c++20"
# endif

# ifdef __cpp_lib_constexpr_atomic
# error "__cpp_lib_constexpr_atomic should not be defined before c++26"
# endif

# ifdef __cpp_lib_constexpr_bitset
# error "__cpp_lib_constexpr_bitset should not be defined before c++23"
# endif
Expand Down Expand Up @@ -3423,6 +3436,10 @@
# error "__cpp_lib_constexpr_algorithms should have the value 201806L in c++20"
# endif

# ifdef __cpp_lib_constexpr_atomic
# error "__cpp_lib_constexpr_atomic should not be defined before c++26"
# endif

# ifdef __cpp_lib_constexpr_bitset
# error "__cpp_lib_constexpr_bitset should not be defined before c++23"
# endif
Expand Down Expand Up @@ -4838,6 +4855,10 @@
# error "__cpp_lib_constexpr_algorithms should have the value 201806L in c++23"
# endif

# ifdef __cpp_lib_constexpr_atomic
# error "__cpp_lib_constexpr_atomic should not be defined before c++26"
# endif

# ifndef __cpp_lib_constexpr_bitset
# error "__cpp_lib_constexpr_bitset should be defined in c++23"
# endif
Expand Down Expand Up @@ -6472,6 +6493,19 @@
# error "__cpp_lib_constexpr_algorithms should have the value 201806L in c++26"
# endif

# if __has_constexpr_builtin(__c11_atomic_load)
# ifndef __cpp_lib_constexpr_atomic
# error "__cpp_lib_constexpr_atomic should be defined in c++26"
# endif
# if __cpp_lib_constexpr_atomic != 202406L
# error "__cpp_lib_constexpr_atomic should have the value 202406L in c++26"
# endif
# else
# ifdef __cpp_lib_constexpr_atomic
# error "__cpp_lib_constexpr_atomic should not be defined when the requirement '__has_constexpr_builtin(__c11_atomic_load)' is not met!"
# endif
# endif

# ifndef __cpp_lib_constexpr_bitset
# error "__cpp_lib_constexpr_bitset should be defined in c++26"
# endif
Expand Down
7 changes: 7 additions & 0 deletions libcxx/utils/generate_feature_test_macro_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,13 @@ def add_version_header(tc):
},
"headers": ["algorithm", "utility"],
},
{
"name": "__cpp_lib_constexpr_atomic",
"values": {"c++26": 202406},
"headers": ["atomic"],
"libcxx_guard": "__has_constexpr_builtin(__c11_atomic_load)",
"test_suite_guard": "__has_constexpr_builtin(__c11_atomic_load)",
},
{
"name": "__cpp_lib_constexpr_bitset",
"values": {"c++23": 202207},
Expand Down

0 comments on commit 8eb0435

Please sign in to comment.