gcc/libstdc++-v3/include/bits
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
..
algorithmfwd.h Update copyright years. 2020-01-01 12:51:42 +01:00
align.h libstdc++: Fix overflow handling in std::align 2020-09-22 17:49:48 +01:00
alloc_traits.h libstdc++: Add remaining C++20 additions to <sstream> [P0408R7] 2020-11-10 19:22:47 +00:00
allocated_ptr.h Update copyright years. 2020-01-01 12:51:42 +01:00
allocator.h libstdc++: Avoid errors in allocator's noexcept-specifier (PR 89510) 2020-04-30 16:01:43 +01:00
atomic_base.h libstdc++: Add unused attributes to suppress warnings 2020-09-10 17:09:15 +01:00
atomic_futex.h libstdc++: Use correct duration for atomic_futex wait on custom clock [PR 91486] 2020-10-05 11:32:10 +01:00
basic_ios.h Update copyright years. 2020-01-01 12:51:42 +01:00
basic_ios.tcc Update copyright years. 2020-01-01 12:51:42 +01:00
basic_string.h libstdc++: Define some std::string constructors inline 2020-10-14 12:52:47 +01:00
basic_string.tcc libstdc++: Define some std::string constructors inline 2020-10-14 12:52:47 +01:00
boost_concept_check.h libstdc++: Make _GLIBCXX_CONCEPT_CHECKS more constexpr-friendly 2020-02-27 10:52:28 +00:00
c++0x_warning.h Update copyright years. 2020-01-01 12:51:42 +01:00
c++config libstdc++: Use __is_same instead of __is_same_as 2020-09-30 18:41:12 +01:00
char_traits.h libstdc++: Update value of __cpp_lib_constexpr_char_traits for C++20 2020-06-15 14:31:26 +01:00
charconv.h Update copyright years. 2020-01-01 12:51:42 +01:00
codecvt.h libstdc++: Fix some -Wunused-parameter warnings 2020-09-10 17:09:16 +01:00
concept_check.h Update copyright years. 2020-01-01 12:51:42 +01:00
cpp_type_traits.h libstdc++: Define type traits for wchar_t even when libc support missing 2020-11-01 11:39:07 +00:00
deque.tcc libstdc++: Fix three-way comparison for std::array [PR 96851] 2020-09-02 15:32:11 +01:00
enable_special_members.h Update copyright years. 2020-01-01 12:51:42 +01:00
erase_if.h libstdc++: Update __cpp_lib_erase_if macro (P1115R3) 2020-02-15 10:25:23 +00:00
forward_list.h libstdc++: Add comparison operators to sequence containers 2020-04-17 23:41:04 +01:00
forward_list.tcc libstdc++: Implement DR 526 on [forward_]list remove_if/unique [PR 91620] 2020-08-11 21:30:05 +02:00
fs_dir.h libstdc++: Fix -Wmismatched-tags warnings (PR 96063) 2020-07-06 17:12:29 +01:00
fs_fwd.h libstdc++: Add comparison operators to std::filesystem types 2020-04-15 21:01:42 +01:00
fs_ops.h Update copyright years. 2020-01-01 12:51:42 +01:00
fs_path.h libstdc++: Add noexcept to declaration of path::_List members 2020-10-27 14:50:37 +00:00
fstream.tcc libstdc++: Override BUFSIZ for Windows targets [PR 94268] 2020-10-28 13:19:21 +00:00
functexcept.h libstdc++: Make allocators throw bad_array_new_length on overflow [LWG 3190] 2020-10-05 15:18:56 +01:00
functional_hash.h Update copyright years. 2020-01-01 12:51:42 +01:00
gslice_array.h Update copyright years. 2020-01-01 12:51:42 +01:00
gslice.h Update copyright years. 2020-01-01 12:51:42 +01:00
hashtable_policy.h libstdc++: Use double for unordered container load factors [PR 96958] 2020-10-31 00:52:57 +00:00
hashtable.h libstdc++: Refactor _Hashtable to prepare for custom pointers 2020-10-20 21:30:22 +02:00
indirect_array.h Update copyright years. 2020-01-01 12:51:42 +01:00
invoke.h Update copyright years. 2020-01-01 12:51:42 +01:00
ios_base.h libstdc++: Add C++11 member functions for ios::failure in old ABI 2020-10-08 14:45:37 +01:00
istream.tcc libstdc++: Do not set eofbit eagerly in operator>>(istream&, char(&)[N]) 2020-08-06 19:23:14 +01:00
iterator_concepts.h libstdc++: Introduce new headers for C++20 ranges components 2020-09-22 15:45:54 +01:00
list.tcc libstdc++: Implement DR 526 on [forward_]list remove_if/unique [PR 91620] 2020-08-11 21:30:05 +02:00
locale_classes.h libstdc++: Add comparison operators to types from Utilities clause 2020-04-15 19:47:48 +01:00
locale_classes.tcc Update copyright years. 2020-01-01 12:51:42 +01:00
locale_conv.h libstdc++: Fix -Wsign-compare warnings in headers 2020-10-27 16:32:53 +00:00
locale_facets_nonio.h Update copyright years. 2020-01-01 12:51:42 +01:00
locale_facets_nonio.tcc libstdc++: Fix -Wsign-compare warnings in headers 2020-10-27 16:32:53 +00:00
locale_facets.h Update copyright years. 2020-01-01 12:51:42 +01:00
locale_facets.tcc Update copyright years. 2020-01-01 12:51:42 +01:00
localefwd.h libstdc++: Fix -Wmismatched-tags warnings (PR 96063) 2020-07-06 17:12:29 +01:00
mask_array.h Update copyright years. 2020-01-01 12:51:42 +01:00
max_size_type.h libstdc++: integer-class types as per [iterator.concept.winc] 2020-08-19 09:12:55 -04:00
memoryfwd.h Update copyright years. 2020-01-01 12:51:42 +01:00
move.h Update copyright years. 2020-01-01 12:51:42 +01:00
node_handle.h libstdc++: Optimize container node-handle type for size 2020-10-19 18:06:51 +01:00
ostream_insert.h Update copyright years. 2020-01-01 12:51:42 +01:00
ostream.tcc Update copyright years. 2020-01-01 12:51:42 +01:00
parse_numbers.h libstdc++: Fix some warnings in headers 2020-10-29 22:47:22 +00:00
postypes.h Update copyright years. 2020-01-01 12:51:42 +01:00
predefined_ops.h Update copyright years. 2020-01-01 12:51:42 +01:00
ptr_traits.h libstdc++: Add missing feature test macros 2020-04-22 22:54:34 +01:00
quoted_string.h Update copyright years. 2020-01-01 12:51:42 +01:00
random.h libstdc++: Prefer double to long double in std::shuffle_order_engine 2020-10-31 12:54:03 +00:00
random.tcc libstdc++: Prefer double to long double in std::shuffle_order_engine 2020-10-31 12:54:03 +00:00
range_access.h libstdc++: Introduce new headers for C++20 ranges components 2020-09-22 15:45:54 +01:00
ranges_algo.h libstdc++: Fix ranges::search_n for random access iterators [PR97828] 2020-11-17 10:28:20 -05:00
ranges_algobase.h libstdc++: Introduce new headers for C++20 ranges components 2020-09-22 15:45:54 +01:00
ranges_base.h libstdc++: Introduce new headers for C++20 ranges components 2020-09-22 15:45:54 +01:00
ranges_cmp.h libstdc++: Introduce new headers for C++20 ranges components 2020-09-22 15:45:54 +01:00
ranges_uninitialized.h libstdc++: Make ranges::construct_at constexpr-friendly [PR95788] 2020-10-08 18:10:05 -04:00
ranges_util.h libstdc++: Avoid narrowing conversion in subrange constructor 2020-10-29 22:47:22 +00:00
refwrap.h libstdc++: Avoid using sizeof with function types (PR 93470) 2020-01-28 13:24:09 +00:00
regex_automaton.h Update copyright years. 2020-01-01 12:51:42 +01:00
regex_automaton.tcc Update copyright years. 2020-01-01 12:51:42 +01:00
regex_compiler.h Update copyright years. 2020-01-01 12:51:42 +01:00
regex_compiler.tcc libstdc++: Fix some warnings in headers 2020-10-29 11:43:55 +00:00
regex_constants.h Update copyright years. 2020-01-01 12:51:42 +01:00
regex_error.h libstdc++: Fix build error in <bits/regex_error.h> 2020-09-11 14:52:40 +01:00
regex_executor.h Update copyright years. 2020-01-01 12:51:42 +01:00
regex_executor.tcc Update copyright years. 2020-01-01 12:51:42 +01:00
regex_scanner.h Update copyright years. 2020-01-01 12:51:42 +01:00
regex_scanner.tcc Update copyright years. 2020-01-01 12:51:42 +01:00
regex.h libstdc++: Fix some warnings in headers 2020-10-29 11:43:55 +00:00
regex.tcc Update copyright years. 2020-01-01 12:51:42 +01:00
shared_ptr_atomic.h Update copyright years. 2020-01-01 12:51:42 +01:00
shared_ptr_base.h libstdc++: Fix some warnings in headers 2020-10-29 11:43:55 +00:00
shared_ptr.h libstdc++: Reduce header dependencies in and on <memory> 2020-10-22 18:42:03 +01:00
slice_array.h libstdc++: Add comparison operators to types from Numerics clause 2020-04-08 16:51:59 +01:00
specfun.h Update copyright years. 2020-01-01 12:51:42 +01:00
sstream.tcc libstdc++: Fix -Wsign-compare warnings in headers 2020-10-27 16:32:53 +00:00
std_abs.h Update copyright years. 2020-01-01 12:51:42 +01:00
std_function.h libstdc++: Include <typeinfo> even for -fno-rtti [PR 97758] 2020-11-09 14:28:37 +00:00
std_mutex.h Update copyright years. 2020-01-01 12:51:42 +01:00
std_thread.h libstdc++: Avoid calling undefined __gthread_self weak symbol [PR 95989] 2020-11-19 21:07:06 +00:00
stl_algo.h libstdc++: Define new C++17 std::search overload for Parallel Mode [PR 94971] 2020-11-04 13:36:32 +00:00
stl_algobase.h libstdc++: Fix documentation for return values of copy algos 2020-10-12 18:14:01 +01:00
stl_bvector.h libstdc++: Fix and improve std::vector<bool> implementation. 2020-07-31 23:18:51 +02:00
stl_construct.h libstdc++: Fix regression in std::_Construct (PR 94831) 2020-04-28 23:39:38 +01:00
stl_deque.h libstdc++: Make self-move well-defined for containers [PR 85828] 2020-08-12 20:36:00 +01:00
stl_function.h Update copyright years. 2020-01-01 12:51:42 +01:00
stl_heap.h Update copyright years. 2020-01-01 12:51:42 +01:00
stl_iterator_base_funcs.h Update copyright years. 2020-01-01 12:51:42 +01:00
stl_iterator_base_types.h libstdc++: Define __cpp_lib_ranges macro for C++20 2020-03-27 23:26:03 +00:00
stl_iterator.h libstdc++: Add missing P0896 changes to <iterator> 2020-10-02 10:51:31 -04:00
stl_list.h libstdc++: Make self-move well-defined for containers [PR 85828] 2020-08-12 20:36:00 +01:00
stl_map.h libstdc++: Fix -Wmismatched-tags warnings (PR 96063) 2020-07-06 17:12:29 +01:00
stl_multimap.h libstdc++: Fix -Wmismatched-tags warnings (PR 96063) 2020-07-06 17:12:29 +01:00
stl_multiset.h libstdc++: Fix -Wmismatched-tags warnings (PR 96063) 2020-07-06 17:12:29 +01:00
stl_numeric.h libstdc++: P1645R1 constexpr for <numeric> algorithms 2020-02-26 10:23:17 -05:00
stl_pair.h libstdc++: Remove redundant std:: qualification 2020-06-19 15:02:54 +01:00
stl_queue.h libstdc++: Define operator<=> for std::stack and std::queue 2020-04-19 21:30:15 +01:00
stl_raw_storage_iter.h Update copyright years. 2020-01-01 12:51:42 +01:00
stl_relops.h Update copyright years. 2020-01-01 12:51:42 +01:00
stl_set.h libstdc++: Fix -Wmismatched-tags warnings (PR 96063) 2020-07-06 17:12:29 +01:00
stl_stack.h libstdc++: Define operator<=> for std::stack and std::queue 2020-04-19 21:30:15 +01:00
stl_tempbuf.h Update copyright years. 2020-01-01 12:51:42 +01:00
stl_tree.h libstdc++: Fix some warnings in headers 2020-10-29 11:43:55 +00:00
stl_uninitialized.h libstdc++: Fix some -Wsystem-headers warnings (PR 95765) 2020-06-19 18:20:05 +01:00
stl_vector.h libstdc++: Add comparison operators to sequence containers 2020-04-17 23:41:04 +01:00
stream_iterator.h libstdc++: Fix compilation with released versions of Clang 2020-03-18 12:55:29 +00:00
streambuf_iterator.h libstdc++: Specialize copy/copy_n for istreambuf_iterator and deque iterators 2020-06-04 22:27:47 +02:00
streambuf.tcc Update copyright years. 2020-01-01 12:51:42 +01:00
string_view.tcc Update copyright years. 2020-01-01 12:51:42 +01:00
stringfwd.h Update copyright years. 2020-01-01 12:51:42 +01:00
uniform_int_dist.h libstdc++: Improve comment on _Power_of_2 helper function 2020-11-09 14:54:29 +00:00
unique_lock.h Update copyright years. 2020-01-01 12:51:42 +01:00
unique_ptr.h libstdc++: Implement std::make_unique_for_overwrite 2020-10-19 22:11:39 +01:00
unordered_map.h libstdc++: Fix unordered containers move constructors noexcept qualification 2020-07-29 11:26:42 +02:00
unordered_set.h libstdc++: Fix unordered containers move constructors noexcept qualification 2020-07-29 11:26:42 +02:00
uses_allocator_args.h libstdc++: Reduce header dependencies in and on <memory> 2020-10-22 18:42:03 +01:00
uses_allocator.h Update copyright years. 2020-01-01 12:51:42 +01:00
valarray_after.h libstdc++: Fix name clash with _Cosh in QNX headers [PR 95592] 2020-10-28 12:35:44 +00:00
valarray_array.h Update copyright years. 2020-01-01 12:51:42 +01:00
valarray_array.tcc Update copyright years. 2020-01-01 12:51:42 +01:00
valarray_before.h Update copyright years. 2020-01-01 12:51:42 +01:00
vector.tcc Update copyright years. 2020-01-01 12:51:42 +01:00