]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
nptl: Convert various tests to use libsupport
authorMike Crowe <mac@mcrowe.com>
Wed, 19 Jun 2019 21:07:58 +0000 (18:07 -0300)
committerAdhemerval Zanella <adhemerval.zanella@linaro.org>
Fri, 21 Jun 2019 12:59:50 +0000 (09:59 -0300)
* nptl/eintr.c: Use libsupport.
* nptl/tst-eintr1.c: Likewise.
* nptl/tst-eintr2.c: Likewise.
* nptl/tst-eintr3.c: Likewise.
* nptl/tst-eintr4.c: Likewise.
* nptl/tst-eintr5.c: Likewise.
* nptl/tst-mutex-errorcheck.c: Likewise.
* nptl/tst-mutex5.c: Likewise.

ChangeLog
nptl/eintr.c
nptl/tst-eintr1.c
nptl/tst-eintr2.c
nptl/tst-eintr3.c
nptl/tst-eintr4.c
nptl/tst-eintr5.c
nptl/tst-mutex-errorcheck.c
nptl/tst-mutex5.c
nptl/tst-mutex9.c

index a67ffe7c538337a9f76f9eddcf5545afd6afbe14..d09b70b28783d6cf78b6f7b94f74dc3315a3cd2f 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,14 @@
 2019-06-20  Mike Crowe  <mac@mcrowe.com>
 
+       * nptl/eintr.c: Use libsupport.
+       * nptl/tst-eintr1.c: Likewise.
+       * nptl/tst-eintr2.c: Likewise.
+       * nptl/tst-eintr3.c: Likewise.
+       * nptl/tst-eintr4.c: Likewise.
+       * nptl/tst-eintr5.c: Likewise.
+       * nptl/tst-mutex-errorcheck.c: Likewise.
+       * nptl/tst-mutex5.c: Likewise.
+
        * support/test-driver.h: Add verbose_printf macro.
 
        * support/xtime.h: Add xclock_now() helper function.
index 4fc4d1c85c6aa1e32670834f9b34818f4dd1a876..6e644d2594dcf6a3061bd83e6cebfcde8f10b9ec 100644 (file)
@@ -19,6 +19,9 @@
 #include <pthread.h>
 #include <signal.h>
 #include <unistd.h>
+#include <support/xthread.h>
+#include <support/xsignal.h>
+#include <support/xthread.h>
 
 
 static int the_sig;
@@ -46,7 +49,7 @@ eintr_source (void *arg)
       sigset_t ss;
       sigemptyset (&ss);
       sigaddset (&ss, the_sig);
-      pthread_sigmask (SIG_BLOCK, &ss, NULL);
+      xpthread_sigmask (SIG_BLOCK, &ss, NULL);
     }
 
   while (1)
@@ -79,10 +82,5 @@ setup_eintr (int sig, pthread_t *thp)
   the_sig = sig;
 
   /* Create the thread which will fire off the signals.  */
-  pthread_t th;
-  if (pthread_create (&th, NULL, eintr_source, thp) != 0)
-    {
-      puts ("setup_eintr: pthread_create failed");
-      exit (1);
-    }
+  xpthread_create (NULL, eintr_source, thp);
 }
index 8134f0affd596d470fb7d7beaac0ca98e701f7e0..b60b796d619713f0e6f293974b2e0b85589b94e7 100644 (file)
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-
-static int do_test (void);
-
-#define TEST_FUNCTION do_test ()
-#include "../test-skeleton.c"
+#include <support/check.h>
+#include <support/xthread.h>
 
 #include "eintr.c"
 
@@ -43,37 +40,8 @@ tf1 (void *arg)
 {
   while (1)
     {
-      pthread_t th;
-
-      int e = pthread_create (&th, NULL, tf2, NULL);
-      if (e != 0)
-       {
-         if (e == EINTR)
-           {
-             puts ("pthread_create returned EINTR");
-             exit (1);
-           }
-
-         char buf[100];
-         printf ("tf1: pthread_create failed: %s\n",
-                 strerror_r (e, buf, sizeof (buf)));
-         exit (1);
-       }
-
-      e = pthread_join (th, NULL);
-      if (e != 0)
-       {
-         if (e == EINTR)
-           {
-             puts ("pthread_join returned EINTR");
-             exit (1);
-           }
-
-         char buf[100];
-         printf ("tf1: pthread_join failed: %s\n",
-                 strerror_r (e, buf, sizeof (buf)));
-         exit (1);
-       }
+      pthread_t th = xpthread_create (NULL, tf2, NULL);
+      xpthread_join (th);
     }
 }
 
