public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
From: Stephan Bergmann <sbergman@redhat.com>
To: Jonathan Wakely <jwakely@redhat.com>,
	libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org
Subject: Re: [committed] libstdc++: Use __libc_single_threaded for locale initialization
Date: Fri, 27 Nov 2020 09:43:46 +0100	[thread overview]
Message-ID: <f953ec50-4425-7c01-7555-ff304968796d@redhat.com> (raw)
In-Reply-To: <20201124145952.GA1857606@redhat.com>

On 24/11/2020 15:59, Jonathan Wakely via Gcc-patches wrote:
> 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.

I now started to get weird crashes when running LibreOffice test code at 
least with Clang -fsanitize=address and latest libstdc++, where the 
Clang ASan machinery SEGVs while it wants to report some malloc/free 
issue.  That goes away when reverting this commit, and I think the root 
cause is that locale::facet::_S_initialize_once() now gets called twice: 
  First during __cxx_global_var_init when the process is still single 
threaded (so

   if (!__gnu_cxx::__is_single_threaded())

in locale::facet::_S_get_c_locale, reading __libc_single_threaded, is 
false, whereas

   if (__gthread_active_p())

would have been true even if the process still only had a single 
thread).  And again after the process has spawned further threads via 
pthread_create (flipping __libc_single_threaded) and calls 
std::ostringstream() -> ... std::locale() -> ..., at which point

   if (!__gnu_cxx::__is_single_threaded())

in locale::facet::_S_get_c_locale is true now.


  reply	other threads:[~2020-11-27  8:43 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-24 14:59 Jonathan Wakely
2020-11-27  8:43 ` Stephan Bergmann [this message]
2020-11-27 11:15   ` Jonathan Wakely
2020-11-27 12:26     ` Jonathan Wakely

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=f953ec50-4425-7c01-7555-ff304968796d@redhat.com \
    --to=sbergman@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jwakely@redhat.com \
    --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).