public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jonathan Wakely <jwakely@redhat.com>
To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org
Subject: [committed] libstdc++: Remove unconditional use of atomics in Debug Mode
Date: Mon, 11 Sep 2023 17:08:12 +0100	[thread overview]
Message-ID: <20230911160836.1912657-1-jwakely@redhat.com> (raw)

Tested aarch64-linux. Pushed to trunk.

Probably worth backporting too.

-- >8 --

The fix for PR 91910 (r10-3426-gf7a3a382279585) introduced unconditional
uses of atomics into src/c++11/debug.cc, which causes linker errors for
arm4t where GCC emits an unresolved reference to __sync_synchronize.

By making the uses of atomics depend on _GLIBCXX_HAS_GTHREADS we can
avoid those unconditional references to __sync_synchronize for targets
where the atomics are unnecessary. As a minor performance optimization
we can also check the __gnu_cxx::__is_single_threaded function to avoid
atomics for single-threaded programs even where they don't cause linker
errors.

libstdc++-v3/ChangeLog:

	* src/c++11/debug.cc (acquire_sequence_ptr_for_lock): New
	function.
	(reset_sequence_ptr): New function.
	(_Safe_iterator_base::_M_detach)
	(_Safe_local_iterator_base::_M_detach): Replace bare atomic_load
	with acquire_sequence_ptr_for_lock.
	(_Safe_iterator_base::_M_reset): Replace bare atomic_store with
	reset_sequence_ptr.
---
 libstdc++-v3/src/c++11/debug.cc | 32 +++++++++++++++++++++++++++++---
 1 file changed, 29 insertions(+), 3 deletions(-)

diff --git a/libstdc++-v3/src/c++11/debug.cc b/libstdc++-v3/src/c++11/debug.cc
index f40e995e0f3..bb0d0db6679 100644
--- a/libstdc++-v3/src/c++11/debug.cc
+++ b/libstdc++-v3/src/c++11/debug.cc
@@ -24,6 +24,7 @@
 
 #include <bits/move.h>
 #include <bits/stl_iterator_base_types.h>
+#include <ext/atomicity.h> // __is_single_threaded
 
 #include <debug/formatter.h>
 #include <debug/safe_base.h>
@@ -156,6 +157,31 @@ namespace
 	__old->_M_reset();
       }
   }
+
+  void*
+  acquire_sequence_ptr_for_lock(__gnu_debug::_Safe_sequence_base*& seq)
+  {
+#ifdef __GTHREADS
+    if (!__gnu_cxx::__is_single_threaded())
+      return __atomic_load_n(&seq, __ATOMIC_ACQUIRE);
+#endif
+    return seq;
+  }
+
+  void
+  reset_sequence_ptr(__gnu_debug::_Safe_sequence_base*& seq)
+  {
+#ifdef __GTHREADS
+    if (!__gnu_cxx::__is_single_threaded())
+      {
+	__atomic_store_n(&seq, (__gnu_debug::_Safe_sequence_base*)nullptr,
+			 __ATOMIC_RELEASE);
+	return;
+      }
+#endif
+    seq = nullptr;
+  }
+
 } // anonymous namespace
 
 namespace __gnu_debug
@@ -439,7 +465,7 @@ namespace __gnu_debug
     // If the sequence destructor runs between loading the pointer and
     // locking the mutex, it will detach this iterator and set _M_sequence
     // to null, and then _M_detach_single() will do nothing.
-    if (auto seq = __atomic_load_n(&_M_sequence, __ATOMIC_ACQUIRE))
+    if (auto seq = acquire_sequence_ptr_for_lock(_M_sequence))
       {
 	__gnu_cxx::__scoped_lock sentry(get_safe_base_mutex(seq));
 	_M_detach_single();
@@ -461,7 +487,7 @@ namespace __gnu_debug
   _Safe_iterator_base::
   _M_reset() throw ()
   {
-    __atomic_store_n(&_M_sequence, (_Safe_sequence_base*)0, __ATOMIC_RELEASE);
+    reset_sequence_ptr(_M_sequence);
     // Do not reset version, so that a detached iterator does not look like a
     // value-initialized one.
     // _M_version = 0;
@@ -523,7 +549,7 @@ namespace __gnu_debug
   _Safe_local_iterator_base::
   _M_detach()
   {
-    if (auto seq = __atomic_load_n(&_M_sequence, __ATOMIC_ACQUIRE))
+    if (auto seq = acquire_sequence_ptr_for_lock(_M_sequence))
       {
 	__gnu_cxx::__scoped_lock sentry(get_safe_base_mutex(seq));
 	_M_detach_single();
-- 
2.41.0


                 reply	other threads:[~2023-09-11 16:08 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20230911160836.1912657-1-jwakely@redhat.com \
    --to=jwakely@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=libstdc++@gcc.gnu.org \
    /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).