public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jonathan Wakely <jwakely@redhat.com>
To: Alexandre Oliva <oliva@adacore.com>
Cc: Jonathan Wakely <jwakely.gcc@gmail.com>,
	gcc-patches <gcc-patches@gcc.gnu.org>,
	 "libstdc++" <libstdc++@gcc.gnu.org>,
	Bernd Edlinger <bernd.edlinger@hotmail.de>
Subject: Re: [libstdc++] Use __gthread_join in jthread/95989
Date: Fri, 3 Mar 2023 09:48:32 +0000	[thread overview]
Message-ID: <CACb0b4k45RJoiFF38ZeDamQ5sasXCO6oQ4eH_=4SmfK0DhxFHA@mail.gmail.com> (raw)
In-Reply-To: <CACb0b4nXs3haiCsxCAVBnfgRjVPxj60YZwv6mOPd7Uw+d-MdPw@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 3154 bytes --]

On Fri, 3 Mar 2023 at 09:33, Jonathan Wakely <jwakely@redhat.com> wrote:

>
>
> On Fri, 3 Mar 2023 at 08:20, Alexandre Oliva via Libstdc++ <
> libstdc++@gcc.gnu.org> wrote:
>
>> On Feb 17, 2023, Jonathan Wakely <jwakely.gcc@gmail.com> wrote:
>>
>> > The proper fix is to ensure the program has a non-weak reference to
>> > pthread_join without extra help (or use a recent glibc where it always
>> > works).
>>
>> Indeed!  How about this?  Regstrapped on x86_64-linux-gnu, also tested
>> on arm-vx7r2 (gcc-12); verified that strong references are present in
>> 95989.o, but not in libstdc++.a or .so.  Ok to install?
>>
>> (nit: the static local in a ctor of a template class may make for
>> multiple copies.  Maybe a non-template always-inline function called by
>> all instantiations would be better.)
>>
>
> Yeah, that does seem less than ideal.
>
> Jakub previously suggested doing this for PR 61841, which was a similar
> problem with pthread_create:
>
> __asm ("" : : "r" (&pthread_create)); would not be optimized away.
>
>
> That would avoid the multiple copies.
>

As Jakub pointed out, it adds a scheduling barrier, but a few cycles when
creating a new thread is probably not even measurable.


>
> Alternatively we could get really creative and cast the addresses of both
> &pthread_create and &pthread_join to uintptr_t and XOR them, and pass that
> as the __depend argument (which is never actually dereferenced, it's only
> there to create a link-time dependency).
>

I should be clear that I don't think the creative solution is a good idea.
And if we ever support building libstdc++ with LTO it will become visible
that the argument is unused, and we'd need something like the asm
dependency anyway.



>
>
>
>>
>>
>> link pthread_join from std::thread ctor
>>
>> Like pthread_create, pthread_join may fail to be statically linked in
>> absent strong uses, so add to user code strong references to both when
>> std::thread objects are created.
>>
>>
>> for  libstdc++-v3/ChangeLog
>>
>>         * include/bits/std_thread.h (std::thread ctor): Add strong
>>         reference to pthread_join.
>>
>> ---
>>  libstdc++-v3/include/bits/std_thread.h |    1 +
>>  1 file changed, 1 insertion(+)
>>
>> diff --git a/libstdc++-v3/include/bits/std_thread.h
>> b/libstdc++-v3/include/bits/std_thread.h
>> index adbd3928ff783..4bda350fa2c7b 100644
>> --- a/libstdc++-v3/include/bits/std_thread.h
>> +++ b/libstdc++-v3/include/bits/std_thread.h
>> @@ -145,6 +145,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
>>  #ifdef GTHR_ACTIVE_PROXY
>>         // Create a reference to pthread_create, not just the gthr weak
>> symbol.
>>         auto __depend = reinterpret_cast<void(*)()>(&pthread_create);
>> +       static auto __attribute__((__used__)) __depend_join =
>> &pthread_join;
>>  #else
>>         auto __depend = nullptr;
>>  #endif
>>
>>
>> --
>> Alexandre Oliva, happy hacker                https://FSFLA.org/blogs/lxo/
>>    Free Software Activist                       GNU Toolchain Engineer
>> Disinformation flourishes because many people care deeply about injustice
>> but very few check the facts.  Ask me about <https://stallmansupport.org>
>>
>>

  reply	other threads:[~2023-03-03  9:48 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-17  6:39 Alexandre Oliva
2023-02-17 10:58 ` Jonathan Wakely
2023-03-03  8:18   ` Alexandre Oliva
2023-03-03  9:33     ` Jonathan Wakely
2023-03-03  9:48       ` Jonathan Wakely [this message]
2023-03-03 17:46         ` Alexandre Oliva
2023-03-03 18:12           ` Jonathan Wakely
2023-03-03 18:14             ` Jonathan Wakely
2023-03-03 19:55               ` Alexandre Oliva
2023-03-03 20:15                 ` Florian Weimer
2023-03-03 20:36                   ` Alexandre Oliva
2023-03-03 20:57                     ` Alexandre Oliva

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='CACb0b4k45RJoiFF38ZeDamQ5sasXCO6oQ4eH_=4SmfK0DhxFHA@mail.gmail.com' \
    --to=jwakely@redhat.com \
    --cc=bernd.edlinger@hotmail.de \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jwakely.gcc@gmail.com \
    --cc=libstdc++@gcc.gnu.org \
    --cc=oliva@adacore.com \
    /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).