@@ -12,7 +12,7 @@ Major new features:
* The dynamic linker accepts the --preload argument to preload shared
objects, in addition to the LD_PRELOAD environment variable.
-* On Linux, the gettid function has been added.
+* On Linux, the gettid and pthread_current_tid_np function have been added.
Deprecated and removed features, and other changes affecting compatibility:
@@ -236,6 +236,21 @@ thread. The returned value is obtained from the Linux kernel and is
not subject to caching. See the discussion of thread IDs above,
especially regarding reuse of the IDs of threads which have exited.
+The @code{pthread_current_tid_np} function below uses caching and is
+therefore faster.
+
+This function is specific to Linux.
+@end deftypefun
+
+@deftypefun pid_t pthread_current_tid_np (void)
+@standards{Linux, pthread.h}
+@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
+The @code{pthread_current_tid_np} function returns the cached thread
+ID of the current thread. It is faster than the @code{gettid}
+function. However, it will return incorrect results in the new
+subprocess after a call to the @code{vfork} function or direct system
+calls which created subprocesses.
+
This function is specific to Linux.
@end deftypefun
@@ -30,7 +30,7 @@ extra-libs-others := $(extra-libs)
routines = alloca_cutoff forward libc-lowlevellock libc-cancellation \
libc-cleanup libc_pthread_init libc_multiple_threads \
register-atfork pthread_atfork pthread_self thrd_current \
- thrd_equal thrd_sleep thrd_yield
+ thrd_equal thrd_sleep thrd_yield pthread_current_tid_np
shared-only-routines = forward
static-only-routines = pthread_atfork
@@ -32,6 +32,9 @@ libc {
GLIBC_2.28 {
thrd_current; thrd_equal; thrd_sleep; thrd_yield;
}
+ GLIBC_2.30 {
+ pthread_current_tid_np;
+ }
GLIBC_PRIVATE {
__libc_alloca_cutoff;
# Internal libc interface to libpthread
new file mode 100644
@@ -0,0 +1,26 @@
+/* Cached access to the kernel TID.
+ Copyright (C) 2019 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, see
+ <http://www.gnu.org/licenses/>. */
+
+#include <pthread.h>
+#include <tls.h>
+
+__pid_t
+pthread_current_tid_np (void)
+{
+ return THREAD_GETMEM (THREAD_SELF, tid);
+}
@@ -275,6 +275,15 @@ extern int pthread_detach (pthread_t __th) __THROW;
/* Obtain the identifier of the current thread. */
extern pthread_t pthread_self (void) __THROW __attribute__ ((__const__));
+#ifdef __USE_GNU
+/* Return the kernel TID of the current thread. This value is cached
+ and will therefore be incorrect after vfork and system calls which
+ alter the kernel TID (such as clone). See gettid for a non-cached
+ version. The same limitations regarding TID reuse apply to this
+ function. */
+extern __pid_t pthread_current_tid_np (void) __THROW __attribute__ ((__const__));
+#endif
+
/* Compare two thread identifiers. */
extern int pthread_equal (pthread_t __thread1, pthread_t __thread2)
__THROW __attribute__ ((__const__));
@@ -2142,3 +2142,4 @@ GLIBC_2.29 getcpu F
GLIBC_2.29 posix_spawn_file_actions_addchdir_np F
GLIBC_2.29 posix_spawn_file_actions_addfchdir_np F
GLIBC_2.30 gettid F
+GLIBC_2.30 pthread_current_tid_np F
@@ -2205,6 +2205,7 @@ GLIBC_2.3.4 setsourcefilter F
GLIBC_2.3.4 xdr_quad_t F
GLIBC_2.3.4 xdr_u_quad_t F
GLIBC_2.30 gettid F
+GLIBC_2.30 pthread_current_tid_np F
GLIBC_2.4 _IO_fprintf F
GLIBC_2.4 _IO_printf F
GLIBC_2.4 _IO_sprintf F
@@ -127,6 +127,7 @@ GLIBC_2.29 getcpu F
GLIBC_2.29 posix_spawn_file_actions_addchdir_np F
GLIBC_2.29 posix_spawn_file_actions_addfchdir_np F
GLIBC_2.30 gettid F
+GLIBC_2.30 pthread_current_tid_np F
GLIBC_2.4 _Exit F
GLIBC_2.4 _IO_2_1_stderr_ D 0xa0
GLIBC_2.4 _IO_2_1_stdin_ D 0xa0
@@ -30,7 +30,8 @@
directly that have not been created explicitly). Note that a TID
uniquely identifies a thread only while this thread is running; a
TID can be reused once a thread has exited, even if the thread is
- not detached and has not been joined. */
+ not detached and has not been joined. See pthread_current_tid_np
+ in <pthread.h> for a caching variant of this function. */
extern __pid_t gettid (void) __THROW;
#endif
@@ -2086,3 +2086,4 @@ GLIBC_2.29 xencrypt F
GLIBC_2.29 xprt_register F
GLIBC_2.29 xprt_unregister F
GLIBC_2.30 gettid F
+GLIBC_2.30 pthread_current_tid_np F
@@ -2038,6 +2038,7 @@ GLIBC_2.3.4 setsourcefilter F
GLIBC_2.3.4 xdr_quad_t F
GLIBC_2.3.4 xdr_u_quad_t F
GLIBC_2.30 gettid F
+GLIBC_2.30 pthread_current_tid_np F
GLIBC_2.4 __confstr_chk F
GLIBC_2.4 __fgets_chk F
GLIBC_2.4 __fgets_unlocked_chk F
@@ -2204,6 +2204,7 @@ GLIBC_2.3.4 vm86 F
GLIBC_2.3.4 xdr_quad_t F
GLIBC_2.3.4 xdr_u_quad_t F
GLIBC_2.30 gettid F
+GLIBC_2.30 pthread_current_tid_np F
GLIBC_2.4 __confstr_chk F
GLIBC_2.4 __fgets_chk F
GLIBC_2.4 __fgets_unlocked_chk F
@@ -2070,6 +2070,7 @@ GLIBC_2.3.4 setsourcefilter F
GLIBC_2.3.4 xdr_quad_t F
GLIBC_2.3.4 xdr_u_quad_t F
GLIBC_2.30 gettid F
+GLIBC_2.30 pthread_current_tid_np F
GLIBC_2.4 __confstr_chk F
GLIBC_2.4 __fgets_chk F
GLIBC_2.4 __fgets_unlocked_chk F
@@ -128,6 +128,7 @@ GLIBC_2.29 getcpu F
GLIBC_2.29 posix_spawn_file_actions_addchdir_np F
GLIBC_2.29 posix_spawn_file_actions_addfchdir_np F
GLIBC_2.30 gettid F
+GLIBC_2.30 pthread_current_tid_np F
GLIBC_2.4 _Exit F
GLIBC_2.4 _IO_2_1_stderr_ D 0x98
GLIBC_2.4 _IO_2_1_stdin_ D 0x98
@@ -2147,6 +2147,7 @@ GLIBC_2.3.4 setsourcefilter F
GLIBC_2.3.4 xdr_quad_t F
GLIBC_2.3.4 xdr_u_quad_t F
GLIBC_2.30 gettid F
+GLIBC_2.30 pthread_current_tid_np F
GLIBC_2.4 __confstr_chk F
GLIBC_2.4 __fgets_chk F
GLIBC_2.4 __fgets_unlocked_chk F
@@ -2134,3 +2134,4 @@ GLIBC_2.29 getcpu F
GLIBC_2.29 posix_spawn_file_actions_addchdir_np F
GLIBC_2.29 posix_spawn_file_actions_addfchdir_np F
GLIBC_2.30 gettid F
+GLIBC_2.30 pthread_current_tid_np F
@@ -2121,6 +2121,7 @@ GLIBC_2.3.4 setsourcefilter F
GLIBC_2.3.4 xdr_quad_t F
GLIBC_2.3.4 xdr_u_quad_t F
GLIBC_2.30 gettid F
+GLIBC_2.30 pthread_current_tid_np F
GLIBC_2.4 __confstr_chk F
GLIBC_2.4 __fgets_chk F
GLIBC_2.4 __fgets_unlocked_chk F
@@ -2119,6 +2119,7 @@ GLIBC_2.3.4 setsourcefilter F
GLIBC_2.3.4 xdr_quad_t F
GLIBC_2.3.4 xdr_u_quad_t F
GLIBC_2.30 gettid F
+GLIBC_2.30 pthread_current_tid_np F
GLIBC_2.4 __confstr_chk F
GLIBC_2.4 __fgets_chk F
GLIBC_2.4 __fgets_unlocked_chk F
@@ -2127,6 +2127,7 @@ GLIBC_2.3.4 setsourcefilter F
GLIBC_2.3.4 xdr_quad_t F
GLIBC_2.3.4 xdr_u_quad_t F
GLIBC_2.30 gettid F
+GLIBC_2.30 pthread_current_tid_np F
GLIBC_2.4 __confstr_chk F
GLIBC_2.4 __fgets_chk F
GLIBC_2.4 __fgets_unlocked_chk F
@@ -2121,6 +2121,7 @@ GLIBC_2.3.4 setsourcefilter F
GLIBC_2.3.4 xdr_quad_t F
GLIBC_2.3.4 xdr_u_quad_t F
GLIBC_2.30 gettid F
+GLIBC_2.30 pthread_current_tid_np F
GLIBC_2.4 __confstr_chk F
GLIBC_2.4 __fgets_chk F
GLIBC_2.4 __fgets_unlocked_chk F
@@ -2175,3 +2175,4 @@ GLIBC_2.29 getcpu F
GLIBC_2.29 posix_spawn_file_actions_addchdir_np F
GLIBC_2.29 posix_spawn_file_actions_addfchdir_np F
GLIBC_2.30 gettid F
+GLIBC_2.30 pthread_current_tid_np F
@@ -2165,6 +2165,7 @@ GLIBC_2.3.4 swapcontext F
GLIBC_2.3.4 xdr_quad_t F
GLIBC_2.3.4 xdr_u_quad_t F
GLIBC_2.30 gettid F
+GLIBC_2.30 pthread_current_tid_np F
GLIBC_2.4 _IO_fprintf F
GLIBC_2.4 _IO_printf F
GLIBC_2.4 _IO_sprintf F
@@ -2198,6 +2198,7 @@ GLIBC_2.3.4 swapcontext F
GLIBC_2.3.4 xdr_quad_t F
GLIBC_2.3.4 xdr_u_quad_t F
GLIBC_2.30 gettid F
+GLIBC_2.30 pthread_current_tid_np F
GLIBC_2.4 _IO_fprintf F
GLIBC_2.4 _IO_printf F
GLIBC_2.4 _IO_sprintf F
@@ -2028,6 +2028,7 @@ GLIBC_2.3.4 swapcontext F
GLIBC_2.3.4 xdr_quad_t F
GLIBC_2.3.4 xdr_u_quad_t F
GLIBC_2.30 gettid F
+GLIBC_2.30 pthread_current_tid_np F
GLIBC_2.4 _IO_fprintf F
GLIBC_2.4 _IO_printf F
GLIBC_2.4 _IO_sprintf F
@@ -2232,3 +2232,4 @@ GLIBC_2.29 getcpu F
GLIBC_2.29 posix_spawn_file_actions_addchdir_np F
GLIBC_2.29 posix_spawn_file_actions_addfchdir_np F
GLIBC_2.30 gettid F
+GLIBC_2.30 pthread_current_tid_np F
@@ -2104,3 +2104,4 @@ GLIBC_2.29 getcpu F
GLIBC_2.29 posix_spawn_file_actions_addchdir_np F
GLIBC_2.29 posix_spawn_file_actions_addfchdir_np F
GLIBC_2.30 gettid F
+GLIBC_2.30 pthread_current_tid_np F
@@ -2160,6 +2160,7 @@ GLIBC_2.3.4 setsourcefilter F
GLIBC_2.3.4 xdr_quad_t F
GLIBC_2.3.4 xdr_u_quad_t F
GLIBC_2.30 gettid F
+GLIBC_2.30 pthread_current_tid_np F
GLIBC_2.4 _IO_fprintf F
GLIBC_2.4 _IO_printf F
GLIBC_2.4 _IO_sprintf F
@@ -2064,6 +2064,7 @@ GLIBC_2.3.4 setsourcefilter F
GLIBC_2.3.4 xdr_quad_t F
GLIBC_2.3.4 xdr_u_quad_t F
GLIBC_2.30 gettid F
+GLIBC_2.30 pthread_current_tid_np F
GLIBC_2.4 _IO_fprintf F
GLIBC_2.4 _IO_printf F
GLIBC_2.4 _IO_sprintf F
@@ -2042,6 +2042,7 @@ GLIBC_2.3.4 setsourcefilter F
GLIBC_2.3.4 xdr_quad_t F
GLIBC_2.3.4 xdr_u_quad_t F
GLIBC_2.30 gettid F
+GLIBC_2.30 pthread_current_tid_np F
GLIBC_2.4 __confstr_chk F
GLIBC_2.4 __fgets_chk F
GLIBC_2.4 __fgets_unlocked_chk F
@@ -2154,6 +2154,7 @@ GLIBC_2.3.4 setsourcefilter F
GLIBC_2.3.4 xdr_quad_t F
GLIBC_2.3.4 xdr_u_quad_t F
GLIBC_2.30 gettid F
+GLIBC_2.30 pthread_current_tid_np F
GLIBC_2.4 _IO_fprintf F
GLIBC_2.4 _IO_printf F
GLIBC_2.4 _IO_sprintf F
@@ -2093,6 +2093,7 @@ GLIBC_2.3.4 setsourcefilter F
GLIBC_2.3.4 xdr_quad_t F
GLIBC_2.3.4 xdr_u_quad_t F
GLIBC_2.30 gettid F
+GLIBC_2.30 pthread_current_tid_np F
GLIBC_2.4 __confstr_chk F
GLIBC_2.4 __fgets_chk F
GLIBC_2.4 __fgets_unlocked_chk F
@@ -30,6 +30,7 @@ subprocess (void *closure)
{
TEST_COMPARE (getpid (), gettid ());
TEST_VERIFY (gettid () != initial_tid);
+ TEST_COMPARE (gettid (), pthread_current_tid_np ());
}
/* Check that the TID changes in a new thread. */
@@ -38,6 +39,7 @@ threadfunc (void *closure)
{
TEST_VERIFY (getpid () != gettid ());
TEST_VERIFY (gettid () != initial_tid);
+ TEST_COMPARE (gettid (), pthread_current_tid_np ());
return NULL;
}
@@ -52,6 +54,9 @@ test_vfork (void)
_exit (1);
if (gettid () == initial_tid)
_exit (2);
+ /* Verify that the cached TID has not changed. */
+ if (pthread_current_tid_np () != initial_tid)
+ _exit (3);
_exit (0);
}
int status;
@@ -66,6 +71,7 @@ do_test (void)
/* The main thread has the same TID as the PID. */
TEST_COMPARE (getpid (), gettid ());
+ TEST_COMPARE (gettid (), pthread_current_tid_np ());
test_vfork ();
@@ -2051,6 +2051,7 @@ GLIBC_2.3.4 setsourcefilter F
GLIBC_2.3.4 xdr_quad_t F
GLIBC_2.3.4 xdr_u_quad_t F
GLIBC_2.30 gettid F
+GLIBC_2.30 pthread_current_tid_np F
GLIBC_2.4 __confstr_chk F
GLIBC_2.4 __fgets_chk F
GLIBC_2.4 __fgets_unlocked_chk F
@@ -2150,3 +2150,4 @@ GLIBC_2.29 getcpu F
GLIBC_2.29 posix_spawn_file_actions_addchdir_np F
GLIBC_2.29 posix_spawn_file_actions_addfchdir_np F
GLIBC_2.30 gettid F
+GLIBC_2.30 pthread_current_tid_np F