]> Savannah Git Hosting - gnulib.git/commitdiff
gettimeofday, pthread-*, thread, thrd: Don't omit intended initializers.
authorBruno Haible <bruno@clisp.org>
Mon, 15 May 2023 12:36:17 +0000 (14:36 +0200)
committerBruno Haible <bruno@clisp.org>
Mon, 15 May 2023 12:36:17 +0000 (14:36 +0200)
* lib/gettimeofday.c (gettimeofday): List the initializers of both
tv_sec and tv_usec.
* lib/glthread/thread.c (gl_thread_self): List the initializers of both
tv_sec and tv_nsec.
* lib/pthread-cond.c (pthread_cond_wait): Likewise.
* lib/thrd.c (rpl_thrd_current): Likewise.
* lib/pthread-rwlock.c (MIN): New macro.
(pthread_rwlock_timedrdlock, pthread_rwlock_timedwrlock): List the
initializers of both tv_sec and tv_nsec. Don't modify the duration after
having initialized it.
* lib/pthread_mutex_timedlock.c (MIN): New macro.
(pthread_mutex_timedlock): List the initializers of both tv_sec and
tv_nsec. Don't modify the duration after having initialized it.

ChangeLog
lib/gettimeofday.c
lib/glthread/thread.c
lib/pthread-cond.c
lib/pthread-rwlock.c
lib/pthread_mutex_timedlock.c
lib/thrd.c

index d01675a51cd831e6a250c7b85d4dee359d07e8c5..9bf5a0d874a2c546be2ca5fa8aaaa6b78b10d0fd 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,20 @@
 2023-05-15  Bruno Haible  <bruno@clisp.org>
 
+       gettimeofday, pthread-*, thread, thrd: Don't omit intended initializers.
+       * lib/gettimeofday.c (gettimeofday): List the initializers of both
+       tv_sec and tv_usec.
+       * lib/glthread/thread.c (gl_thread_self): List the initializers of both
+       tv_sec and tv_nsec.
+       * lib/pthread-cond.c (pthread_cond_wait): Likewise.
+       * lib/thrd.c (rpl_thrd_current): Likewise.
+       * lib/pthread-rwlock.c (MIN): New macro.
+       (pthread_rwlock_timedrdlock, pthread_rwlock_timedwrlock): List the
+       initializers of both tv_sec and tv_nsec. Don't modify the duration after
+       having initialized it.
+       * lib/pthread_mutex_timedlock.c (MIN): New macro.
+       (pthread_mutex_timedlock): List the initializers of both tv_sec and
+       tv_nsec. Don't modify the duration after having initialized it.
+
        select: Fix compilation error (regression from yesterday).
        * lib/select.c (rpl_select): Revert last change.
 
index 69b43a62c86de91eaa5bab9b2161b567f64c5385..c71629cbc576c80414299de3426e0921b84e3c53 100644 (file)
@@ -142,7 +142,7 @@ gettimeofday (struct timeval *restrict tv, void *restrict tz)
 #   error "Only 1-second nominal clock resolution found.  Is that intended?" \
           "If so, compile with the -DOK_TO_USE_1S_CLOCK option."
 #  endif
-  *tv = (struct timeval) { .tv_sec = time (NULL) };
+  *tv = (struct timeval) { .tv_sec = time (NULL), .tv_usec = 0 };
 
   return 0;
 
index 3352159edc4d40a65aedcaea7d17256abf8a0cfa..ce6b9a83edc3f91447ece5c030f2580231ff6f15 100644 (file)
@@ -139,7 +139,11 @@ gl_thread_self (void)
             /* Memory allocation failed.  There is not much we can do.  Have to
                busy-loop, waiting for the availability of memory.  */
             {
-              struct timespec ts = { .tv_sec = 1 };
+              struct timespec ts =
+                {
+                  .tv_sec = 1,
+                  .tv_nsec = 0
+                };
               thrd_sleep (&ts, NULL);
             }
           }
index 6980cc6ed93ed466d4ce480142ea5925fe49229a..7c94e47b1ade0e7fb908fc4077a6a794a0de2c11 100644 (file)
@@ -115,7 +115,11 @@ pthread_cond_wait (_GL_UNUSED pthread_cond_t *cond,
      Wait endlessly.  */
   for (;;)
     {
-      struct timespec duration = { .tv_sec = 86400 };
+      struct timespec duration =
+        {
+          .tv_sec = 86400,
+          .tv_nsec = 0
+        };
       nanosleep (&duration, NULL);
     }
 }
index 5087661b0b7f838613a98988974f6969c48a52c0..25ff5eeb370df1bc4d59e45ea89ab4f8e313a6ae 100644 (file)
 # include <time.h>
 #endif
 
+#ifndef MIN
+# define MIN(a, b) ((a) < (b) ? (a) : (b))
+#endif
+
 #if ((defined _WIN32 && ! defined __CYGWIN__) && USE_WINDOWS_THREADS) || !HAVE_PTHREAD_H
 
 int
@@ -409,9 +413,11 @@ pthread_rwlock_timedrdlock (pthread_rwlock_t *lock,
         return ETIMEDOUT;
 
       /* Sleep 1 ms.  */
-      struct timespec duration = { .tv_nsec = 1000000 };
-      if (duration.tv_nsec > remaining)
-        duration.tv_nsec = remaining;
+      struct timespec duration =
+        {
+          .tv_sec = 0,
+          .tv_nsec = MIN (1000000, remaining)
+        };
       nanosleep (&duration, NULL);
     }
 }
@@ -464,9 +470,11 @@ pthread_rwlock_timedwrlock (pthread_rwlock_t *lock,
         return ETIMEDOUT;
 
       /* Sleep 1 ms.  */
-      struct timespec duration = { .tv_nsec = 1000000 };
-      if (duration.tv_nsec > remaining)
-        duration.tv_nsec = remaining;
+      struct timespec duration =
+        {
+          .tv_sec = 0,
+          .tv_nsec = MIN (1000000, remaining)
+        };
       nanosleep (&duration, NULL);
     }
 }
index 2394092e83b2c18d673f8487a575ccea59965cdd..2e931bb1d8fd4e2b527a76c1415455c7387e13a0 100644 (file)
 #include <sys/time.h>
 #include <time.h>
 
+#ifndef MIN
+# define MIN(a, b) ((a) < (b) ? (a) : (b))
+#endif
+
 int
 pthread_mutex_timedlock (pthread_mutex_t *mutex, const struct timespec *abstime)
 {
@@ -77,9 +81,11 @@ pthread_mutex_timedlock (pthread_mutex_t *mutex, const struct timespec *abstime)
         return ETIMEDOUT;
 
       /* Sleep 1 ms.  */
-      struct timespec duration = { .tv_nsec = 1000000 };
-      if (duration.tv_nsec > remaining)
-        duration.tv_nsec = remaining;
+      struct timespec duration =
+        {
+          .tv_sec = 0,
+          .tv_nsec = MIN (1000000, remaining)
+        };
       nanosleep (&duration, NULL);
     }
 }
index 1a13a51e00b55880d6b05afaa277cbb0209cbbd8..5e9a988c2cbf5c85e54b5c137709b0d0db3d0d9b 100644 (file)
@@ -143,7 +143,11 @@ rpl_thrd_current (void)
             /* Memory allocation failed.  There is not much we can do.  Have to
                busy-loop, waiting for the availability of memory.  */
             {
-              struct timespec ts = { .tv_sec = 1 };
+              struct timespec ts =
+                {
+                  .tv_sec = 1,
+                  .tv_nsec = 0
+                };
               thrd_sleep (&ts, NULL);
             }
           }