From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2140) id B59E93858416; Fri, 3 Mar 2023 17:36:42 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B59E93858416 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1677865002; bh=O7Iqb4MlaUf5VlfzxjJSw795mITCWFRAOXZpc3WlS/M=; h=From:To:Subject:Date:From; b=ve909F6v/Fx404SIn7ZMLUSVoCmFdORcf9k7if5X/ESrYr0y2RqGodSz4i/+8m0fa COcawcYhddfvTuCN6276NvYkIdwejftvNKTi7G2dkBOSRSMHi0VO1zinDHXNn2v930 nyj5wwQtKYORSmKdGXAe8WE084apCvZSGUHm3SWQ= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Alexandre Oliva To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc(refs/users/aoliva/heads/testme)] link pthread_join from std::thread ctor X-Act-Checkin: gcc X-Git-Author: Alexandre Oliva X-Git-Refname: refs/users/aoliva/heads/testme X-Git-Oldrev: 1c8edeb6ea81dfb571eb32de2a2569764ba47185 X-Git-Newrev: 2a01a08b7dfb4102d5f86149536e5fc238b3e53d Message-Id: <20230303173642.B59E93858416@sourceware.org> Date: Fri, 3 Mar 2023 17:36:42 +0000 (GMT) List-Id: https://gcc.gnu.org/g:2a01a08b7dfb4102d5f86149536e5fc238b3e53d commit 2a01a08b7dfb4102d5f86149536e5fc238b3e53d Author: Alexandre Oliva Date: Fri Mar 3 02:14:53 2023 -0300 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 (thread::_M_thread_deps): New static inline function. (std::thread template ctor): Pass it to _M_start_thread. * sr/c++11/thread.cc (thread::_M_start_thread): Name depend parameter, force it live on entry. Diff: --- libstdc++-v3/include/bits/std_thread.h | 51 +++++++++++++++++++++++++++++----- libstdc++-v3/src/c++11/thread.cc | 10 +++++-- 2 files changed, 52 insertions(+), 9 deletions(-) diff --git a/libstdc++-v3/include/bits/std_thread.h b/libstdc++-v3/include/bits/std_thread.h index adbd3928ff7..3ffd2a823a6 100644 --- a/libstdc++-v3/include/bits/std_thread.h +++ b/libstdc++-v3/include/bits/std_thread.h @@ -132,6 +132,49 @@ _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 +#if 0 + static auto const __attribute__ ((__used__)) _M_create = pthread_create; + static auto const __attribute__ ((__used__)) _M_join = pthread_join; +#elif 0 + pthread_t thr; + pthread_create (&thr, nullptr, nullptr, nullptr); + pthread_join (thr, nullptr); +#elif 0 + asm goto ("" : : : : _M_never_run); + if (0) + { + _M_never_run: + pthread_t thr; + pthread_create (&thr, nullptr, nullptr, nullptr); + pthread_join (thr, nullptr); + } +#elif 0 + bool _M_skip_always = false; + asm ("" : "+rm" (_M_skip_always)); + if (__builtin_expect (_M_skip_always, false)) + { + pthread_t thr; + pthread_create (&thr, nullptr, nullptr, nullptr); + pthread_join (thr, nullptr); + } +#else + reinterpret_cast(&pthread_create)(); + reinterpret_cast(&pthread_join)(); +#endif +#endif + } + + public: template>> explicit @@ -142,18 +185,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 2d5ffaf678e..c91f7b02e1f 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);