[4/5] nptl: Make test-cond-printers check the number of waiters

Message ID 20210116204950.16434-4-malteskarupke@web.de
State Superseded
Delegated to: Carlos O'Donell
Headers
Series [1/5] nptl: Fix pthread_cond_signal missing a sleeper (#BZ 25847) |

Commit Message

Malte Skarupke Jan. 16, 2021, 8:49 p.m. UTC
  In my last change I changed the semantics of how to determine the
number of waiters on a condition variable. The existing test only
tested that the printers print something. They didn't cover the case
when there is a thread sleeping on the condition variable. In this
patch I changed the test to ensure that the correct number is printed.

This is just to double-check the changes from my previous patch.
---
 nptl/test-cond-printers.c  | 56 +++++++++++++++++++++++++++++++++-----
 nptl/test-cond-printers.py |  5 ++++
 2 files changed, 54 insertions(+), 7 deletions(-)

--
2.17.1
  

Patch

diff --git a/nptl/test-cond-printers.c b/nptl/test-cond-printers.c
index 4b6db831f9..603a7cccee 100644
--- a/nptl/test-cond-printers.c
+++ b/nptl/test-cond-printers.c
@@ -26,7 +26,14 @@ 
 #define PASS 0
 #define FAIL 1

-static int test_status_destroyed (pthread_cond_t *condvar);
+static int test_status (pthread_cond_t *condvar);
+
+typedef struct
+{
+  pthread_mutex_t *mutex;
+  pthread_cond_t *condvar;
+  int *wait_thread_asleep;
+} test_state;

 int
 main (void)
@@ -36,22 +43,57 @@  main (void)
   int result = FAIL;

   if (pthread_condattr_init (&attr) == 0
-      && test_status_destroyed (&condvar) == PASS)
+      && test_status (&condvar) == PASS)
     result = PASS;
   /* Else, one of the pthread_cond* functions failed.  */

   return result;
 }

+static void *
+wait (void *arg)
+{
+  test_state *state = (test_state *)arg;
+  void *result = PASS;
+  if (pthread_mutex_lock (state->mutex) != 0)
+    result = (void *)FAIL;
+  *state->wait_thread_asleep = 1;
+  if (pthread_cond_signal (state->condvar) != 0)
+    result = (void *)FAIL;
+  if (pthread_cond_wait (state->condvar, state->mutex) != 0)
+    result = (void *)FAIL;
+  if (pthread_mutex_unlock (state->mutex) != 0)
+    result = (void *)FAIL;
+  return result;
+}
+
 /* Initializes CONDVAR, then destroys it.  */
 static int
-test_status_destroyed (pthread_cond_t *condvar)
+test_status (pthread_cond_t *condvar)
 {
-  int result = FAIL;
+  int result = PASS;

-  if (pthread_cond_init (condvar, NULL) == 0
-      && pthread_cond_destroy (condvar) == 0)
-    result = PASS; /* Test status (destroyed).  */
+  pthread_mutex_t mutex;
+  result |= pthread_mutex_init (&mutex, NULL);
+  result |= pthread_cond_init (condvar, NULL);
+  int wait_thread_asleep = 0;
+  test_state state = { &mutex, condvar, &wait_thread_asleep };
+  result |= pthread_mutex_lock (&mutex);
+  pthread_t thread;
+  result |= pthread_create (&thread, NULL, wait, &state);
+  while (!wait_thread_asleep)
+    {
+      result |= pthread_cond_wait (condvar, &mutex);
+    }
+  result |= pthread_cond_signal (condvar); /* Test about to signal */
+  result |= pthread_mutex_unlock (&mutex);
+  result |= pthread_cond_destroy (condvar);
+  void *retval = NULL;
+  result |= pthread_join (thread, &retval);  /* Test status (destroyed).  */
+  result |= pthread_mutex_destroy (&mutex);
+  result = result ? FAIL : PASS;
+  if (retval != NULL)
+    result = FAIL;

   return result;
 }
diff --git a/nptl/test-cond-printers.py b/nptl/test-cond-printers.py
index 38e2da4269..ab12218802 100644
--- a/nptl/test-cond-printers.py
+++ b/nptl/test-cond-printers.py
@@ -33,6 +33,11 @@  try:
     var = 'condvar'
     to_string = 'pthread_cond_t'

+    break_at(test_source, 'Test about to signal')
+    continue_cmd() # Go to test_status_destroyed
+    test_printer(var, to_string, {'Threads known to still execute a wait function': '1'})
+
+
     break_at(test_source, 'Test status (destroyed)')
     continue_cmd() # Go to test_status_destroyed
     test_printer(var, to_string, {'Threads known to still execute a wait function': '0'})