]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
Optimize pthread_cond_timedwait to avoid unnecessary call to clock_gettime for CLOCK_...
authorJustin King <jcking@google.com>
Thu, 11 Jan 2024 18:51:23 +0000 (18:51 +0000)
committerPranav Kant <prka@google.com>
Fri, 12 Jan 2024 23:20:06 +0000 (23:20 +0000)
nptl/pthread_cond_wait.c
nptl/sem_waitcommon.c
sysdeps/nptl/futex-internal.h
sysdeps/sparc/sparc32/sem_waitcommon.c
sysdeps/unix/sysv/linux/futex-internal.h

index 3e1105418210288ef21c37730a59dc8bc619ab3b..0dbab42148ed3466698d5d6dfec28cdb16a791eb 100644 (file)
@@ -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);
                }
            }
 
index 30984be2d0e0e7312e5daac0fe1122cafa0f9ef4..7d2a5b2121e1e5db0da68bf60cb9925b7dfbead2 100644 (file)
@@ -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;
index 1a5624789d4ab117f4c1ab7544168b508401ab9f..a0ed961def290a1ec7f68145c4fcf34abf241d5a 100644 (file)
@@ -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
index 1ddd65f745e4acfba0a61592024d42286d489ac8..5e45d0c3af68aba7422e4cc9b48449c4ac7aff90 100644 (file)
@@ -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;
 }
index 96a07b05b9fc553f2f4e2bb0420045e1cb020758..7ccd40ac08a854bcb3b98c990a9dc4e51f255eb0 100644 (file)
@@ -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)
     {