]> Savannah Git Hosting - gnulib.git/commitdiff
cond tests: Improve comments.
authorBruno Haible <bruno@clisp.org>
Fri, 28 Jun 2024 19:40:16 +0000 (21:40 +0200)
committerBruno Haible <bruno@clisp.org>
Fri, 28 Jun 2024 20:39:41 +0000 (22:39 +0200)
* 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.

ChangeLog
tests/test-cnd.c
tests/test-cond.c
tests/test-pthread-cond.c

index a9453b0b451378bff7d140656a1bf001c7440f9a..a309c59a503f04e03b650dc593af8f7b22b824a3 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,16 @@
+2024-06-28  Bruno Haible  <bruno@clisp.org>
+
+       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  <bruno@clisp.org>
 
        time: Fix test failure on FreeBSD.
index 61a86a859c76dba7a3261cf6ba5ea27d006b74c6..0aca5cdf07be172449f49cbdb3dee9b8f711582a 100644 (file)
@@ -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 ();
 }
 
index a9b1e03406be2abc098eb91c0edd14df4933bcd8..eb92580e448680d1bb296dadf13d768f4a7a6f7c 100644 (file)
@@ -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 ();
 }
 
index 145d4b4666b67f0743847816685c7fa6ec80e5d4..40cba9e548f91fc0ca7952f53e38da40bc574c1f 100644 (file)
@@ -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 ();
 }