From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2140) id BAFDE3857C4F; Fri, 3 Mar 2023 13:11:31 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BAFDE3857C4F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1677849091; bh=W8m8VwnevDTAQdZPffCY2qDeLxbLkK4ijjE+LOoNXtw=; h=From:To:Subject:Date:From; b=bd2MzrAk44Rt+d4G2rjjGg1hiyfFpUtaF8x2Nzj8zqN0kqzMgTWnWqKBj+GpsNh4f bocWBVUJaH4oYeAHGWIqTd1+LCxKOGgMXcyaKjwg0PSxlDZfmP5BK35YDBElQ3k0Rr StYkNbRZC/+gSTGkquu42LBppzkmjxKb9CV2pD4Y= 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: 6f67ec05d6e1aad9b01a634575b6a10fae5ea6bc Message-Id: <20230303131131.BAFDE3857C4F@sourceware.org> Date: Fri, 3 Mar 2023 13:11:31 +0000 (GMT) List-Id: https://gcc.gnu.org/g:6f67ec05d6e1aad9b01a634575b6a10fae5ea6bc commit 6f67ec05d6e1aad9b01a634575b6a10fae5ea6bc 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 (std::thread ctor): Add strong reference to pthread_join. Diff: --- libstdc++-v3/include/bits/std_thread.h | 45 ++++++++++++++++++++++++++++------ 1 file changed, 38 insertions(+), 7 deletions(-) diff --git a/libstdc++-v3/include/bits/std_thread.h b/libstdc++-v3/include/bits/std_thread.h index adbd3928ff7..69b1c6cc5ad 100644 --- a/libstdc++-v3/include/bits/std_thread.h +++ b/libstdc++-v3/include/bits/std_thread.h @@ -132,6 +132,43 @@ _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() { +#ifdef GTHR_ACTIVE_PROXY +#if 1 + static auto const __attribute__ ((__used__)) _M_create = pthread_create; + static auto const __attribute__ ((__used__)) _M_join = pthread_join; +#elif 0 + asm goto ("" : : : : _M_never_run); + if (0) + { + _M_never_run: + pthread_create (nullptr, nullptr, nullptr, nullptr); + pthread_join (nullptr, nullptr); + } +#else + int _M_skip_always = 0; + asm ("" : "+X" (_M_skip_always)); + if (__builtin_expect (_M_skip_always, 0)) + { + // This never runs. + if (_M_skip_always > 0) + pthread_create (nullptr, nullptr, nullptr, nullptr); + else + pthread_join (nullptr, nullptr); + } +#endif +#endif + } + + public: template>> explicit @@ -142,18 +179,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); } #endif // _GLIBCXX_HAS_GTHREADS