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 [63.128.21.124]) by sourceware.org (Postfix) with ESMTP id 86CC039450F3 for ; Tue, 24 Nov 2020 14:59:57 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 86CC039450F3 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-486-Ka8WPo9CMQSUf2nnuANQfA-1; Tue, 24 Nov 2020 09:59:54 -0500 X-MC-Unique: Ka8WPo9CMQSUf2nnuANQfA-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 7BB0718C89D0; Tue, 24 Nov 2020 14:59:53 +0000 (UTC) Received: from localhost (unknown [10.33.37.15]) by smtp.corp.redhat.com (Postfix) with ESMTP id 28EDC60864; Tue, 24 Nov 2020 14:59:52 +0000 (UTC) Date: Tue, 24 Nov 2020 14:59:52 +0000 From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Use __libc_single_threaded for locale initialization Message-ID: <20201124145952.GA1857606@redhat.com> MIME-Version: 1.0 X-Clacks-Overhead: GNU Terry Pratchett X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: multipart/mixed; boundary="LZvS9be/3tNcYl/X" Content-Disposition: inline X-Spam-Status: No, score=-14.0 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_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libstdc++@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++ mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 24 Nov 2020 15:00:08 -0000 --LZvS9be/3tNcYl/X Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Most initialization of locales and facets happens before main() during startup, when the program is likely to only have one thread. By using the new __gnu_cxx::__is_single_threaded() function instead of checking __gthread_active_p() we can avoid using pthread_once or atomics for the common case. That said, I'm not sure why we don't just use a local static variable instead, as __cxa_guard_acquire() already optimizes for the single-threaded case: static const bool init = (_S_initialize_once(), true); I'll revisit that for GCC 12. libstdc++-v3/ChangeLog: * src/c++98/locale.cc (locale::facet::_S_get_c_locale()) (locale::id::_M_id() const): Use __is_single_threaded. * src/c++98/locale_init.cc (locale::_S_initialize()): Likewise. Tested powerpc64le-linux. Committed to trunk. --LZvS9be/3tNcYl/X Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="patch.txt" commit e253d36214015ed10ffd335e3628ccaac22dd5c7 Author: Jonathan Wakely Date: Tue Nov 24 12:29:30 2020 libstdc++: Use __libc_single_threaded for locale initialization Most initialization of locales and facets happens before main() during startup, when the program is likely to only have one thread. By using the new __gnu_cxx::__is_single_threaded() function instead of checking __gthread_active_p() we can avoid using pthread_once or atomics for the common case. That said, I'm not sure why we don't just use a local static variable instead, as __cxa_guard_acquire() already optimizes for the single-threaded case: static const bool init = (_S_initialize_once(), true); I'll revisit that for GCC 12. libstdc++-v3/ChangeLog: * src/c++98/locale.cc (locale::facet::_S_get_c_locale()) (locale::id::_M_id() const): Use __is_single_threaded. * src/c++98/locale_init.cc (locale::_S_initialize()): Likewise. diff --git a/libstdc++-v3/src/c++98/locale.cc b/libstdc++-v3/src/c++98/locale.cc index 06422412039c..9b3fc3515152 100644 --- a/libstdc++-v3/src/c++98/locale.cc +++ b/libstdc++-v3/src/c++98/locale.cc @@ -214,7 +214,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION locale::facet::_S_get_c_locale() { #ifdef __GTHREADS - if (__gthread_active_p()) + if (!__gnu_cxx::__is_single_threaded()) __gthread_once(&_S_once, _S_initialize_once); else #endif @@ -515,7 +515,7 @@ namespace { #endif #ifdef __GTHREADS - if (__gthread_active_p()) + if (!__gnu_cxx::__is_single_threaded()) { if (__atomic_always_lock_free(sizeof(_M_index), &_M_index)) { diff --git a/libstdc++-v3/src/c++98/locale_init.cc b/libstdc++-v3/src/c++98/locale_init.cc index c3841ccbd3c9..fc8416ba01a6 100644 --- a/libstdc++-v3/src/c++98/locale_init.cc +++ b/libstdc++-v3/src/c++98/locale_init.cc @@ -320,7 +320,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION locale::_S_initialize() { #ifdef __GTHREADS - if (__gthread_active_p()) + if (!__gnu_cxx::__is_single_threaded()) __gthread_once(&_S_once, _S_initialize_once); #endif if (!_S_classic) --LZvS9be/3tNcYl/X--