-
Notifications
You must be signed in to change notification settings - Fork 18k
[libc++] Implement P0528R3 std::atomic CAS for types with padding
#76180
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
41 commits
Select commit
Hold shift + click to select a range
6272ee2
[libc++] make std::atomic works with types with paddings
huixie90 5e5b628
gcc
huixie90 bdfc4c9
lint
huixie90 af90c9a
gcc c++11 ci
huixie90 1e9d805
Update libcxx/test/std/atomics/atomics.types.generic/padding.pass.cpp
huixie90 1c047a9
Update libcxx/include/__atomic/support/c11.h
huixie90 543d559
Update libcxx/include/__atomic/support/common.h
huixie90 fa10f55
review comments batch 1
huixie90 7334f3e
batch2
huixie90 8dfd839
CI
huixie90 f938e09
review comment
huixie90 8ff3104
review
huixie90 0eb7a5e
14
huixie90 347f6e9
14
huixie90 b28d7e4
ci
huixie90 37c72ba
ci
huixie90 1cc0224
CI
huixie90 386f7e3
ci
huixie90 029073a
review
huixie90 fd55e4b
release notes
huixie90 f529e02
ci
huixie90 ddbc06a
ci
huixie90 f7b6225
debug
huixie90 8f5626f
Revert "debug"
huixie90 40c3316
test ci
huixie90 75b19b2
padding
huixie90 399acfd
clang22
huixie90 bc33e49
cxx 03
huixie90 7b17a5b
ci
huixie90 5ddfbe6
ci
huixie90 749277c
ci
huixie90 5739b6c
ci
huixie90 2adfb7c
ci
huixie90 e278047
review
huixie90 7d841c5
inline
huixie90 3b63e5c
f
huixie90 edc991c
Update libcxx/include/__atomic/support/c11.h
huixie90 6417035
Update libcxx/utils/libcxx/test/features/misc.py
huixie90 6a15f6a
review
huixie90 a546c54
ci
huixie90 5766ff8
rename
huixie90 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #ifndef _LIBCPP___ATOMIC_CLEAR_PADDING_H | ||
| #define _LIBCPP___ATOMIC_CLEAR_PADDING_H | ||
|
|
||
| #include <__config> | ||
| #include <__memory/addressof.h> | ||
| #include <__type_traits/conjunction.h> | ||
| #include <__type_traits/enable_if.h> | ||
| #include <__type_traits/has_unique_object_representation.h> | ||
| #include <__type_traits/integral_constant.h> | ||
| #include <__type_traits/is_same.h> | ||
| #include <__type_traits/negation.h> | ||
| #include <__type_traits/remove_cv.h> | ||
| #include <__type_traits/remove_cvref.h> | ||
| #include <__utility/forward.h> | ||
| #include <cstring> | ||
|
|
||
| #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) | ||
| # pragma GCC system_header | ||
| #endif | ||
|
|
||
| _LIBCPP_BEGIN_NAMESPACE_STD | ||
|
|
||
| #if __has_builtin(__builtin_clear_padding) | ||
|
|
||
| template <class _Tp> | ||
| struct __needs_clear_padding | ||
| : _And<_Not<integral_constant<bool, __has_unique_object_representations(_Tp)> >, | ||
| _Not<is_same<_Tp, float> >, | ||
| _Not<is_same<_Tp, double> > > {}; | ||
|
|
||
| template <class _Tp, __enable_if_t<!__needs_clear_padding<__remove_cvref_t<_Tp> >::value, int> = 0> | ||
| _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _Tp& __clear_padding_if_needed(_Tp& __obj) _NOEXCEPT { | ||
| return __obj; | ||
| } | ||
|
|
||
| template <class _Tp, __enable_if_t<__needs_clear_padding<__remove_cvref_t<_Tp> >::value, int> = 0> | ||
| _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _Tp& __clear_padding_if_needed(_Tp& __obj) _NOEXCEPT { | ||
| return __builtin_is_constant_evaluated() ? __obj : (__builtin_clear_padding(std::addressof(__obj)), __obj); | ||
| } | ||
|
|
||
| // clang fails to inline the function when the memory order is a constant | ||
| template <class _Tp, | ||
| class _Up, | ||
| class _CasFunc, | ||
| __enable_if_t<!__needs_clear_padding<__remove_cvref_t<_Tp> >::value, int> = 0> | ||
| _LIBCPP_HIDE_FROM_ABI _LIBCPP_ALWAYS_INLINE bool | ||
| __atomic_cas_with_clear_padding(_Tp* __expected, _Up __value, _CasFunc&& __cas_func) { | ||
| return __cas_func(__expected, __value); | ||
| } | ||
|
|
||
| template <class _Tp, | ||
| class _Up, | ||
| class _CasFunc, | ||
| __enable_if_t<__needs_clear_padding<__remove_cvref_t<_Tp> >::value, int> = 0> | ||
| _LIBCPP_HIDE_FROM_ABI bool __atomic_cas_with_clear_padding(_Tp* __expected, _Up __value, _CasFunc&& __cas_func) { | ||
| std::__clear_padding_if_needed(__value); | ||
| __remove_cvref_t<_Tp> __expected_copy = *__expected; | ||
| std::__clear_padding_if_needed(__expected_copy); | ||
| if (__cas_func(std::addressof(__expected_copy), __value)) { | ||
| return true; | ||
| } else { | ||
| std::memcpy(__expected, std::addressof(__expected_copy), sizeof(__remove_cvref_t<_Tp>)); | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| #else // __has_builtin(__builtin_clear_padding) | ||
|
|
||
| template <class _Tp> | ||
| _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _Tp& __clear_padding_if_needed(_Tp& __obj) _NOEXCEPT { | ||
| return __obj; | ||
| } | ||
|
|
||
| template <class _Tp, class _Up, class _CasFunc> | ||
| _LIBCPP_HIDE_FROM_ABI _LIBCPP_ALWAYS_INLINE bool | ||
| __atomic_cas_with_clear_padding(_Tp* __expected, _Up __value, _CasFunc&& __cas_func) { | ||
| return __cas_func(__expected, __value); | ||
| } | ||
|
|
||
| #endif // __has_builtin(__builtin_clear_padding) | ||
|
|
||
| _LIBCPP_END_NAMESPACE_STD | ||
|
|
||
| #endif // _LIBCPP___ATOMIC_CLEAR_PADDING_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this marked with
_LIBCPP_ALWAYS_INLINE?Edit: Sorry, missed the comment above.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah Ok. Let me know if the comment is unclear. The detailed discussion is here #76180 (comment)