public inbox for glibc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Pranav Kant <pranavk@sourceware.org>
To: glibc-cvs@sourceware.org
Subject: [glibc/google/grte/v5-2.27/master] Optimize pthread_cond_timedwait to avoid unnecessary call to clock_gettime for CLOCK_MONOTONIC
Date: Fri, 12 Jan 2024 23:23:50 +0000 (GMT)	[thread overview]
Message-ID: <20240112232350.E3ABC385734E@sourceware.org> (raw)

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

commit be3c0fe888d69cdfda53a8065c5f486d01f7096e
Author: Justin King <jcking@google.com>
Date:   Thu Jan 11 18:51:23 2024 +0000

    Optimize pthread_cond_timedwait to avoid unnecessary call to clock_gettime for CLOCK_MONOTONIC

Diff:
---
 nptl/pthread_cond_wait.c                 | 29 ++++-------------------------
 nptl/sem_waitcommon.c                    |  4 ++--
 sysdeps/nptl/futex-internal.h            |  2 +-
 sysdeps/sparc/sparc32/sem_waitcommon.c   |  2 +-
 sysdeps/unix/sysv/linux/futex-internal.h |  4 ++--
 5 files changed, 10 insertions(+), 31 deletions(-)

diff --git a/nptl/pthread_cond_wait.c b/nptl/pthread_cond_wait.c
index 3e11054182..0dbab42148 100644
--- a/nptl/pthread_cond_wait.c
+++ b/nptl/pthread_cond_wait.c
@@ -510,34 +510,13 @@ __pthread_cond_wait_common (pthread_cond_t *cond, pthread_mutex_t *mutex,
 	      if (__glibc_unlikely (abstime->tv_sec < 0))
 	        err = ETIMEDOUT;
 
-	      else if ((flags & __PTHREAD_COND_CLOCK_MONOTONIC_MASK) != 0)
-		{
-		  /* CLOCK_MONOTONIC is requested.  */
-		  struct timespec rt;
-		  if (__clock_gettime (CLOCK_MONOTONIC, &rt) != 0)
-		    __libc_fatal ("clock_gettime does not support "
-				  "CLOCK_MONOTONIC");
-		  /* Convert the absolute timeout value to a relative
-		     timeout.  */
-		  rt.tv_sec = abstime->tv_sec - rt.tv_sec;
-		  rt.tv_nsec = abstime->tv_nsec - rt.tv_nsec;
-		  if (rt.tv_nsec < 0)
-		    {
-		      rt.tv_nsec += 1000000000;
-		      --rt.tv_sec;
-		    }
-		  /* Did we already time out?  */
-		  if (__glibc_unlikely (rt.tv_sec < 0))
-		    err = ETIMEDOUT;
-		  else
-		    err = futex_reltimed_wait_cancelable
-			(cond->__data.__g_signals + g, 0, &rt, private);
-		}
 	      else
 		{
-		  /* Use CLOCK_REALTIME.  */
 		  err = futex_abstimed_wait_cancelable
-		      (cond->__data.__g_signals + g, 0, abstime, private);
+		      (cond->__data.__g_signals + g, 0, abstime,
+		      (flags & __PTHREAD_COND_CLOCK_MONOTONIC_MASK) != 0
+                        ? 0
+                        : FUTEX_CLOCK_REALTIME, private);
 		}
 	    }
 
diff --git a/nptl/sem_waitcommon.c b/nptl/sem_waitcommon.c
index 30984be2d0..7d2a5b2121 100644
--- a/nptl/sem_waitcommon.c
+++ b/nptl/sem_waitcommon.c
@@ -110,10 +110,10 @@ do_futex_wait (struct new_sem *sem, const struct timespec *abstime)
 #if __HAVE_64B_ATOMICS
   err = futex_abstimed_wait_cancelable (
       (unsigned int *) &sem->data + SEM_VALUE_OFFSET, 0, abstime,
-      sem->private);
+      FUTEX_CLOCK_REALTIME, sem->private);
 #else
   err = futex_abstimed_wait_cancelable (&sem->value, SEM_NWAITERS_MASK,
-					abstime, sem->private);
+					abstime, FUTEX_CLOCK_REALTIME, sem->private);
 #endif
 
   return err;
diff --git a/sysdeps/nptl/futex-internal.h b/sysdeps/nptl/futex-internal.h
index 1a5624789d..a0ed961def 100644
--- a/sysdeps/nptl/futex-internal.h
+++ b/sysdeps/nptl/futex-internal.h
@@ -169,7 +169,7 @@ futex_abstimed_wait (unsigned int* futex_word, unsigned int expected,
 static __always_inline int
 futex_abstimed_wait_cancelable (unsigned int* futex_word,
 				unsigned int expected,
-			        const struct timespec* abstime, int private);
+			        const struct timespec* abstime, int clockbit, int private);
 
 /* Atomically wrt other futex operations on the same futex, this unblocks the
    specified number of processes, or all processes blocked on this futex if
diff --git a/sysdeps/sparc/sparc32/sem_waitcommon.c b/sysdeps/sparc/sparc32/sem_waitcommon.c
index 1ddd65f745..5e45d0c3af 100644
--- a/sysdeps/sparc/sparc32/sem_waitcommon.c
+++ b/sysdeps/sparc/sparc32/sem_waitcommon.c
@@ -52,7 +52,7 @@ do_futex_wait (struct new_sem *sem, const struct timespec *abstime)
   int err;
 
   err = futex_abstimed_wait_cancelable (&sem->value, SEM_NWAITERS_MASK,
-					abstime, sem->private);
+					abstime, FUTEX_CLOCK_REALTIME, sem->private);
 
   return err;
 }
diff --git a/sysdeps/unix/sysv/linux/futex-internal.h b/sysdeps/unix/sysv/linux/futex-internal.h
index 96a07b05b9..7ccd40ac08 100644
--- a/sysdeps/unix/sysv/linux/futex-internal.h
+++ b/sysdeps/unix/sysv/linux/futex-internal.h
@@ -194,7 +194,7 @@ futex_abstimed_wait (unsigned int *futex_word, unsigned int expected,
 static __always_inline int
 futex_abstimed_wait_cancelable (unsigned int *futex_word,
 				unsigned int expected,
-			        const struct timespec *abstime, int private)
+			        const struct timespec *abstime, int clockbit, int private)
 {
   /* Work around the fact that the kernel rejects negative timeout values
      despite them being valid.  */
@@ -203,7 +203,7 @@ futex_abstimed_wait_cancelable (unsigned int *futex_word,
   int oldtype;
   oldtype = __pthread_enable_asynccancel ();
   int err = lll_futex_timed_wait_bitset (futex_word, expected, abstime,
-					 FUTEX_CLOCK_REALTIME, private);
+					 clockbit, private);
   __pthread_disable_asynccancel (oldtype);
   switch (err)
     {

                 reply	other threads:[~2024-01-12 23:23 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240112232350.E3ABC385734E@sourceware.org \
    --to=pranavk@sourceware.org \
    --cc=glibc-cvs@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).