From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id 06DF73858C50; Mon, 18 Mar 2024 14:04:44 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 06DF73858C50 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1710770685; bh=lSoGkhlxoD/hnlGWOFR9S4v0RX4nOqUZWNADjy715gY=; h=From:To:Subject:Date:From; b=EEgJoGW46Mt54dw242/jDY9rlTL5LL64yxpEKP7Q1ZGta1/XxgrXhRH7yA58RKIWT CN5OWRmOegc+BtvDyGSap/yNK7kAee63epodVV+AsvHZpv0ms28fxHJpcglnxKQnCM xYn1GspPwCJZCGstbiBV2DwNdO4dW3vzmln894ag= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jonathan Wakely To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r12-10237] libstdc++: Remove unconditional use of atomics in Debug Mode X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/releases/gcc-12 X-Git-Oldrev: b9d09c7cfcaeb777c5cbc2848a84142a329992c9 X-Git-Newrev: 73d0816f570c26e856d94d56491e50332fd8b425 Message-Id: <20240318140445.06DF73858C50@sourceware.org> Date: Mon, 18 Mar 2024 14:04:44 +0000 (GMT) List-Id: https://gcc.gnu.org/g:73d0816f570c26e856d94d56491e50332fd8b425 commit r12-10237-g73d0816f570c26e856d94d56491e50332fd8b425 Author: Jonathan Wakely Date: Mon Sep 11 16:42:54 2023 +0100 libstdc++: Remove unconditional use of atomics in Debug Mode 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. (cherry picked from commit 4a2766ed00a47904dc8b85bf0538aa116d8e658b) Diff: --- 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 4706defedf1..1bfc6ccc581 100644 --- a/libstdc++-v3/src/c++11/debug.cc +++ b/libstdc++-v3/src/c++11/debug.cc @@ -24,6 +24,7 @@ #include #include +#include // __is_single_threaded #include #include @@ -173,6 +174,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 @@ -403,7 +429,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(); @@ -425,7 +451,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); _M_version = 0; _M_prior = 0; _M_next = 0; @@ -485,7 +511,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();