public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jonathan Wakely <jwakely@redhat.com>
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	[thread overview]
Message-ID: <20211119202450.502097-1-jwakely@redhat.com> (raw)

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 <cctype>
 #include <cwctype>     // For towupper, etc.
 #include <locale>
+#include <ext/atomicity.h>
 #include <ext/concurrence.h>
 
 #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


                 reply	other threads:[~2021-11-19 20:24 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20211119202450.502097-1-jwakely@redhat.com \
    --to=jwakely@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=libstdc++@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).