public inbox for glibc-cvs@sourceware.org
help / color / mirror / Atom feed
* [glibc/azanella/pthread-multiple-fixes] nptl: Use exit_lock when accessing TID on pthread_getcpuclockid
@ 2021-09-10 14:37 Adhemerval Zanella
  0 siblings, 0 replies; only message in thread
From: Adhemerval Zanella @ 2021-09-10 14:37 UTC (permalink / raw)
  To: glibc-cvs

https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=f7a07b5f8f401902ea5af99c192f7dd291dc968f

commit f7a07b5f8f401902ea5af99c192f7dd291dc968f
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Date:   Mon Aug 23 14:09:23 2021 -0300

    nptl: Use exit_lock when accessing TID on pthread_getcpuclockid
    
    Checked on x86_64-linux-gnu.

Diff:
---
 nptl/pthread_getcpuclockid.c         | 26 ++++++++++++++------------
 sysdeps/pthread/tst-pthread-exited.c |  7 +++++++
 2 files changed, 21 insertions(+), 12 deletions(-)

diff --git a/nptl/pthread_getcpuclockid.c b/nptl/pthread_getcpuclockid.c
index 0a6656ea4c..63f1978665 100644
--- a/nptl/pthread_getcpuclockid.c
+++ b/nptl/pthread_getcpuclockid.c
@@ -16,11 +16,9 @@
    License along with the GNU C Library; see the file COPYING.LIB.  If
    not, see <https://www.gnu.org/licenses/>.  */
 
-#include <errno.h>
-#include <pthreadP.h>
-#include <sys/time.h>
-#include <tls.h>
+#include <libc-lock.h>
 #include <kernel-posix-cpu-timers.h>
+#include <pthreadP.h>
 #include <shlib-compat.h>
 
 int
@@ -28,17 +26,21 @@ __pthread_getcpuclockid (pthread_t threadid, clockid_t *clockid)
 {
   struct pthread *pd = (struct pthread *) threadid;
 
-  /* Make sure the descriptor is valid.  */
-  if (INVALID_TD_P (pd))
-    /* Not a valid thread handle.  */
-    return ESRCH;
+  /* Block all signals, as required by pd->exit_lock.  */
+  sigset_t old_mask;
+  __libc_signal_block_all (&old_mask);
+  __libc_lock_lock (pd->exit_lock);
 
-  /* The clockid_t value is a simple computation from the TID.  */
+  int res = 0;
+  if (pd->tid != 0)
+    *clockid = MAKE_THREAD_CPUCLOCK (pd->tid, CPUCLOCK_SCHED);
+  else
+    res = ESRCH;
 
-  const clockid_t tidclock = MAKE_THREAD_CPUCLOCK (pd->tid, CPUCLOCK_SCHED);
+  __libc_lock_unlock (pd->exit_lock);
+  __libc_signal_restore_set (&old_mask);
 
-  *clockid = tidclock;
-  return 0;
+  return res;
 }
 versioned_symbol (libc, __pthread_getcpuclockid, pthread_getcpuclockid,
                   GLIBC_2_34);
diff --git a/sysdeps/pthread/tst-pthread-exited.c b/sysdeps/pthread/tst-pthread-exited.c
index 567d6b9b49..d33e6b28fe 100644
--- a/sysdeps/pthread/tst-pthread-exited.c
+++ b/sysdeps/pthread/tst-pthread-exited.c
@@ -23,6 +23,7 @@
 #include <support/check.h>
 #include <support/support.h>
 #include <support/xthread.h>
+#include <time.h>
 
 static void *
 noop_thread (void *closure)
@@ -50,6 +51,12 @@ do_test (void)
     TEST_COMPARE (r, ESRCH);
   }
 
+  {
+    clockid_t clkid;
+    int r = pthread_getcpuclockid (thr, &clkid);
+    TEST_COMPARE (r, ESRCH);
+  }
+
   xpthread_join (thr);
 
   return 0;


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2021-09-10 14:37 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-09-10 14:37 [glibc/azanella/pthread-multiple-fixes] nptl: Use exit_lock when accessing TID on pthread_getcpuclockid Adhemerval Zanella

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).