[libstdc++] Use __gthread_join in jthread/95989

Alexandre Oliva oliva@adacore.com
Fri Mar 3 08:18:33 GMT 2023


On Feb 17, 2023, Jonathan Wakely <jwakely.gcc@gmail.com> 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.)


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<void(*)()>(&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 <https://stallmansupport.org>


More information about the Libstdc++ mailing list