@@ -86,15 +54,7 @@ do_test (void)
   int i;
   for (i = 0; i < 10; ++i)
     {
-      pthread_t th;
-      int e = pthread_create (&th, NULL, tf1, NULL);
-      if (e != 0)
-       {
-         char buf[100];
-         printf ("main: pthread_create failed: %s\n",
-                 strerror_r (e, buf, sizeof (buf)));
-         exit (1);
-       }
+      pthread_t th = xpthread_create (NULL, tf1, NULL);
     }
 
   delayed_exit (3);
@@ -102,3 +62,5 @@ do_test (void)
   (void) tf1 (NULL);
   return 1;
 }
+
+#include <support/test-driver.c>
index 12e2715fbd0368950ca28f6b6c424d2d602392ae..c78c19ab33e6bc8d81f3fb6f513aab2243865b72 100644 (file)
 #include <stdlib.h>
 #include <string.h>
 #include <sys/time.h>
-
-static int do_test (void);
-
-#define TEST_FUNCTION do_test ()
-#include "../test-skeleton.c"
+#include <support/check.h>
+#include <support/timespec.h>
+#include <support/xtime.h>
 
 #include "eintr.c"
 
@@ -39,12 +37,8 @@ static pthread_mutex_t m2 = PTHREAD_MUTEX_INITIALIZER;
 static void *
 tf1 (void *arg)
 {
-  struct timespec ts;
-  struct timeval tv;
-
-  gettimeofday (&tv, NULL);
-  TIMEVAL_TO_TIMESPEC (&tv, &ts);
-  ts.tv_sec += 10000;
+  struct timespec ts = timespec_add (xclock_now (CLOCK_REALTIME),
+                                     make_timespec (10000, 0));
 
   /* This call must never return.  */
   int e = pthread_mutex_timedlock (&m1, &ts);
@@ -61,58 +55,34 @@ tf2 (void *arg)
 {
   while (1)
     {
-      int e = pthread_mutex_lock (&m2);
-      if (e != 0)
-       {
-         puts ("tf2: mutex_lock failed");
-         exit (1);
-       }
-      e = pthread_mutex_unlock (&m2);
-      if (e != 0)
-       {
-         puts ("tf2: mutex_unlock failed");
-         exit (1);
-       }
+      TEST_COMPARE (pthread_mutex_lock (&m2), 0);
+      TEST_COMPARE (pthread_mutex_unlock (&m2), 0);
+
       struct timespec ts = { .tv_sec = 0, .tv_nsec = 10000000 };
       nanosleep (&ts, NULL);
     }
+  return NULL;
 }
 
 
 static int
 do_test (void)
 {
-  if (pthread_mutex_lock (&m1) != 0)
-    {
-      puts ("mutex_lock failed");
-      exit (1);
-    }
+  TEST_COMPARE (pthread_mutex_lock (&m1), 0);
 
   setup_eintr (SIGUSR1, NULL);
 
-  pthread_t th;
   char buf[100];
-  int e = pthread_create (&th, NULL, tf1, NULL);
-  if (e != 0)
-    {
-      printf ("main: 1st pthread_create failed: %s\n",
-             strerror_r (e, buf, sizeof (buf)));
-      exit (1);
-    }
-
-  e = pthread_create (&th, NULL, tf2, NULL);
-  if (e != 0)
-    {
-      printf ("main: 2nd pthread_create failed: %s\n",
-             strerror_r (e, buf, sizeof (buf)));
-      exit (1);
-    }
+  xpthread_create (NULL, tf1, NULL);
+  xpthread_create (NULL, tf2, NULL);
 
   delayed_exit (3);
   /* This call must never return.  */
-  e = pthread_mutex_lock (&m1);
+  int e = pthread_mutex_lock (&m1);
   printf ("main: mutex_lock returned: %s\n",
          strerror_r (e, buf, sizeof (buf)));
 
   return 1;
 }
+
+#include <support/test-driver.c>
index d6c4e2bf0df2ebca7d5ae774b5b5e21f944bcfc3..0562eba4b4a11a714e640d19ff7a3a74a9902f98 100644 (file)
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-
-static int do_test (void);
-
-#define TEST_FUNCTION do_test ()
-#include "../test-skeleton.c"
+#include <support/check.h>
+#include <support/xthread.h>
 
 #include "eintr.c"
 
@@ -35,9 +32,9 @@ static void *
 tf (void *arg)
 {
   pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
-  pthread_mutex_lock (&m);
+  xpthread_mutex_lock (&m);
   /* This call must not return.  */
-  pthread_mutex_lock (&m);
+  xpthread_mutex_lock (&m);
 
   puts ("tf: mutex_lock returned");
   exit (1);
@@ -51,15 +48,7 @@ do_test (void)
 
   setup_eintr (SIGUSR1, &self);
 
-  pthread_t th;
-  char buf[100];
-  int e = pthread_create (&th, NULL, tf, NULL);
-  if (e != 0)
-    {
-      printf ("main: pthread_create failed: %s\n",
-             strerror_r (e, buf, sizeof (buf)));
-      exit (1);
-    }
+  pthread_t th = xpthread_create (NULL, tf, NULL);
 
   delayed_exit (1);
   /* This call must never return.  */
@@ -67,3 +56,5 @@ do_test (void)
   puts ("error: pthread_join returned");
   return 1;
 }
+
+#include <support/test-driver.c>
index 2d9b2997052ebcf01564d4da04573ccd40a9a5c6..1f4b711a3cad4e343aa852d4a425ee938bc8e83a 100644 (file)
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-
-static int do_test (void);
-
-#define TEST_FUNCTION do_test ()
-#include "../test-skeleton.c"
+#include <support/check.h>
+#include <support/xthread.h>
 
 #include "eintr.c"
 
@@ -39,11 +36,7 @@ do_test (void)
   setup_eintr (SIGUSR1, &self);
 
   pthread_barrier_t b;
-  if (pthread_barrier_init (&b, NULL, 2) != 0)
-    {
-      puts ("barrier_init failed");
-      exit (1);
-    }
+  xpthread_barrier_init (&b, NULL, 2);
 
   delayed_exit (1);
   /* This call must never return.  */
@@ -51,3 +44,5 @@ do_test (void)
   puts ("error: pthread_barrier_wait returned");
   return 1;
 }
