gcc/libstdc++-v3/testsuite
Jonathan Wakely 08b4d32571 libstdc++: Avoid calling undefined __gthread_self weak symbol [PR 95989]
Since glibc 2.27 the pthread_self symbol has been defined in libc rather
than libpthread. Because we only call pthread_self through a weak alias
it's possible for statically linked executables to end up without a
definition of pthread_self. This crashes when trying to call an
undefined weak symbol.

We can use the __GLIBC_PREREQ version check to detect the version of
glibc where pthread_self is no longer in libpthread, and call it
directly rather than through the weak reference.

It would be better to check for pthread_self in libc during configure
instead of hardcoding the __GLIBC_PREREQ check. That would be
complicated by the fact that prior to glibc 2.27 libc.a didn't have the
pthread_self symbol, but libc.so.6 did.  The configure checks would need
to try to link both statically and dynamically, and the result would
depend on whether the static libc.a happens to be installed during
configure (which could vary between different systems using the same
version of glibc). Doing it properly is left for a future date, as that
will be needed anyway after glibc moves all pthread symbols from
libpthread to libc. When that happens we should revisit the whole
approach of using weak symbols for pthread symbols.

For the purposes of std::this_thread::get_id() we call
pthread_self() directly when using glibc 2.27 or later. Otherwise, if
__gthread_active_p() is true then we know the libpthread symbol is
available so we call that. Otherwise, we are single-threaded and just
use ((__gthread_t)1) as the thread ID.

An undesirable consequence of this change is that code compiled prior to
the change might inline the old definition of this_thread::get_id()
which always returns (__gthread_t)1 in a program that isn't linked to
libpthread. Code compiled after the change will use pthread_self() and
so get a real TID. That could result in the main thread having different
thread::id values in different translation units. This seems acceptable,
as there are not expected to be many uses of thread::id in programs
that aren't linked to libpthread.

An earlier version of this patch also changed __gthread_self() to use
__GLIBC_PREREQ(2, 27) and only use the weak symbol for older glibc. Tha
might still make sense to do, but isn't needed by libstdc++ now.

libstdc++-v3/ChangeLog:

	PR libstdc++/95989
	* config/os/gnu-linux/os_defines.h (_GLIBCXX_NATIVE_THREAD_ID):
	Define new macro to get reliable thread ID.
	* include/bits/std_thread.h: (this_thread::get_id): Use new
	macro if it's defined.
	* testsuite/30_threads/jthread/95989.cc: New test.
	* testsuite/30_threads/this_thread/95989.cc: New test.
2020-11-19 21:07:06 +00:00
..
17_intro libstdc++: Replace use of reserved name that clashes [PR 97362] 2020-10-10 21:22:12 +01:00
18_support libstdc++: Fix test failure with --disable-linux-futex 2020-11-04 15:30:54 +00:00
19_diagnostics libstdc++: Ensure c++NN effective target present in all C++17 tests 2020-07-31 19:58:02 +01:00
20_util c++: Check abstract type only on object creation. [PR86252] 2020-11-15 12:07:13 -05:00
21_strings libstdc++: Make _GLIBCXX_DEBUG checks constexpr compatible 2020-11-09 21:11:13 +01:00
22_locale libstdc++: Avoid warnings in tests 2020-10-29 22:47:21 +00:00
23_containers libstdc++: Remove <debug/array> 2020-11-09 21:20:01 +01:00
24_iterators libstdc++: Add missing P0896 changes to <iterator> 2020-10-02 10:51:31 -04:00
25_algorithms libstdc++: Fix ranges::search_n for random access iterators [PR97828] 2020-11-17 10:28:20 -05:00
26_numerics libstdc++: Prefer double to long double in std::shuffle_order_engine 2020-10-31 12:54:03 +00:00
27_io libstdc++: Implement std::emit_on_flush etc. 2020-11-11 00:19:40 +00:00
28_regex libstdc++: Ensure c++NN effective-target present in more tests 2020-07-31 19:58:03 +01:00
29_atomics libstdc++: Change test to work without 64-bit atomics 2020-10-02 22:18:51 +01:00
30_threads libstdc++: Avoid calling undefined __gthread_self weak symbol [PR 95989] 2020-11-19 21:07:06 +00:00
abi Update copyright years. 2020-01-01 12:51:42 +01:00
backward Update copyright years. 2020-01-01 12:51:42 +01:00
config Update copyright years. 2020-01-01 12:51:42 +01:00
data
decimal Update copyright years. 2020-01-01 12:51:42 +01:00
experimental libstdc++: Use non-throwing increment in recursive_directory_iterator [PR 97731] 2020-11-05 18:01:26 +00:00
ext libstdc++: Avoid warnings in tests 2020-10-29 22:47:21 +00:00
lib libstdc++: Improve comments for check_effective_target_cxx11-abi 2020-10-14 12:52:47 +01:00
libstdc++-abi Update copyright years. 2020-01-01 12:51:42 +01:00
libstdc++-dg Update copyright years. 2020-01-01 12:51:42 +01:00
libstdc++-prettyprinters libstdc++: Remove inheritance from elements in std::tuple 2020-08-17 15:27:51 +01:00
libstdc++-xmethods Update copyright years. 2020-01-01 12:51:42 +01:00
performance libstdc++: Add performance test for <random> 2020-10-09 14:01:54 +01:00
special_functions Update copyright years. 2020-01-01 12:51:42 +01:00
std libstdc++: Fix ranges::join_view::_Iterator::operator-> [LWG 3500] 2020-11-18 10:23:57 -05:00
tr1 libstdc++: Prevent deprecation warnings from <tr1/shared_ptr> 2020-10-29 22:47:22 +00:00
tr2 Update copyright years. 2020-01-01 12:51:42 +01:00
util libstdc++: Avoid warnings in tests 2020-10-29 22:47:21 +00:00
Makefile.am libstdc++: Adjust variable export in makefile 2020-10-09 14:08:42 +01:00
Makefile.in libstdc++: Adjust variable export in makefile 2020-10-09 14:08:42 +01:00