From 3a70af2168910c6ad0d59924d276f831638a52e0 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Mon, 15 May 2023 14:36:17 +0200 Subject: 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. --- ChangeLog | 15 +++++++++++++++ lib/gettimeofday.c | 2 +- lib/glthread/thread.c | 6 +++++- lib/pthread-cond.c | 6 +++++- lib/pthread-rwlock.c | 20 ++++++++++++++------ lib/pthread_mutex_timedlock.c | 12 +++++++++--- lib/thrd.c | 6 +++++- 7 files changed, 54 insertions(+), 13 deletions(-) diff --git a/ChangeLog b/ChangeLog index d01675a51c..9bf5a0d874 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,20 @@ 2023-05-15 Bruno Haible + 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. diff --git a/lib/gettimeofday.c b/lib/gettimeofday.c index 69b43a62c8..c71629cbc5 100644 --- a/lib/gettimeofday.c +++ b/lib/gettimeofday.c @@ -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; diff --git a/lib/glthread/thread.c b/lib/glthread/thread.c index 3352159edc..ce6b9a83ed 100644 --- a/lib/glthread/thread.c +++ b/lib/glthread/thread.c @@ -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); } } diff --git a/lib/pthread-cond.c b/lib/pthread-cond.c index 6980cc6ed9..7c94e47b1a 100644 --- a/lib/pthread-cond.c +++ b/lib/pthread-cond.c @@ -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); } } diff --git a/lib/pthread-rwlock.c b/lib/pthread-rwlock.c index 5087661b0b..25ff5eeb37 100644 --- a/lib/pthread-rwlock.c +++ b/lib/pthread-rwlock.c @@ -30,6 +30,10 @@ # include #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); } } diff --git a/lib/pthread_mutex_timedlock.c b/lib/pthread_mutex_timedlock.c index 2394092e83..2e931bb1d8 100644 --- a/lib/pthread_mutex_timedlock.c +++ b/lib/pthread_mutex_timedlock.c @@ -24,6 +24,10 @@ #include #include +#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); } } diff --git a/lib/thrd.c b/lib/thrd.c index 1a13a51e00..5e9a988c2c 100644 --- a/lib/thrd.c +++ b/lib/thrd.c @@ -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); } } -- cgit v1.2.1