+
+#include <support/test-driver.c>
index 6eee2a166f80674b9d44600e0180d8c175178fa3..da049c4eae018723db9abd8b82666f395b15760b 100644 (file)
 #include <stdlib.h>
 #include <string.h>
 #include <sys/time.h>
-
-static int do_test (void);
-
-#define TEST_FUNCTION do_test ()
-#include "../test-skeleton.c"
+#include <support/check.h>
+#include <support/timespec.h>
+#include <support/xthread.h>
+#include <support/xtime.h>
 
 #include "eintr.c"
 
@@ -39,20 +38,12 @@ static pthread_cond_t c = PTHREAD_COND_INITIALIZER;
 static void *
 tf (void *arg)
 {
-  struct timespec ts;
-  struct timeval tv;
-
-  gettimeofday (&tv, NULL);
-  TIMEVAL_TO_TIMESPEC (&tv, &ts);
-  ts.tv_sec += 10000;
+  struct timespec ts = timespec_add (xclock_now (CLOCK_REALTIME),
+                                     make_timespec (10000, 0));
 
   /* This call must never return.  */
-  int e = pthread_cond_timedwait (&c, &m, &ts);
-  char buf[100];
-  printf ("tf: cond_timedwait returned: %s\n",
-         strerror_r (e, buf, sizeof (buf)));
-
-  exit (1);
+  TEST_COMPARE (pthread_cond_timedwait (&c, &m, &ts), 0);
+  FAIL_EXIT1 ("pthread_cond_timedwait returned unexpectedly\n");
 }
 
 
