public inbox for gcc-bugs@sourceware.org help / color / mirror / Atom feed
From: "redi at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org> To: gcc-bugs@gcc.gnu.org Subject: [Bug libstdc++/98108] Broken Schwarz counter for iostreams initialization Date: Wed, 09 Dec 2020 15:52:08 +0000 [thread overview] Message-ID: <bug-98108-4-XFNITdHZea@http.gcc.gnu.org/bugzilla/> (raw) In-Reply-To: <bug-98108-4@http.gcc.gnu.org/bugzilla/> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98108 --- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> --- I think it's QoI whether this works. "The objects are constructed and the associations are established at some time prior to or during the first time an object of class ios_base::Init is constructed, ..." "The results of including <iostream> in a translation unit shall be as if <iostream> defined an instance of ios_base::Init with static storage duration." So there's no guarantee that the global iostreams will be usable during the static initialization phase, because there's no guarantee that a given constructor will be before the first ios_base::Init constructor. The problem is *not* that the counters use atomics rather than waiting. Even if the initialization is done as in the patch below, it still crashes: --- a/libstdc++-v3/src/c++98/ios_init.cc +++ b/libstdc++-v3/src/c++98/ios_init.cc @@ -77,7 +77,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ios_base::Init::Init() { - if (__gnu_cxx::__exchange_and_add_dispatch(&_S_refcount, 1) == 0) + struct do_init + { + do_init() { // Standard streams default to synced with "C" operations. _S_synced_with_stdio = true; @@ -116,8 +118,15 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION // streams are not re-initialized with uses of ios_base::Init // besides <iostream> static object, ie just using <ios> with // ios_base::Init objects. - __gnu_cxx::__atomic_add_dispatch(&_S_refcount, 1); + _S_refcount = 1; } + }; +#ifndef __cpp_threadsafe_static_init +# warning "ios_base::Init::Init() initialization is not thread-safe" +#endif + static const do_init once __attribute__((unused)) = do_init(); + + __gnu_cxx::__exchange_and_add_dispatch(&_S_refcount, 1); } ios_base::Init::~Init() This doesn't help, because the thread1 and thread2 objects in file1.cc are still constructed before the static ios_base::Init object in file2.cc and so they still try to use std::cout before the first _S_refcount increment has happened. Waiting for the iostream initialization to finish doesn't help if it hasn't started yet. And isn't relevant in your program, because only one ios_base::Init constructor ever runs in the whole program. Making the second one wait for the first doesn't help if there is only one. What does make your program work is: --- a/libstdc++-v3/include/std/iostream +++ b/libstdc++-v3/include/std/iostream @@ -71,7 +71,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION //@} // For construction of filebuffers for cout, cin, cerr, clog et. al. - static ios_base::Init __ioinit; + static ios_base::Init __ioinit __attribute__((__init_priority__(50))); _GLIBCXX_END_NAMESPACE_VERSION } // namespace And this works with or without changing the Schwarz counter.
next prev parent reply other threads:[~2020-12-09 15:52 UTC|newest] Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top 2020-12-02 21:32 [Bug libstdc++/98108] New: " i.hamsa at gmail dot com 2020-12-03 7:28 ` [Bug libstdc++/98108] " rguenth at gcc dot gnu.org 2020-12-03 9:19 ` i.hamsa at gmail dot com 2020-12-09 15:52 ` redi at gcc dot gnu.org [this message] 2020-12-15 11:45 ` cvs-commit at gcc dot gnu.org 2020-12-15 11:53 ` redi at gcc dot gnu.org 2020-12-15 18:42 ` cvs-commit at gcc dot gnu.org 2020-12-16 16:16 ` redi at gcc dot gnu.org 2020-12-16 17:41 ` i.hamsa at gmail dot com 2020-12-16 23:32 ` redi at gcc dot gnu.org 2022-11-06 16:16 ` cvs-commit at gcc dot gnu.org
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=bug-98108-4-XFNITdHZea@http.gcc.gnu.org/bugzilla/ \ --to=gcc-bugzilla@gcc.gnu.org \ --cc=gcc-bugs@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: linkBe 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).