From: Jonathan Wakely <jwakely@redhat.com>
To: Thomas Rodgers <rodgert@appliantology.com>
Cc: gcc Patches <gcc-patches@gcc.gnu.org>,
"libstdc++" <libstdc++@gcc.gnu.org>,
Thomas Rodgers <trodgers@redhat.com>,
Thomas Rodgers <rodgert@twrodgers.com>
Subject: Re: [PATCH] libstdc++: Clear padding bits in atomic compare_exchange
Date: Wed, 29 Sep 2021 13:18:55 +0100 [thread overview]
Message-ID: <CACb0b4kwJeT8UdKcWb0LdsMKf_6W=2HO7G-u2VRPAjD1o2r25g@mail.gmail.com> (raw)
In-Reply-To: <CACb0b4=dS6mPgzHtXfva9x52u4v2H_O=j=57npUDyn8rcYwU2w@mail.gmail.com>
[-- Attachment #1: Type: text/plain, Size: 242 bytes --]
On Wed, 29 Sept 2021 at 13:13, Jonathan Wakely wrote:
> We repeat this *a lot*. When I started work on this I defined a
> non-member function in the __atomic_impl namespace:
I've attached my incomplete patch from when I was working on this.
[-- Attachment #2: patch.txt --]
[-- Type: text/plain, Size: 2929 bytes --]
diff --cc libstdc++-v3/include/bits/atomic_base.h
index 71e1de078b5,35023ad30a8..00000000000
--- a/libstdc++-v3/include/bits/atomic_base.h
+++ b/libstdc++-v3/include/bits/atomic_base.h
@@@ -944,6 -952,28 +944,27 @@@ _GLIBCXX_BEGIN_NAMESPACE_VERSIO
template<typename _Tp>
using _Val = remove_volatile_t<_Tp>;
+ template<typename _Tp>
+ constexpr bool
+ __maybe_has_padding()
+ {
-#if _GLIBCXX_HAVE_BUILTIN_HAS_UNIQ_OBJ_REP
- return !__has_unique_object_representations(_Tp);
++#if __has_builtin(__has_unique_object_representations)
++ return !__has_unique_object_representations(_Tp)
++ && !is_floating_point<_Tp>::value;
+ #else
+ return true;
+ #endif
+ }
+
+ template<typename _Tp>
+ _GLIBCXX_ALWAYS_INLINE void
+ __clear_padding(_Tp& __val) noexcept
+ {
- auto* __ptr = std::__addressof(__val);
+ #if __has_builtin(__builtin_clear_padding)
- __builtin_clear_padding(__ptr);
++ __builtin_clear_padding(std::__addressof(__val));
+ #endif
- return __ptr;
+ }
+
// As above, but for difference_type arguments.
template<typename _Tp>
using _Diff = conditional_t<is_pointer_v<_Tp>, ptrdiff_t, _Val<_Tp>>;
@@@ -984,13 -1015,26 +1006,26 @@@
template<typename _Tp>
_GLIBCXX_ALWAYS_INLINE bool
compare_exchange_weak(_Tp* __ptr, _Val<_Tp>& __expected,
- _Val<_Tp> __desired, memory_order __success,
- memory_order __failure) noexcept
+ _Val<_Tp> __desired,
+ memory_order __success, memory_order __failure,
+ bool __weak = true) noexcept
{
+ __glibcxx_assert(__is_valid_cmpexch_failure_order(__failure));
-
+ #if __has_builtin(__builtin_clear_padding)
+ if _GLIBCXX_CONSTEXPR17 (__maybe_has_padding<_Tp>())
+ {
+ _Val<_Tp> __expected0 = __expected;
+ auto* __exp = __atomic_impl::__clear_padding(__expected0);
+ auto* __des = __atomic_impl::__clear_padding(__desired);
+ if (__atomic_compare_exchange(__ptr, __exp, __des, __weak,
+ int(__success), int(__failure)))
+ return true;
+ __builtin_memcpy(std::__addressof(__expected), __exp, sizeof(_Tp));
+ return false;
+ }
- else
+ #endif
return __atomic_compare_exchange(__ptr, std::__addressof(__expected),
- std::__addressof(__desired), true,
+ std::__addressof(__desired), __weak,
int(__success), int(__failure));
}
@@@ -1000,11 -1044,8 +1035,9 @@@
_Val<_Tp> __desired, memory_order __success,
memory_order __failure) noexcept
{
+ __glibcxx_assert(__is_valid_cmpexch_failure_order(__failure));
-
- return __atomic_compare_exchange(__ptr, std::__addressof(__expected),
- std::__addressof(__desired), false,
- int(__success), int(__failure));
+ return compare_exchange_weak(__ptr, __expected, __desired, __success,
+ __failure, false);
}
#if __cpp_lib_atomic_wait
next prev parent reply other threads:[~2021-09-29 12:19 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-09-23 18:08 Thomas Rodgers
2021-09-23 19:07 ` Jakub Jelinek
2021-09-23 20:15 ` Thomas Rodgers
2021-09-23 20:15 ` Jonathan Wakely
2021-09-27 14:10 ` Thomas Rodgers
2021-09-29 12:13 ` Jonathan Wakely
2021-09-29 12:18 ` Jonathan Wakely [this message]
2021-09-29 12:28 ` Jakub Jelinek
2021-09-29 18:22 ` Thomas Rodgers
2021-09-29 18:29 ` Jakub Jelinek
2021-11-02 1:25 ` Thomas Rodgers
2021-11-02 7:49 ` Jakub Jelinek
2021-11-03 3:06 ` Thomas Rodgers
2021-11-02 8:49 ` Daniel Krügler
2022-01-18 21:48 ` Jonathan Wakely
2022-08-25 10:11 ` Patch ping (was Re: [PATCH] libstdc++: Clear padding bits in atomic compare_exchange) Jakub Jelinek
2022-09-01 22:57 ` Thomas Rodgers
2022-09-07 11:56 ` Jonathan Wakely
2022-09-07 22:06 ` Thomas Rodgers
2022-09-09 18:36 ` Rainer Orth
2022-09-09 18:46 ` Iain Sandoe
2022-09-09 19:01 ` Thomas Rodgers
2022-09-09 20:14 ` Jonathan Wakely
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='CACb0b4kwJeT8UdKcWb0LdsMKf_6W=2HO7G-u2VRPAjD1o2r25g@mail.gmail.com' \
--to=jwakely@redhat.com \
--cc=gcc-patches@gcc.gnu.org \
--cc=libstdc++@gcc.gnu.org \
--cc=rodgert@appliantology.com \
--cc=rodgert@twrodgers.com \
--cc=trodgers@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).