@@ -61,19 +52,12 @@ do_test (void)
 {
   setup_eintr (SIGUSR1, NULL);
 
-  pthread_t th;
-  char buf[100];
-  int e = pthread_create (&th, NULL, tf, NULL);
-  if (e != 0)
-    {
-      printf ("main: pthread_create failed: %s\n",
-             strerror_r (e, buf, sizeof (buf)));
-      exit (1);
-    }
+  xpthread_create (NULL, tf, NULL);
 
   delayed_exit (3);
   /* This call must never return.  */
   xpthread_cond_wait (&c, &m);
-  puts ("error: pthread_cond_wait returned");
-  return 1;
+  FAIL_RET ("error: pthread_cond_wait returned");
 }
+
+#include <support/test-driver.c>
index 30ff7ea64a494e2c3b2a19010309de05d58e7945..afc27e7472eca4dd2e9af8ca9b32923a84eae42c 100644 (file)
@@ -20,6 +20,7 @@
 #include <errno.h>
 #include <time.h>
 #include <pthread.h>
+#include <support/check.h>
 
 static int
 do_test (void)
@@ -29,33 +30,23 @@ do_test (void)
   pthread_mutexattr_t mutexattr;
   int ret = 0;
 
-  if (pthread_mutexattr_init (&mutexattr) != 0)
-    return 1;
-  if (pthread_mutexattr_settype (&mutexattr, PTHREAD_MUTEX_ERRORCHECK) != 0)
-    return 1;
+  TEST_COMPARE (pthread_mutexattr_init (&mutexattr), 0);
+  TEST_COMPARE (pthread_mutexattr_settype (&mutexattr,
+                                           PTHREAD_MUTEX_ERRORCHECK), 0);
 
-  if (pthread_mutex_init (&mutex, &mutexattr) != 0)
-    return 1;
-  if (pthread_mutexattr_destroy (&mutexattr) != 0)
-    return 1;
+  TEST_COMPARE (pthread_mutex_init (&mutex, &mutexattr), 0);
+  TEST_COMPARE (pthread_mutexattr_destroy (&mutexattr), 0);
 
   /* The call to pthread_mutex_timedlock erroneously enabled lock elision
      on the mutex, which then triggered an assertion failure in
      pthread_mutex_unlock.  It would also defeat the error checking nature
      of the mutex.  */
-  if (pthread_mutex_timedlock (&mutex, &tms) != 0)
-    return 1;
-  if (pthread_mutex_timedlock (&mutex, &tms) != EDEADLK)
-    {
-      printf ("Failed error checking on locked mutex\n");
-      ret = 1;
-    }
+  TEST_COMPARE (pthread_mutex_timedlock (&mutex, &tms), 0);
+  TEST_COMPARE (pthread_mutex_timedlock (&mutex, &tms), EDEADLK);
 
-  if (pthread_mutex_unlock (&mutex) != 0)
-    ret = 1;
+  TEST_COMPARE (pthread_mutex_unlock (&mutex), 0);
 
   return ret;
 }
 
-#define TEST_FUNCTION do_test ()
-#include "../test-skeleton.c"
+#include <support/test-driver.c>
index 4e1d93b3e18efe5b324ca752625fd4b1f98bd335..fb27152ecc3a1c714cfcff0288de6850d208f5c4 100644 (file)
@@ -24,6 +24,8 @@
 #include <sys/time.h>
 #include <stdint.h>
 #include <config.h>
+#include <support/check.h>
+#include <support/timespec.h>
 
 
 #ifndef TYPE
