From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 147BE3858D37 for ; Fri, 3 Mar 2023 09:48:47 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 147BE3858D37 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1677836926; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=0ZFZteD5OoJgpBwIApg3E1afIpNfdpnpa5AR9NLyd+w=; b=PPa5sExbYEVMtGICemrKDio+LaWC29SAdWUpGVXZqrIRQgIlaVMWJeM4wpc0wkn5JkP7ai csyAK/BrY0l+IATQ8ndeSeWKOGVyhtqICrQza+Ycq7fJfsqtjIONR5IQQbpbXF3PUUDgZG sOEz7AeoIhWTMiO2LwLhrb6F2XhoQ/c= Received: from mail-lj1-f197.google.com (mail-lj1-f197.google.com [209.85.208.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-592-DfZjznbSNFCwXD5h1Hwftg-1; Fri, 03 Mar 2023 04:48:45 -0500 X-MC-Unique: DfZjznbSNFCwXD5h1Hwftg-1 Received: by mail-lj1-f197.google.com with SMTP id v10-20020a2e87ca000000b00290658792ceso458127ljj.4 for ; Fri, 03 Mar 2023 01:48:45 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=0ZFZteD5OoJgpBwIApg3E1afIpNfdpnpa5AR9NLyd+w=; b=LXSr2ukZYIuVCxXDNwKA1kDJvYqxT3IT4dkCXrjdeyO51WXWY4h0IRBLxhh0Ju9eva R8jPd/OvxpcTkwHOAVo4UmpdxdkzYEEM3OMWUMIz3GFUmJL/sWSrQ06LgUD1pE8OH3O2 sfKYewlGP4dqfna2TRcOKFZP1ylkhDt36vrQ3dXvdnzWJ1a73QQZ5axC3DZbsD8W2mFF a3+isnBryxsYAiX7usyTI/4iiY+RfvVX3gy3/K+HgvMUx9aegCsbppKIDjy+oHIZYpWG SwooRWMWIm5fJbBWt9/clf2ufDswVEqAYY3YBNqBOnDq6QCCvCR10XWqpymkGVi0c7d5 yGeg== X-Gm-Message-State: AO0yUKXwQenuqlli9WuhPM46h0Tp/qlWRncxMRvqXEkITKF5USA8x7gw qq3l/5ZtIeSE6+BC6RbMZLiikCEEpqQhxTi+ziZ8tcsUfeojrWv4cfeulaC6hq3pHTbyIPycIy5 U39nQd3XZ4gt7Cqk/TQJa3bj7sbyz+3vnNQ== X-Received: by 2002:ac2:5e8e:0:b0:4d5:ca32:9bdc with SMTP id b14-20020ac25e8e000000b004d5ca329bdcmr439857lfq.8.1677836923911; Fri, 03 Mar 2023 01:48:43 -0800 (PST) X-Google-Smtp-Source: AK7set9nE/hNjrtyBPEccpOrPfREEjwvgERjMsnfEtbqPKztmilpmA5UlS0RwMhbRuTxzFKXRJ9zAGLY8aOWb4A7cco= X-Received: by 2002:ac2:5e8e:0:b0:4d5:ca32:9bdc with SMTP id b14-20020ac25e8e000000b004d5ca329bdcmr439849lfq.8.1677836923552; Fri, 03 Mar 2023 01:48:43 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: Jonathan Wakely Date: Fri, 3 Mar 2023 09:48:32 +0000 Message-ID: Subject: Re: [libstdc++] Use __gthread_join in jthread/95989 To: Alexandre Oliva Cc: Jonathan Wakely , gcc-patches , "libstdc++" , Bernd Edlinger X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: multipart/alternative; boundary="000000000000d1d58b05f5fbd92d" X-Spam-Status: No, score=-12.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,HTML_MESSAGE,KAM_NUMSUBJECT,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: --000000000000d1d58b05f5fbd92d Content-Type: text/plain; charset="UTF-8" On Fri, 3 Mar 2023 at 09:33, Jonathan Wakely wrote: > > > On Fri, 3 Mar 2023 at 08:20, Alexandre Oliva via Libstdc++ < > libstdc++@gcc.gnu.org> wrote: > >> On Feb 17, 2023, Jonathan Wakely 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(&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 >> >> --000000000000d1d58b05f5fbd92d--