From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from rock.gnat.com (rock.gnat.com [205.232.38.15]) by sourceware.org (Postfix) with ESMTPS id EB44D3858D28; Fri, 3 Mar 2023 19:55:39 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org EB44D3858D28 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=adacore.com Received: from localhost (localhost.localdomain [127.0.0.1]) by filtered-rock.gnat.com (Postfix) with ESMTP id B1F70116590; Fri, 3 Mar 2023 14:55:39 -0500 (EST) X-Virus-Scanned: Debian amavisd-new at gnat.com Received: from rock.gnat.com ([127.0.0.1]) by localhost (rock.gnat.com [127.0.0.1]) (amavisd-new, port 10024) with LMTP id XIV7hAyGmbFK; Fri, 3 Mar 2023 14:55:39 -0500 (EST) Received: from free.home (tron.gnat.com [IPv6:2620:20:4000:0:46a8:42ff:fe0e:e294]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by rock.gnat.com (Postfix) with ESMTPS id 21BCB116572; Fri, 3 Mar 2023 14:55:38 -0500 (EST) Received: from livre (livre.home [172.31.160.2]) by free.home (8.15.2/8.15.2) with ESMTPS id 323JtTfd609780 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Fri, 3 Mar 2023 16:55:30 -0300 From: Alexandre Oliva To: Jonathan Wakely Cc: Jonathan Wakely , gcc-patches , "libstdc++" , Bernd Edlinger Subject: Re: [libstdc++] Use __gthread_join in jthread/95989 Organization: Free thinker, does not speak for AdaCore References: Errors-To: aoliva@lxoliva.fsfla.org Date: Fri, 03 Mar 2023 16:55:29 -0300 In-Reply-To: (Jonathan Wakely's message of "Fri, 3 Mar 2023 18:14:20 +0000") Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.3 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain X-Scanned-By: MIMEDefang 2.84 X-Spam-Status: No, score=-12.0 required=5.0 tests=BAYES_00,GIT_PATCH_0,KAM_DMARC_STATUS,KAM_NUMSUBJECT,SPF_HELO_NONE,SPF_PASS,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: On Mar 3, 2023, Jonathan Wakely wrote: > On Fri, 3 Mar 2023 at 18:12, Jonathan Wakely wrote: >> And those expressions aren't ever optimized away as unused? > Oh, I missed that they're called after casting them *nod* > That would be UB to call them through the wrong pointer type, so the > compiler could decide they're unreachable Ugh, indeed. We can drop the cast and add the required parameters if it ever becomes an issue. Here's what I've just installed. 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 PR libstdc++/104852 PR libstdc++/95989 PR libstdc++/52590 * include/bits/std_thread.h (thread::_M_thread_deps): New static implicitly-inline member function. (std::thread template ctor): Pass it to _M_start_thread. * src/c++11/thread.cc (thread::_M_start_thread): Name depend parameter, force it live on entry. --- libstdc++-v3/include/bits/std_thread.h | 24 +++++++++++++++++------- libstdc++-v3/src/c++11/thread.cc | 10 ++++++++-- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/libstdc++-v3/include/bits/std_thread.h b/libstdc++-v3/include/bits/std_thread.h index adbd3928ff783..e88c07fbd4f0f 100644 --- a/libstdc++-v3/include/bits/std_thread.h +++ b/libstdc++-v3/include/bits/std_thread.h @@ -132,6 +132,22 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION thread() noexcept = default; #ifdef _GLIBCXX_HAS_GTHREADS + private: + // This adds to user code that creates std:thread objects (because + // it is called by the template ctor below) strong references to + // pthread_create and pthread_join, which ensures they are both + // linked in even during static linking. We can't depend on + // gthread calls to bring them in, because those may use weak + // references. + static void + _M_thread_deps_never_run() { +#ifdef GTHR_ACTIVE_PROXY + reinterpret_cast(&pthread_create)(); + reinterpret_cast(&pthread_join)(); +#endif + } + + public: template>> explicit @@ -142,18 +158,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION "std::thread arguments must be invocable after conversion to rvalues" ); -#ifdef GTHR_ACTIVE_PROXY - // Create a reference to pthread_create, not just the gthr weak symbol. - auto __depend = reinterpret_cast(&pthread_create); -#else - auto __depend = nullptr; -#endif using _Wrapper = _Call_wrapper<_Callable, _Args...>; // Create a call wrapper with DECAY_COPY(__f) as its target object // and DECAY_COPY(__args)... as its bound argument entities. _M_start_thread(_State_ptr(new _State_impl<_Wrapper>( std::forward<_Callable>(__f), std::forward<_Args>(__args)...)), - __depend); + _M_thread_deps_never_run); } #endif // _GLIBCXX_HAS_GTHREADS diff --git a/libstdc++-v3/src/c++11/thread.cc b/libstdc++-v3/src/c++11/thread.cc index 2d5ffaf678e97..c91f7b02e1f3f 100644 --- a/libstdc++-v3/src/c++11/thread.cc +++ b/libstdc++-v3/src/c++11/thread.cc @@ -154,8 +154,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION } void - thread::_M_start_thread(_State_ptr state, void (*)()) + thread::_M_start_thread(_State_ptr state, void (*depend)()) { + // Make sure it's not optimized out, not even with LTO. + asm ("" : : "rm" (depend)); + if (!__gthread_active_p()) { #if __cpp_exceptions @@ -190,8 +193,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION } void - thread::_M_start_thread(__shared_base_type __b, void (*)()) + thread::_M_start_thread(__shared_base_type __b, void (*depend)()) { + // Make sure it's not optimized out, not even with LTO. + asm ("" : : "rm" (depend)); + auto ptr = __b.get(); // Create a reference cycle that will be broken in the new thread. ptr->_M_this_ptr = std::move(__b); -- 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