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 3E8B7385840D for ; Fri, 19 Nov 2021 20:24:54 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 3E8B7385840D Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-460-Wp2n3sQ_Nze7sA1ZUDjtGw-1; Fri, 19 Nov 2021 15:24:52 -0500 X-MC-Unique: Wp2n3sQ_Nze7sA1ZUDjtGw-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id CD27110144E8; Fri, 19 Nov 2021 20:24:51 +0000 (UTC) Received: from localhost (unknown [10.33.36.17]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7E4CC5D9DE; Fri, 19 Nov 2021 20:24:51 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Use __is_single_threaded in locale initialization Date: Fri, 19 Nov 2021 20:24:50 +0000 Message-Id: <20211119202450.502097-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII" 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_LOW, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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: Fri, 19 Nov 2021 20:24:55 -0000 Tested x86_64-linux, pushed to trunk. This replaces a __gthread_active_p() check with __is_single_threaded() so that std::locale initialization doesn't use __gthread_once if it happens before the first thread is created. This means that _S_initialize_once() might now be called twice instead of only once, because if __is_single_threaded() changes to false then we will do the __gthread_once call even if _S_initialize_once() was already called. Add a check to _S_initialize_once() and return immediately if it is the second call. Also use __builtin_expect to _S_initialize, as the branch will be taken at most once in the lifetime of the program. libstdc++-v3/ChangeLog: * src/c++98/locale_init.cc (_S_initialize_once): Check if initialization has already been done. (_S_initialize): Replace __gthread_active_p with __is_single_threaded. Use __builtin_expect. --- libstdc++-v3/src/c++98/locale_init.cc | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/libstdc++-v3/src/c++98/locale_init.cc b/libstdc++-v3/src/c++98/locale_init.cc index e96b1a336aa..ff563b80465 100644 --- a/libstdc++-v3/src/c++98/locale_init.cc +++ b/libstdc++-v3/src/c++98/locale_init.cc @@ -31,6 +31,7 @@ #include #include // For towupper, etc. #include +#include #include #if _GLIBCXX_USE_DUAL_ABI @@ -321,6 +322,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION void locale::_S_initialize_once() throw() { + // Need to check this because we could get called once from _S_initialize() + // when the program is single-threaded, and then again (via __gthread_once) + // when it's multi-threaded. + if (_S_classic) + return; + // 2 references. // One reference for _S_classic, one for _S_global _S_classic = new (&c_locale_impl) _Impl(2); @@ -332,10 +339,10 @@ _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) + if (__builtin_expect(!_S_classic, 0)) _S_initialize_once(); } -- 2.31.1