public inbox for libstdc++-cvs@sourceware.org help / color / mirror / Atom feed
From: Alexandre Oliva <aoliva@gcc.gnu.org> 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 Date: Fri, 3 Mar 2023 14:59:00 +0000 (GMT) [thread overview] Message-ID: <20230303145900.04F753858005@sourceware.org> (raw) https://gcc.gnu.org/g:f258ef7e44aefac0f732f25dd6a6263780dc7fae commit f258ef7e44aefac0f732f25dd6a6263780dc7fae Author: Alexandre Oliva <oliva@adacore.com> 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. Diff: --- libstdc++-v3/include/bits/std_thread.h | 48 +++++++++++++++++++++++++++++----- 1 file changed, 41 insertions(+), 7 deletions(-) diff --git a/libstdc++-v3/include/bits/std_thread.h b/libstdc++-v3/include/bits/std_thread.h index adbd3928ff7..1ceb11cbed8 100644 --- a/libstdc++-v3/include/bits/std_thread.h +++ b/libstdc++-v3/include/bits/std_thread.h @@ -132,6 +132,46 @@ _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); + } +#else + bool _M_skip_always = false; + asm ("" : "+X" (_M_skip_always)); + if (__builtin_expect (_M_skip_always, false)) + { + pthread_t thr; + pthread_create (&thr, nullptr, nullptr, nullptr); + pthread_join (thr, nullptr); + } +#endif +#endif + } + + public: template<typename _Callable, typename... _Args, typename = _Require<__not_same<_Callable>>> explicit @@ -142,18 +182,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<void(*)()>(&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
next reply other threads:[~2023-03-03 14:59 UTC|newest] Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top 2023-03-03 14:59 Alexandre Oliva [this message] -- strict thread matches above, loose matches on Subject: below -- 2023-03-03 18:47 Alexandre Oliva 2023-03-03 17:36 Alexandre Oliva 2023-03-03 15:05 Alexandre Oliva 2023-03-03 15:04 Alexandre Oliva 2023-03-03 15:01 Alexandre Oliva 2023-03-03 14:57 Alexandre Oliva 2023-03-03 14:45 Alexandre Oliva 2023-03-03 14:42 Alexandre Oliva 2023-03-03 14:40 Alexandre Oliva 2023-03-03 13:11 Alexandre Oliva 2023-03-03 12:39 Alexandre Oliva 2023-03-03 12:37 Alexandre Oliva 2023-03-03 5:23 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=20230303145900.04F753858005@sourceware.org \ --to=aoliva@gcc.gnu.org \ --cc=gcc-cvs@gcc.gnu.org \ --cc=libstdc++-cvs@gcc.gnu.org \ /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: linkBe 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).