about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMike Crowe <mac@mcrowe.com>2019-06-19 18:07:58 -0300
committerAdhemerval Zanella <adhemerval.zanella@linaro.org>2019-06-21 09:59:50 -0300
commitce5b73a7c3d8caefc72ac3b95490861e3ee13520 (patch)
treee6b816726b34c77c1b9569cacbb824dc4c960f94
parent8bf225d5832eda8fefce9361c53cb68a55150b96 (diff)
downloadglibc-ce5b73a7c3d8caefc72ac3b95490861e3ee13520.tar.gz
glibc-ce5b73a7c3d8caefc72ac3b95490861e3ee13520.tar.xz
glibc-ce5b73a7c3d8caefc72ac3b95490861e3ee13520.zip
nptl: Convert various tests to use libsupport
	* 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.
-rw-r--r--ChangeLog9
-rw-r--r--nptl/eintr.c12
-rw-r--r--nptl/tst-eintr1.c52
-rw-r--r--nptl/tst-eintr2.c60
-rw-r--r--nptl/tst-eintr3.c23
-rw-r--r--nptl/tst-eintr4.c15
-rw-r--r--nptl/tst-eintr5.c40
-rw-r--r--nptl/tst-mutex-errorcheck.c29
-rw-r--r--nptl/tst-mutex5.c164
-rw-r--r--nptl/tst-mutex9.c131
10 files changed, 126 insertions, 409 deletions
diff --git a/ChangeLog b/ChangeLog
index a67ffe7c53..d09b70b287 100644
--- 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.
diff --git a/nptl/eintr.c b/nptl/eintr.c
index 4fc4d1c85c..6e644d2594 100644
--- a/nptl/eintr.c
+++ b/nptl/eintr.c
@@ -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);
 }
diff --git a/nptl/tst-eintr1.c b/nptl/tst-eintr1.c
index 8134f0affd..b60b796d61 100644
--- a/nptl/tst-eintr1.c
+++ b/nptl/tst-eintr1.c
@@ -22,11 +22,8 @@
 #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>
diff --git a/nptl/tst-eintr2.c b/nptl/tst-eintr2.c
index 12e2715fbd..c78c19ab33 100644
--- a/nptl/tst-eintr2.c
+++ b/nptl/tst-eintr2.c
@@ -23,11 +23,9 @@
 #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>
diff --git a/nptl/tst-eintr3.c b/nptl/tst-eintr3.c
index d6c4e2bf0d..0562eba4b4 100644
--- a/nptl/tst-eintr3.c
+++ b/nptl/tst-eintr3.c
@@ -22,11 +22,8 @@
 #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>
diff --git a/nptl/tst-eintr4.c b/nptl/tst-eintr4.c
index 2d9b299705..1f4b711a3c 100644
--- a/nptl/tst-eintr4.c
+++ b/nptl/tst-eintr4.c
@@ -22,11 +22,8 @@
 #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>
diff --git a/nptl/tst-eintr5.c b/nptl/tst-eintr5.c
index 6eee2a166f..da049c4eae 100644
--- a/nptl/tst-eintr5.c
+++ b/nptl/tst-eintr5.c
@@ -23,11 +23,10 @@
 #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>
diff --git a/nptl/tst-mutex-errorcheck.c b/nptl/tst-mutex-errorcheck.c
index 30ff7ea64a..afc27e7472 100644
--- a/nptl/tst-mutex-errorcheck.c
+++ b/nptl/tst-mutex-errorcheck.c
@@ -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>
diff --git a/nptl/tst-mutex5.c b/nptl/tst-mutex5.c
index 4e1d93b3e1..fb27152ecc 100644
--- a/nptl/tst-mutex5.c
+++ b/nptl/tst-mutex5.c
@@ -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>
diff --git a/nptl/tst-mutex9.c b/nptl/tst-mutex9.c
index 917276e380..e9fd8e2859 100644
--- a/nptl/tst-mutex9.c
+++ b/nptl/tst-mutex9.c
@@ -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>