Skip to content

Commit 8a27b48

Browse files
H-G-HristovZingam
andauthored
[libc++][atomic] P2835R7: Expose std::atomic_ref's object address (llvm#162236)
Implements https://wg21.link/P2835R7 Closes llvm#118377 # References - https://wg21.link/atomics.ref.generic.general - https://wg21.link/atomics.ref.int - https://wg21.link/atomics.ref.float - https://wg21.link/atomics.ref.pointer --------- Co-authored-by: Hristo Hristov <[email protected]>
1 parent b9877ec commit 8a27b48

File tree

10 files changed

+89
-7
lines changed

10 files changed

+89
-7
lines changed

libcxx/docs/FeatureTestMacroTable.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,8 @@ Status
418418
---------------------------------------------------------- -----------------
419419
``__cpp_lib_atomic_min_max`` *unimplemented*
420420
---------------------------------------------------------- -----------------
421+
``__cpp_lib_atomic_ref`` ``202411L``
422+
---------------------------------------------------------- -----------------
421423
``__cpp_lib_bind_front`` ``202306L``
422424
---------------------------------------------------------- -----------------
423425
``__cpp_lib_bitset`` ``202306L``

libcxx/docs/ReleaseNotes/22.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ Implemented Papers
4343
- P3044R2: sub-``string_view`` from ``string`` (`Github <https://llvm.org/PR148140>`__)
4444
- P3223R2: Making ``std::istream::ignore`` less surprising (`Github <https://llvm.org/PR148178>`__)
4545
- P3060R3: Add ``std::views::indices(n)`` (`Github <https://llvm.org/PR148175>`__)
46+
- P2835R7: Expose ``std::atomic_ref``'s object address (`Github <https://llvm.org/PR118377>`__)
4647
- P3168R2: Give ``std::optional`` Range Support (`Github <https://llvm.org/PR105430>`__)
4748

4849
Improvements and New Features

libcxx/docs/Status/Cxx2cPapers.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@
8585
"`P3222R0 <https://wg21.link/P3222R0>`__","Fix C++26 by adding transposed special cases for P2642 layouts","2024-11 (Wrocław)","","","`#118374 <https://github.com/llvm/llvm-project/issues/118374>`__",""
8686
"`P3050R2 <https://wg21.link/P3050R2>`__","Fix C++26 by optimizing ``linalg::conjugated`` for noncomplex value types","2024-11 (Wrocław)","","","`#118375 <https://github.com/llvm/llvm-project/issues/118375>`__",""
8787
"`P3396R1 <https://wg21.link/P3396R1>`__","``std::execution`` wording fixes","2024-11 (Wrocław)","","","`#118376 <https://github.com/llvm/llvm-project/issues/118376>`__",""
88-
"`P2835R7 <https://wg21.link/P2835R7>`__","Expose ``std::atomic_ref``'s object address","2024-11 (Wrocław)","","","`#118377 <https://github.com/llvm/llvm-project/issues/118377>`__",""
88+
"`P2835R7 <https://wg21.link/P2835R7>`__","Expose ``std::atomic_ref``'s object address","2024-11 (Wrocław)","|Complete|","22","`#118377 <https://github.com/llvm/llvm-project/issues/118377>`__",""
8989
"`P3323R1 <https://wg21.link/P3323R1>`__","cv-qualified types in ``atomic`` and ``atomic_ref``","2024-11 (Wrocław)","","","`#118378 <https://github.com/llvm/llvm-project/issues/118378>`__",""
9090
"`P3508R0 <https://wg21.link/P3508R0>`__","Wording for ""constexpr for specialized memory algorithms""","2024-11 (Wrocław)","","","`#118379 <https://github.com/llvm/llvm-project/issues/118379>`__",""
9191
"`P3369R0 <https://wg21.link/P3369R0>`__","constexpr for ``uninitialized_default_construct``","2024-11 (Wrocław)","","","`#118380 <https://github.com/llvm/llvm-project/issues/118380>`__",""

libcxx/include/__atomic/atomic_ref.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,9 @@ struct __atomic_ref_base {
220220
}
221221
_LIBCPP_HIDE_FROM_ABI void notify_one() const noexcept { std::__atomic_notify_one(*this); }
222222
_LIBCPP_HIDE_FROM_ABI void notify_all() const noexcept { std::__atomic_notify_all(*this); }
223+
# if _LIBCPP_STD_VER >= 26
224+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp* address() const noexcept { return __ptr_; }
225+
# endif
223226

224227
protected:
225228
using _Aligned_Tp [[__gnu__::__aligned__(required_alignment), __gnu__::__nodebug__]] = _Tp;

