From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id D9EB8385770A for ; Mon, 11 Sep 2023 16:08:39 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D9EB8385770A Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1694448519; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=5tlo6Yb6i1U9TnXiFCxnTFihJD+UiNOe/zCtgY4fwMg=; b=hFkUQ6f9di46k9/3frr965le0TtgdwtESJvprrhowx2rQffy3PhLFZNlu9v8VHjDHE9jnv WHmO1ltpr2rJ+Sipccu0+76Hjn8/SDxJWywecPQsHM5Qze59Iy0MCT6cLNmBKJf966XUkW yZxtZEKOWHaNmPfJ46ntYXTREc3xi2k= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-582-DPMNJv0kMEKf7RIoyfdjkw-1; Mon, 11 Sep 2023 12:08:38 -0400 X-MC-Unique: DPMNJv0kMEKf7RIoyfdjkw-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id F206529DD98F; Mon, 11 Sep 2023 16:08:36 +0000 (UTC) Received: from localhost (unknown [10.42.28.190]) by smtp.corp.redhat.com (Postfix) with ESMTP id B42C52156701; Mon, 11 Sep 2023 16:08:36 +0000 (UTC) From: Jonathan Wakely 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 Message-ID: <20230911160836.1912657-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.6 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-12.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: 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 #include +#include // __is_single_threaded #include #include @@ -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