@@ -35,168 +37,60 @@ static int
 do_test (void)
 {
   pthread_mutex_t m;
-  struct timespec ts;
-  struct timeval tv;
-  struct timeval tv2;
-  int err;
   pthread_mutexattr_t a;
 
-  if (pthread_mutexattr_init (&a) != 0)
-    {
-      puts ("mutexattr_init failed");
-      return 1;
-    }
-
-  if (pthread_mutexattr_settype (&a, TYPE) != 0)
-    {
-      puts ("mutexattr_settype failed");
-      return 1;
-    }
+  TEST_COMPARE (pthread_mutexattr_init (&a), 0);
+  TEST_COMPARE (pthread_mutexattr_settype (&a, TYPE), 0);
 
 #ifdef ENABLE_PI
-  if (pthread_mutexattr_setprotocol (&a, PTHREAD_PRIO_INHERIT) != 0)
-    {
-      puts ("pthread_mutexattr_setprotocol failed");
-      return 1;
-    }
+  TEST_COMPARE (pthread_mutexattr_setprotocol (&a, PTHREAD_PRIO_INHERIT), 0);
 #endif
 
-  err = pthread_mutex_init (&m, &a);
+  int err = pthread_mutex_init (&m, &a);
   if (err != 0)
     {
 #ifdef ENABLE_PI
       if (err == ENOTSUP)
-       {
-         puts ("PI mutexes unsupported");
-         return 0;
-       }
+        FAIL_UNSUPPORTED ("PI mutexes unsupported");
 #endif
-      puts ("mutex_init failed");
-      return 1;
-    }
-
-  if (pthread_mutexattr_destroy (&a) != 0)
-    {
-      puts ("mutexattr_destroy failed");
-      return 1;
-    }
-
-  if (pthread_mutex_lock (&m) != 0)
-    {
-      puts ("mutex_lock failed");
-      return 1;
+      FAIL_EXIT1 ("mutex_init failed");
     }
 
+  TEST_COMPARE (pthread_mutexattr_destroy (&a), 0);
+  TEST_COMPARE (pthread_mutex_lock (&m), 0);
   if (pthread_mutex_trylock (&m) == 0)
-    {
-      puts ("mutex_trylock succeeded");
-      return 1;
-    }
+    FAIL_EXIT1 ("mutex_trylock succeeded");
 
-  gettimeofday (&tv, NULL);
-  TIMEVAL_TO_TIMESPEC (&tv, &ts);
-
-  ts.tv_sec += 2;      /* Wait 2 seconds.  */
-
-  err = pthread_mutex_timedlock (&m, &ts);
-  if (err == 0)
-    {
-      puts ("timedlock succeeded");
-      return 1;
-    }
-  else if (err != ETIMEDOUT)
-    {
-      printf ("timedlock error != ETIMEDOUT: %d\n", err);
-      return 1;
-    }
-  else
-    {
-      int clk_tck = sysconf (_SC_CLK_TCK);
-
-      gettimeofday (&tv2, NULL);
-
-      tv2.tv_sec -= tv.tv_sec;
-      tv2.tv_usec -= tv.tv_usec;
-      if (tv2.tv_usec < 0)
-       {
-         tv2.tv_usec += 1000000;
-         tv2.tv_sec -= 1;
-       }
-
-      /* Be a bit tolerant, add one CLK_TCK.  */
-      tv2.tv_usec += 1000000 / clk_tck;
-      if (tv2.tv_usec >= 1000000)
-       {
-         tv2.tv_usec -= 1000000;
-         ++tv2.tv_sec;
-       }
-
-      if (tv2.tv_sec < 2)
-       {
-         printf ("premature timeout: %jd.%06jd difference\n",
-                 (intmax_t) tv2.tv_sec, (intmax_t) tv2.tv_usec);
-         return 1;
-       }
-    }
+  /* Wait 2 seconds.  */
+  struct timespec ts_timeout = timespec_add (xclock_now (CLOCK_REALTIME),
+                                             make_timespec (2, 0));
 
-  (void) gettimeofday (&tv, NULL);
-  TIMEVAL_TO_TIMESPEC (&tv, &ts);
+  TEST_COMPARE (pthread_mutex_timedlock (&m, &ts_timeout), ETIMEDOUT);
+  TEST_TIMESPEC_BEFORE_NOW (ts_timeout, CLOCK_REALTIME);
 
-  ts.tv_sec += 2;      /* Wait 2 seconds.  */
   /* The following makes the ts value invalid.  */
-  ts.tv_nsec += 1000000000;
+  ts_timeout.tv_nsec += 1000000000;
 
-  err = pthread_mutex_timedlock (&m, &ts);
-  if (err == 0)
-    {
-      puts ("2nd timedlock succeeded");
-      return 1;
-    }
-  else if (err != EINVAL)
-    {
-      printf ("2nd timedlock error != EINVAL: %d\n", err);
-      return 1;
-    }
+  TEST_COMPARE (pthread_mutex_timedlock (&m, &ts_timeout), EINVAL);
+  TEST_COMPARE (pthread_mutex_unlock (&m), 0);
 
-  if (pthread_mutex_unlock (&m) != 0)
-    {
-      puts ("mutex_unlock failed");
-      return 1;
-    }
+  const struct timespec ts_start = xclock_now (CLOCK_REALTIME);
 
-  (void) gettimeofday (&tv, NULL);
-  TIMEVAL_TO_TIMESPEC (&tv, &ts);
+  /* Wait 2 seconds.  */
+  ts_timeout = timespec_add (ts_start, make_timespec (2, 0));
 
-  ts.tv_sec += 2;      /* Wait 2 seconds.  */
-  if (pthread_mutex_timedlock (&m, &ts) != 0)
-    {
-      puts ("3rd timedlock failed");
-    }
+  TEST_COMPARE (pthread_mutex_timedlock (&m, &ts_timeout), 0);
 
-  (void) gettimeofday (&tv2, NULL);
+  const struct timespec ts_end = xclock_now (CLOCK_REALTIME);
 
   /* Check that timedlock didn't delay.  We use a limit of 0.1 secs.  */
-  timersub (&tv2, &tv, &tv2);
-  if (tv2.tv_sec > 0 || tv2.tv_usec > 100000)
-    {
-      puts ("3rd timedlock didn't return right away");
-      return 1;
-    }
+  TEST_TIMESPEC_BEFORE (ts_end,
+                        timespec_add (ts_start, make_timespec (0, 100000000)));
 
-  if (pthread_mutex_unlock (&m) != 0)
-    {
-      puts ("final mutex_unlock failed");
-      return 1;
-    }
-
-  if (pthread_mutex_destroy (&m) != 0)
-    {
-      puts ("mutex_destroy failed");
-      return 1;
-    }
+  TEST_COMPARE (pthread_mutex_unlock (&m), 0);
+  TEST_COMPARE (pthread_mutex_destroy (&m), 0);
 
   return 0;
 }
 