libcxx/include/version

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ __cpp_lib_atomic_float 201711L <atomic>
3737
__cpp_lib_atomic_is_always_lock_free 201603L <atomic>
3838
__cpp_lib_atomic_lock_free_type_aliases 201907L <atomic>
3939
__cpp_lib_atomic_min_max 202403L <atomic>
40-
__cpp_lib_atomic_ref 201806L <atomic>
40+
__cpp_lib_atomic_ref 202411L <atomic>
41+
201806L // C++20
4142
__cpp_lib_atomic_shared_ptr 201711L <atomic>
4243
__cpp_lib_atomic_value_initialization 201911L <atomic> <memory>
4344
__cpp_lib_atomic_wait 201907L <atomic>
@@ -544,6 +545,8 @@ __cpp_lib_void_t 201411L <type_traits>
544545
# define __cpp_lib_aligned_accessor 202411L
545546
// # define __cpp_lib_associative_heterogeneous_insertion 202306L
546547
// # define __cpp_lib_atomic_min_max 202403L
548+
# undef __cpp_lib_atomic_ref
549+
# define __cpp_lib_atomic_ref 202411L
547550
# undef __cpp_lib_bind_front
548551
# define __cpp_lib_bind_front 202306L
549552
# define __cpp_lib_bitset 202306L
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// REQUIRES: std-at-least-c++26
10+
11+
// check that <atomic> functions are marked [[nodiscard]]
12+
13+
#include <atomic>
14+
15+
#include "atomic_helpers.h"
16+
#include "test_macros.h"
17+
18+
template <typename T>
19+
struct TestAtomicRef {
20+
void operator()() const {
21+
T x(T(1));
22+
const std::atomic_ref<T> a(x);
23+
24+
a.address(); // expected-warning 4 {{ignoring return value of function declared with 'nodiscard' attribute}}
25+
}
26+
};
27+
28+
void test() {
29+
TestAtomicRef<UserAtomicType>()();
30+
TestAtomicRef<int>()();
31+
TestAtomicRef<float>()();
32+
TestAtomicRef<char*>()();
33+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//
2+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
3+
// See https://llvm.org/LICENSE.txt for license information.
4+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5+
//
6+
//===----------------------------------------------------------------------===//
7+
8+
// REQUIRES: std-at-least-c++26
9+
10+
// constexpr T* address() const noexcept;
11+
12+
#include <atomic>
13+
#include <cassert>
14+
#include <concepts>
15+
#include <memory>
16+
17+
#include "atomic_helpers.h"
18+
#include "test_macros.h"
19+
20+
template <typename T>
21+
struct TestAddress {
22+
void operator()() const {
23+
T x(T(1));
24+
const std::atomic_ref<T> a(x);
25+
26+
std::same_as<T*> decltype(auto) p = a.address();
27+
assert(std::addressof(x) == p);
28+
29+
static_assert(noexcept((a.address())));
30+
}
31+
};
32+
33+
int main(int, char**) {
34+
TestEachAtomicType<TestAddress>()();
35+
36+
return 0;
37+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,8 +355,8 @@
355355
# ifndef __cpp_lib_atomic_ref
356356
# error "__cpp_lib_atomic_ref should be defined in c++26"
357357
# endif
358-
# if __cpp_lib_atomic_ref != 201806L
359-
# error "__cpp_lib_atomic_ref should have the value 201806L in c++26"
358+
# if __cpp_lib_atomic_ref != 202411L
359+
# error "__cpp_lib_atomic_ref should have the value 202411L in c++26"
360360
# endif
361361

362362
# if !defined(_LIBCPP_VERSION)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6325,8 +6325,8 @@
63256325
# ifndef __cpp_lib_atomic_ref
63266326
# error "__cpp_lib_atomic_ref should be defined in c++26"
63276327
# endif
6328-
# if __cpp_lib_atomic_ref != 201806L
6329-
# error "__cpp_lib_atomic_ref should have the value 201806L in c++26"
6328+
# if __cpp_lib_atomic_ref != 202411L
6329+
# error "__cpp_lib_atomic_ref should have the value 202411L in c++26"
63306330
# endif
63316331

63326332
# if !defined(_LIBCPP_VERSION)

libcxx/utils/generate_feature_test_macro_components.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,10 @@ def add_version_header(tc):
195195
},
196196
{
197197
"name": "__cpp_lib_atomic_ref",
198-
"values": {"c++20": 201806},
198+
"values": {
199+
"c++20": 201806,
200+
"c++26": 202411, # P2835R7: Expose std::atomic_ref 's object address
201+
},
199202
"headers": ["atomic"],
200203
},
201204
{

0 commit comments

Comments
 (0)