From: Bruno Haible Date: Fri, 28 Jun 2024 19:40:16 +0000 (+0200) Subject: cond tests: Improve comments. X-Git-Url: https://gitweb.git.savannah.gnu.org/gitweb/?a=commitdiff_plain;h=d7b8b1ccc988290277945f52e1582dd669a97d7f;p=gnulib.git cond tests: Improve comments. * tests/test-cnd.c: Improve comments. (cond_value): Remove initializer. (cond_timed_out): Renamed from cond_timeout. * tests/test-cond.c: Improve comments. (cond_value): Remove initializer. (cond_timed_out): Renamed from cond_timeout. * tests/test-pthread-cond.c: Improve comments. (cond_value): Remove initializer. (cond_timed_out): Renamed from cond_timeout. --- diff --git a/ChangeLog b/ChangeLog index a9453b0b45..a309c59a50 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,16 @@ +2024-06-28 Bruno Haible + + cond tests: Improve comments. + * tests/test-cnd.c: Improve comments. + (cond_value): Remove initializer. + (cond_timed_out): Renamed from cond_timeout. + * tests/test-cond.c: Improve comments. + (cond_value): Remove initializer. + (cond_timed_out): Renamed from cond_timeout. + * tests/test-pthread-cond.c: Improve comments. + (cond_value): Remove initializer. + (cond_timed_out): Renamed from cond_timeout. + 2024-06-28 Bruno Haible time: Fix test failure on FreeBSD. diff --git a/tests/test-cnd.c b/tests/test-cnd.c index 61a86a859c..0aca5cdf07 100644 --- a/tests/test-cnd.c +++ b/tests/test-cnd.c @@ -59,7 +59,7 @@ /* * Condition check */ -static int cond_value = 0; +static int cond_value; static cnd_t condtest; static mtx_t lockcond; @@ -81,25 +81,31 @@ cnd_wait_routine (void *arg) static void test_cnd_wait () { - struct timespec remain; thrd_t thread; int ret; - remain.tv_sec = 2; - remain.tv_nsec = 0; - cond_value = 0; + /* Create a separate thread. */ ASSERT (thrd_create (&thread, cnd_wait_routine, NULL) == thrd_success); - do - { - yield (); - ret = thrd_sleep (&remain, &remain); - ASSERT (ret >= -1); - } - while (ret == -1 && (remain.tv_sec != 0 || remain.tv_nsec != 0)); - /* signal condition */ + /* Sleep for 2 seconds. */ + { + struct timespec remaining; + + remaining.tv_sec = 2; + remaining.tv_nsec = 0; + + do + { + yield (); + ret = thrd_sleep (&remaining, &remaining); + ASSERT (ret >= -1); + } + while (ret == -1 && (remaining.tv_sec != 0 || remaining.tv_nsec != 0)); + } + + /* Tell one of the waiting threads (if any) to continue. */ ASSERT (mtx_lock (&lockcond) == thrd_success); cond_value = 1; ASSERT (cnd_signal (&condtest) == thrd_success); @@ -115,8 +121,9 @@ test_cnd_wait () /* * Timed Condition check */ -static int cond_timeout; +static int cond_timed_out; +/* Stores in *TS the current time plus 1 second. */ static void get_ts (struct timespec *ts) { @@ -140,7 +147,7 @@ cnd_timedwait_routine (void *arg) get_ts (&ts); ret = cnd_timedwait (&condtest, &lockcond, &ts); if (ret == thrd_timedout) - cond_timeout = 1; + cond_timed_out = 1; } ASSERT (mtx_unlock (&lockcond) == thrd_success); @@ -150,25 +157,31 @@ cnd_timedwait_routine (void *arg) static void test_cnd_timedwait (void) { - struct timespec remain; thrd_t thread; int ret; - remain.tv_sec = 2; - remain.tv_nsec = 0; - - cond_value = cond_timeout = 0; + cond_value = cond_timed_out = 0; + /* Create a separate thread. */ ASSERT (thrd_create (&thread, cnd_timedwait_routine, NULL) == thrd_success); - do - { - yield (); - ret = thrd_sleep (&remain, &remain); - ASSERT (ret >= -1); - } - while (ret == -1 && (remain.tv_sec != 0 || remain.tv_nsec != 0)); - /* signal condition */ + /* Sleep for 2 seconds. */ + { + struct timespec remaining; + + remaining.tv_sec = 2; + remaining.tv_nsec = 0; + + do + { + yield (); + ret = thrd_sleep (&remaining, &remaining); + ASSERT (ret >= -1); + } + while (ret == -1 && (remaining.tv_sec != 0 || remaining.tv_nsec != 0)); + } + + /* Tell one of the waiting threads (if any) to continue. */ ASSERT (mtx_lock (&lockcond) == thrd_success); cond_value = 1; ASSERT (cnd_signal (&condtest) == thrd_success); @@ -176,7 +189,7 @@ test_cnd_timedwait (void) ASSERT (thrd_join (thread, NULL) == thrd_success); - if (!cond_timeout) + if (!cond_timed_out) abort (); } diff --git a/tests/test-cond.c b/tests/test-cond.c index a9b1e03406..eb92580e44 100644 --- a/tests/test-cond.c +++ b/tests/test-cond.c @@ -59,7 +59,7 @@ /* * Condition check */ -static int cond_value = 0; +static int cond_value; gl_cond_define_initialized(static, condtest) gl_lock_define_initialized(static, lockcond) @@ -81,20 +81,26 @@ cond_routine (void *arg) static void test_cond () { - int remain = 2; gl_thread_t thread; cond_value = 0; + /* Create a separate thread. */ thread = gl_thread_create (cond_routine, NULL); - do - { - yield (); - remain = sleep (remain); - } - while (remain); - /* signal condition */ + /* Sleep for 2 seconds. */ + { + int remaining = 2; + + do + { + yield (); + remaining = sleep (remaining); + } + while (remaining); + } + + /* Tell one of the waiting threads (if any) to continue. */ gl_lock_lock (lockcond); cond_value = 1; gl_cond_signal (condtest); @@ -110,8 +116,9 @@ test_cond () /* * Timed Condition check */ -static int cond_timeout; +static int cond_timed_out; +/* Stores in *TS the current time plus 1 second. */ static void get_ts (struct timespec *ts) { @@ -135,7 +142,7 @@ timedcond_routine (void *arg) get_ts (&ts); ret = glthread_cond_timedwait (&condtest, &lockcond, &ts); if (ret == ETIMEDOUT) - cond_timeout = 1; + cond_timed_out = 1; } gl_lock_unlock (lockcond); @@ -145,20 +152,26 @@ timedcond_routine (void *arg) static void test_timedcond (void) { - int remain = 2; gl_thread_t thread; - cond_value = cond_timeout = 0; + cond_value = cond_timed_out = 0; + /* Create a separate thread. */ thread = gl_thread_create (timedcond_routine, NULL); - do - { - yield (); - remain = sleep (remain); - } - while (remain); - /* signal condition */ + /* Sleep for 2 seconds. */ + { + int remaining = 2; + + do + { + yield (); + remaining = sleep (remaining); + } + while (remaining); + } + + /* Tell one of the waiting threads (if any) to continue. */ gl_lock_lock (lockcond); cond_value = 1; gl_cond_signal (condtest); @@ -166,7 +179,7 @@ test_timedcond (void) gl_thread_join (thread, NULL); - if (!cond_timeout) + if (!cond_timed_out) abort (); } diff --git a/tests/test-pthread-cond.c b/tests/test-pthread-cond.c index 145d4b4666..40cba9e548 100644 --- a/tests/test-pthread-cond.c +++ b/tests/test-pthread-cond.c @@ -64,7 +64,7 @@ /* * Condition check */ -static int cond_value = 0; +static int cond_value; static pthread_cond_t condtest; static pthread_mutex_t lockcond; @@ -86,25 +86,31 @@ pthread_cond_wait_routine (void *arg) static void test_pthread_cond_wait () { - struct timespec remain; pthread_t thread; int ret; - remain.tv_sec = 2; - remain.tv_nsec = 0; - cond_value = 0; + /* Create a separate thread. */ ASSERT (pthread_create (&thread, NULL, pthread_cond_wait_routine, NULL) == 0); - do - { - yield (); - ret = nanosleep (&remain, &remain); - ASSERT (ret >= -1); - } - while (ret == -1 && (remain.tv_sec != 0 || remain.tv_nsec != 0)); - /* signal condition */ + /* Sleep for 2 seconds. */ + { + struct timespec remaining; + + remaining.tv_sec = 2; + remaining.tv_nsec = 0; + + do + { + yield (); + ret = nanosleep (&remaining, &remaining); + ASSERT (ret >= -1); + } + while (ret == -1 && (remaining.tv_sec != 0 || remaining.tv_nsec != 0)); + } + + /* Tell one of the waiting threads (if any) to continue. */ ASSERT (pthread_mutex_lock (&lockcond) == 0); cond_value = 1; ASSERT (pthread_cond_signal (&condtest) == 0); @@ -120,8 +126,9 @@ test_pthread_cond_wait () /* * Timed Condition check */ -static int cond_timeout; +static int cond_timed_out; +/* Stores in *TS the current time plus 1 second. */ static void get_ts (struct timespec *ts) { @@ -145,7 +152,7 @@ pthread_cond_timedwait_routine (void *arg) get_ts (&ts); ret = pthread_cond_timedwait (&condtest, &lockcond, &ts); if (ret == ETIMEDOUT) - cond_timeout = 1; + cond_timed_out = 1; } ASSERT (pthread_mutex_unlock (&lockcond) == 0); @@ -155,26 +162,32 @@ pthread_cond_timedwait_routine (void *arg) static void test_pthread_cond_timedwait (void) { - struct timespec remain; pthread_t thread; int ret; - remain.tv_sec = 2; - remain.tv_nsec = 0; - - cond_value = cond_timeout = 0; + cond_value = cond_timed_out = 0; + /* Create a separate thread. */ ASSERT (pthread_create (&thread, NULL, pthread_cond_timedwait_routine, NULL) == 0); - do - { - yield (); - ret = nanosleep (&remain, &remain); - ASSERT (ret >= -1); - } - while (ret == -1 && (remain.tv_sec != 0 || remain.tv_nsec != 0)); - /* signal condition */ + /* Sleep for 2 seconds. */ + { + struct timespec remaining; + + remaining.tv_sec = 2; + remaining.tv_nsec = 0; + + do + { + yield (); + ret = nanosleep (&remaining, &remaining); + ASSERT (ret >= -1); + } + while (ret == -1 && (remaining.tv_sec != 0 || remaining.tv_nsec != 0)); + } + + /* Tell one of the waiting threads (if any) to continue. */ ASSERT (pthread_mutex_lock (&lockcond) == 0); cond_value = 1; ASSERT (pthread_cond_signal (&condtest) == 0); @@ -182,7 +195,7 @@ test_pthread_cond_timedwait (void) ASSERT (pthread_join (thread, NULL) == 0); - if (!cond_timeout) + if (!cond_timed_out) abort (); }