-#define TEST_FUNCTION do_test ()
-#include "../test-skeleton.c"
+#include <support/test-driver.c>
index 917276e38074861adeddc8d52389ed11550caa2c..e9fd8e2859497a778fa78f4cde185d286944238c 100644 (file)
@@ -26,6 +26,9 @@
 #include <sys/mman.h>
 #include <sys/time.h>
 #include <sys/wait.h>
+#include <support/check.h>
+#include <support/timespec.h>
+#include <support/xunistd.h>
 
 
 static int
@@ -42,10 +45,7 @@ do_test (void)
 
   fd = mkstemp (tmpfname);
   if (fd == -1)
-    {
-      printf ("cannot open temporary file: %m\n");
-      return 1;
-    }
+      FAIL_EXIT1 ("cannot open temporary file: %m\n");
 
   /* Make sure it is always removed.  */
   unlink (tmpfname);
@@ -54,46 +54,21 @@ do_test (void)
   memset (data, '\0', ps);
 
   /* Write the data to the file.  */
-  if (write (fd, data, ps) != (ssize_t) ps)
-    {
-      puts ("short write");
-      return 1;
-    }
+  xwrite (fd, data, ps);
 
-  mem = mmap (NULL, ps, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
-  if (mem == MAP_FAILED)
-    {
-      printf ("mmap failed: %m\n");
-      return 1;
-    }
+  mem = xmmap (NULL, ps, PROT_READ | PROT_WRITE, MAP_SHARED, fd);
 
   m = (pthread_mutex_t *) (((uintptr_t) mem + __alignof (pthread_mutex_t))
                           & ~(__alignof (pthread_mutex_t) - 1));
 
-  if (pthread_mutexattr_init (&a) != 0)
-    {
-      puts ("mutexattr_init failed");
-      return 1;
-    }
+  TEST_COMPARE (pthread_mutexattr_init (&a), 0);
 
-  if (pthread_mutexattr_setpshared (&a, PTHREAD_PROCESS_SHARED) != 0)
-    {
-      puts ("mutexattr_setpshared failed");
-      return 1;
-    }
+  TEST_COMPARE (pthread_mutexattr_setpshared (&a, PTHREAD_PROCESS_SHARED), 0);
 
-  if (pthread_mutexattr_settype (&a, PTHREAD_MUTEX_RECURSIVE) != 0)
-    {
-      puts ("mutexattr_settype failed");
-      return 1;
-    }
+  TEST_COMPARE (pthread_mutexattr_settype (&a, PTHREAD_MUTEX_RECURSIVE), 0);
 
 #ifdef ENABLE_PI
-  if (pthread_mutexattr_setprotocol (&a, PTHREAD_PRIO_INHERIT) != 0)
-    {
-      puts ("pthread_mutexattr_setprotocol failed");
-      return 1;
-    }
+  TEST_COMPARE (pthread_mutexattr_setprotocol (&a, PTHREAD_PRIO_INHERIT), 0);
 #endif
 
   int e;
@@ -101,70 +76,29 @@ do_test (void)
     {
 #ifdef ENABLE_PI
       if (e == ENOTSUP)
-       {
-         puts ("PI mutexes unsupported");
-         return 0;
-       }
+        FAIL_UNSUPPORTED ("PI mutexes unsupported");
 #endif
-      puts ("mutex_init failed");
-      return 1;
+      FAIL_EXIT1 ("mutex_init failed");
     }
 
-  if (pthread_mutex_lock (m) != 0)
-    {
-      puts ("mutex_lock failed");
-      return 1;
-    }
+  TEST_COMPARE (pthread_mutex_lock (m), 0);
 
-  if (pthread_mutexattr_destroy (&a) != 0)
-    {
-      puts ("mutexattr_destroy failed");
-      return 1;
-    }
+  TEST_COMPARE (pthread_mutexattr_destroy (&a), 0);
 
   puts ("going to fork now");
-  pid = fork ();
-  if (pid == -1)
-    {
-      puts ("fork failed");
-      return 1;
-    }
-  else if (pid == 0)
+  pid = xfork ();
+  if (pid == 0)
     {
       if (pthread_mutex_trylock (m) == 0)
-       {
-         puts ("child: mutex_trylock succeeded");
-         exit (1);
-       }
+        FAIL_EXIT1 ("child: mutex_trylock succeeded");
 
       if (pthread_mutex_unlock (m) == 0)
-       {
-         puts ("child: mutex_unlock succeeded");
-         exit (1);
-       }
-
-      struct timeval tv;
-      gettimeofday (&tv, NULL);
-      struct timespec ts;
-      TIMEVAL_TO_TIMESPEC (&tv, &ts);
-      ts.tv_nsec += 500000000;
-      if (ts.tv_nsec >= 1000000000)
-       {
-         ++ts.tv_sec;
-         ts.tv_nsec -= 1000000000;
-       }
-
-      e = pthread_mutex_timedlock (m, &ts);
-      if (e == 0)
-       {
-         puts ("child: mutex_timedlock succeeded");
-         exit (1);
-       }
-      if (e != ETIMEDOUT)
-       {
-         puts ("child: mutex_timedlock didn't time out");
-         exit (1);
-       }
+        FAIL_EXIT1 ("child: mutex_unlock succeeded");
+
+      const struct timespec ts = timespec_add (xclock_now (CLOCK_REALTIME),
+                                               make_timespec (0, 500000000));
+
+      TEST_COMPARE (pthread_mutex_timedlock (m, &ts), ETIMEDOUT);
 
       alarm (1);
 
@@ -179,23 +113,12 @@ do_test (void)
 
   int status;
   if (TEMP_FAILURE_RETRY (waitpid (pid, &status, 0)) != pid)
-    {
-      puts ("waitpid failed");
-      return 1;
-    }
+    FAIL_EXIT1 ("waitpid failed");
   if (! WIFSIGNALED (status))
-    {
-      puts ("child not killed by signal");
-      return 1;
-    }
-  if (WTERMSIG (status) != SIGALRM)
-    {
-      puts ("child not killed by SIGALRM");
-      return 1;
-    }
+    FAIL_EXIT1 ("child not killed by signal");
+  TEST_COMPARE (WTERMSIG (status), SIGALRM);
 
   return 0;
 }
 
-#define TEST_FUNCTION do_test ()
-#include "../test-skeleton.c"
+#include <support/test-driver.c>