Skip to content

Commit e4d4e91

Browse files
committed
[libc++] constexpr atomic and atomic_ref
1 parent 8a7f829 commit e4d4e91

File tree

8 files changed

+95
-13
lines changed

8 files changed

+95
-13
lines changed

libcxx/docs/FeatureTestMacroTable.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,8 @@ Status
404404
---------------------------------------------------------- -----------------
405405
``__cpp_lib_bitset`` ``202306L``
406406
---------------------------------------------------------- -----------------
407+
``__cpp_lib_constexpr_atomic`` ``202406L``
408+
---------------------------------------------------------- -----------------
407409
``__cpp_lib_constexpr_new`` *unimplemented*
408410
---------------------------------------------------------- -----------------
409411
``__cpp_lib_constrained_equality`` *unimplemented*

libcxx/include/__atomic/atomic_base.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,23 +122,23 @@ struct __atomic_base // false
122122
std::__atomic_notify_one(*this);
123123
}
124124
_LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 void notify_one() _NOEXCEPT {
125-
if !consteval {
125+
if (!__libcpp_is_constant_evaluated()) {
126126
std::__atomic_notify_one(*this);
127127
}
128128
}
129129
_LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void notify_all() volatile _NOEXCEPT {
130130
std::__atomic_notify_all(*this);
131131
}
132132
_LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 void notify_all() _NOEXCEPT {
133-
if !consteval {
133+
if (!__libcpp_is_constant_evaluated()) {
134134
std::__atomic_notify_all(*this);
135135
}
136136
}
137137

138138
#if _LIBCPP_STD_VER >= 20
139139
_LIBCPP_HIDE_FROM_ABI constexpr __atomic_base() noexcept(is_nothrow_default_constructible_v<_Tp>) : __a_(_Tp()) {}
140140
#else
141-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR __atomic_base() _NOEXCEPT = default;
141+
_LIBCPP_HIDE_FROM_ABI __atomic_base() _NOEXCEPT = default;
142142
#endif
143143

144144
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR __atomic_base(_Tp __d) _NOEXCEPT : __a_(__d) {}

libcxx/include/__atomic/atomic_ref.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <__memory/addressof.h>
2828
#include <__type_traits/has_unique_object_representation.h>
2929
#include <__type_traits/is_trivially_copyable.h>
30+
#include <__utility/exchange.h>
3031
#include <cstddef>
3132
#include <cstdint>
3233
#include <cstring>
@@ -161,9 +162,7 @@ struct __atomic_ref_base {
161162
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 _Tp
162163
exchange(_Tp __desired, memory_order __order = memory_order::seq_cst) const noexcept {
163164
if (__libcpp_is_constant_evaluated()) {
164-
_Tp tmp = *__ptr_;
165-
*__ptr_ = __desired;
166-
return tmp;
165+
return std::exchange(*__ptr_, __desired);
167166
} else {
168167
alignas(_Tp) byte __mem[sizeof(_Tp)];
169168
auto* __ret = reinterpret_cast<_Tp*>(__mem);
@@ -286,12 +285,12 @@ struct __atomic_ref_base {
286285
}
287286
}
288287
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 void notify_one() const noexcept {
289-
if !consteval {
288+
if (!__libcpp_is_constant_evaluated()) {
290289
std::__atomic_notify_one(*this);
291290
}
292291
}
293292
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 void notify_all() const noexcept {
294-
if !consteval {
293+
if (!__libcpp_is_constant_evaluated()) {
295294
std::__atomic_notify_all(*this);
296295
}
297296
}

libcxx/include/__atomic/cxx_atomic_impl.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <__config>
1515
#include <__memory/addressof.h>
1616
#include <__type_traits/is_assignable.h>
17+
#include <__type_traits/is_constant_evaluated.h>
1718
#include <__type_traits/is_trivially_copyable.h>
1819
#include <__type_traits/remove_const.h>
1920
#include <cstddef>
@@ -283,11 +284,15 @@ struct __cxx_atomic_base_impl {
283284
# define __cxx_atomic_is_lock_free(__s) __c11_atomic_is_lock_free(__s)
284285

285286
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR inline void __cxx_atomic_thread_fence(memory_order __order) _NOEXCEPT {
286-
__c11_atomic_thread_fence(static_cast<__memory_order_underlying_t>(__order));
287+
if (!__libcpp_is_constant_evaluated()) {
288+
__c11_atomic_thread_fence(static_cast<__memory_order_underlying_t>(__order));
289+
}
287290
}
288291

289292
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR inline void __cxx_atomic_signal_fence(memory_order __order) _NOEXCEPT {
290-
__c11_atomic_signal_fence(static_cast<__memory_order_underlying_t>(__order));
293+
if (!__libcpp_is_constant_evaluated()) {
294+
__c11_atomic_signal_fence(static_cast<__memory_order_underlying_t>(__order));
295+
}
291296
}
292297

293298
template <class _Tp>

libcxx/include/version

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ __cpp_lib_clamp 201603L <algorithm>
6161
__cpp_lib_complex_udls 201309L <complex>
6262
__cpp_lib_concepts 202002L <concepts>
6363
__cpp_lib_constexpr_algorithms 201806L <algorithm> <utility>
64+
__cpp_lib_constexpr_atomic 202406L <atomic>
6465
__cpp_lib_constexpr_bitset 202207L <bitset>
6566
__cpp_lib_constexpr_charconv 202207L <charconv>
6667
__cpp_lib_constexpr_cmath 202202L <cmath> <cstdlib>
@@ -387,9 +388,6 @@ __cpp_lib_void_t 201411L <type_traits>
387388
# endif
388389
# define __cpp_lib_concepts 202002L
389390
# define __cpp_lib_constexpr_algorithms 201806L
390-
# if __has_constexpr_builtin(__c11_atomic_load)
391-
# define __cpp_constexpr_atomic 202406L
392-
# endif
393391
# define __cpp_lib_constexpr_complex 201711L
394392
# define __cpp_lib_constexpr_dynamic_alloc 201907L
395393
# define __cpp_lib_constexpr_functional 201907L
@@ -513,6 +511,9 @@ __cpp_lib_void_t 201411L <type_traits>
513511
# undef __cpp_lib_bind_front
514512
# define __cpp_lib_bind_front 202306L
515513
# define __cpp_lib_bitset 202306L
514+
# if __has_constexpr_builtin(__c11_atomic_load)
515+
# define __cpp_lib_constexpr_atomic 202406L
516+
# endif
516517
// # define __cpp_lib_constexpr_new 202406L
517518
// # define __cpp_lib_constrained_equality 202403L
518519
// # define __cpp_lib_copyable_function 202306L

libcxx/test/std/language.support/support.limits/support.limits.general/atomic.version.compile.pass.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
__cpp_lib_atomic_value_initialization 201911L [C++20]
2727
__cpp_lib_atomic_wait 201907L [C++20]
2828
__cpp_lib_char8_t 201907L [C++20]
29+
__cpp_lib_constexpr_atomic 202406L [C++26]
2930
*/
3031

3132
#include <atomic>
@@ -73,6 +74,10 @@
7374
# error "__cpp_lib_char8_t should not be defined before c++20"
7475
# endif
7576

77+
# ifdef __cpp_lib_constexpr_atomic
78+
# error "__cpp_lib_constexpr_atomic should not be defined before c++26"
79+
# endif
80+
7681
#elif TEST_STD_VER == 14
7782

7883
# ifdef __cpp_lib_atomic_flag_test
@@ -115,6 +120,10 @@
115120
# error "__cpp_lib_char8_t should not be defined before c++20"
116121
# endif
117122

123+
# ifdef __cpp_lib_constexpr_atomic
124+
# error "__cpp_lib_constexpr_atomic should not be defined before c++26"
125+
# endif
126+
118127
#elif TEST_STD_VER == 17
119128

120129
# ifdef __cpp_lib_atomic_flag_test
@@ -160,6 +169,10 @@
160169
# error "__cpp_lib_char8_t should not be defined before c++20"
161170
# endif
162171

172+
# ifdef __cpp_lib_constexpr_atomic
173+
# error "__cpp_lib_constexpr_atomic should not be defined before c++26"
174+
# endif
175+
163176
#elif TEST_STD_VER == 20
164177

165178
# ifndef __cpp_lib_atomic_flag_test
@@ -253,6 +266,10 @@
253266
# endif
254267
# endif
255268

269+
# ifdef __cpp_lib_constexpr_atomic
270+
# error "__cpp_lib_constexpr_atomic should not be defined before c++26"
271+
# endif
272+
256273
#elif TEST_STD_VER == 23
257274

258275
# ifndef __cpp_lib_atomic_flag_test
@@ -346,6 +363,10 @@
346363
# endif
347364
# endif
348365

366+
# ifdef __cpp_lib_constexpr_atomic
367+
# error "__cpp_lib_constexpr_atomic should not be defined before c++26"
368+
# endif
369+
349370
#elif TEST_STD_VER > 23
350371

351372
# ifndef __cpp_lib_atomic_flag_test
@@ -448,5 +469,18 @@
448469
# endif
449470
# endif
450471

472+
# if __has_constexpr_builtin(__c11_atomic_load)
473+
# ifndef __cpp_lib_constexpr_atomic
474+
# error "__cpp_lib_constexpr_atomic should be defined in c++26"
475+
# endif
476+
# if __cpp_lib_constexpr_atomic != 202406L
477+
# error "__cpp_lib_constexpr_atomic should have the value 202406L in c++26"
478+
# endif
479+
# else
480+
# ifdef __cpp_lib_constexpr_atomic
481+
# error "__cpp_lib_constexpr_atomic should not be defined when the requirement '__has_constexpr_builtin(__c11_atomic_load)' is not met!"
482+
# endif
483+
# endif
484+
451485
#endif // TEST_STD_VER > 23
452486

libcxx/test/std/language.support/support.limits/support.limits.general/version.version.compile.pass.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
__cpp_lib_complex_udls 201309L [C++14]
5757
__cpp_lib_concepts 202002L [C++20]
5858
__cpp_lib_constexpr_algorithms 201806L [C++20]
59+
__cpp_lib_constexpr_atomic 202406L [C++26]
5960
__cpp_lib_constexpr_bitset 202207L [C++23]
6061
__cpp_lib_constexpr_charconv 202207L [C++23]
6162
__cpp_lib_constexpr_cmath 202202L [C++23]
@@ -402,6 +403,10 @@
402403
# error "__cpp_lib_constexpr_algorithms should not be defined before c++20"
403404
# endif
404405

406+
# ifdef __cpp_lib_constexpr_atomic
407+
# error "__cpp_lib_constexpr_atomic should not be defined before c++26"
408+
# endif
409+
405410
# ifdef __cpp_lib_constexpr_bitset
406411
# error "__cpp_lib_constexpr_bitset should not be defined before c++23"
407412
# endif
@@ -1266,6 +1271,10 @@
12661271
# error "__cpp_lib_constexpr_algorithms should not be defined before c++20"
12671272
# endif
12681273

1274+
# ifdef __cpp_lib_constexpr_atomic
1275+
# error "__cpp_lib_constexpr_atomic should not be defined before c++26"
1276+
# endif
1277+
12691278
# ifdef __cpp_lib_constexpr_bitset
12701279
# error "__cpp_lib_constexpr_bitset should not be defined before c++23"
12711280
# endif
@@ -2232,6 +2241,10 @@
22322241
# error "__cpp_lib_constexpr_algorithms should not be defined before c++20"
22332242
# endif
22342243

2244+
# ifdef __cpp_lib_constexpr_atomic
2245+
# error "__cpp_lib_constexpr_atomic should not be defined before c++26"
2246+
# endif
2247+
22352248
# ifdef __cpp_lib_constexpr_bitset
22362249
# error "__cpp_lib_constexpr_bitset should not be defined before c++23"
22372250
# endif
@@ -3438,6 +3451,10 @@
34383451
# error "__cpp_lib_constexpr_algorithms should have the value 201806L in c++20"
34393452
# endif
34403453

3454+
# ifdef __cpp_lib_constexpr_atomic
3455+
# error "__cpp_lib_constexpr_atomic should not be defined before c++26"
3456+
# endif
3457+
34413458
# ifdef __cpp_lib_constexpr_bitset
34423459
# error "__cpp_lib_constexpr_bitset should not be defined before c++23"
34433460
# endif
@@ -4857,6 +4874,10 @@
48574874
# error "__cpp_lib_constexpr_algorithms should have the value 201806L in c++23"
48584875
# endif
48594876

4877+
# ifdef __cpp_lib_constexpr_atomic
4878+
# error "__cpp_lib_constexpr_atomic should not be defined before c++26"
4879+
# endif
4880+
48604881
# ifndef __cpp_lib_constexpr_bitset
48614882
# error "__cpp_lib_constexpr_bitset should be defined in c++23"
48624883
# endif
@@ -6492,6 +6513,19 @@
64926513
# error "__cpp_lib_constexpr_algorithms should have the value 201806L in c++26"
64936514
# endif
64946515

6516+
# if __has_constexpr_builtin(__c11_atomic_load)
6517+
# ifndef __cpp_lib_constexpr_atomic
6518+
# error "__cpp_lib_constexpr_atomic should be defined in c++26"
6519+
# endif
6520+
# if __cpp_lib_constexpr_atomic != 202406L
6521+
# error "__cpp_lib_constexpr_atomic should have the value 202406L in c++26"
6522+
# endif
6523+
# else
6524+
# ifdef __cpp_lib_constexpr_atomic
6525+
# error "__cpp_lib_constexpr_atomic should not be defined when the requirement '__has_constexpr_builtin(__c11_atomic_load)' is not met!"
6526+
# endif
6527+
# endif
6528+
64956529
# ifndef __cpp_lib_constexpr_bitset
64966530
# error "__cpp_lib_constexpr_bitset should be defined in c++26"
64976531
# endif

libcxx/utils/generate_feature_test_macro_components.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,13 @@ def add_version_header(tc):
318318
},
319319
"headers": ["algorithm", "utility"],
320320
},
321+
{
322+
"name": "__cpp_lib_constexpr_atomic",
323+
"values": {"c++26": 202406},
324+
"headers": ["atomic"],
325+
"libcxx_guard": "__has_constexpr_builtin(__c11_atomic_load)",
326+
"test_suite_guard": "__has_constexpr_builtin(__c11_atomic_load)",
327+
},
321328
{
322329
"name": "__cpp_lib_constexpr_bitset",
323330
"values": {"c++23": 202207},

0 commit comments

Comments
 (0)