From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id DCD073944835; Tue, 24 Nov 2020 14:59:45 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org DCD073944835 Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Jonathan Wakely To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r11-5314] libstdc++: Use __libc_single_threaded for locale initialization X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/master X-Git-Oldrev: 97981e13b7f7b1ffd1c9ccf3d96d574b7b3caada X-Git-Newrev: e253d36214015ed10ffd335e3628ccaac22dd5c7 Message-Id: <20201124145945.DCD073944835@sourceware.org> Date: Tue, 24 Nov 2020 14:59:45 +0000 (GMT) X-BeenThere: libstdc++-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 24 Nov 2020 14:59:46 -0000 https://gcc.gnu.org/g:e253d36214015ed10ffd335e3628ccaac22dd5c7 commit r11-5314-ge253d36214015ed10ffd335e3628ccaac22dd5c7 Author: Jonathan Wakely Date: Tue Nov 24 12:29:30 2020 +0000 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: --- libstdc++-v3/src/c++98/locale.cc | 4 ++-- libstdc++-v3/src/c++98/locale_init.cc | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/libstdc++-v3/src/c++98/locale.cc b/libstdc++-v3/src/c++98/locale.cc index 06422412039..9b3fc351515 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 c3841ccbd3c..fc8416ba01a 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)