libstdc++: Use __libc_single_threaded for locale initialization
authorJonathan Wakely <jwakely@redhat.com>
Tue, 24 Nov 2020 12:29:30 +0000 (12:29 +0000)
committerJonathan Wakely <jwakely@redhat.com>
Tue, 24 Nov 2020 14:59:40 +0000 (14:59 +0000)
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.

libstdc++-v3/src/c++98/locale.cc
libstdc++-v3/src/c++98/locale_init.cc

index 06422412039c1bb100b1b7adf5b30e504e57e0e9..9b3fc3515152318ba99783e433132f12d4ac5956 100644 (file)
@@ -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))
              {
index c3841ccbd3c96b4b0c28d0498250a8e8545c0c3b..fc8416ba01a62377718b7a3004973b909fba307b 100644 (file)
@@ -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)