]> git.ipfire.org Git - thirdparty/valgrind.git/commitdiff
drd: Make drd_pthread_intercepts.c compile again on systems where
authorBart Van Assche <bvanassche@acm.org>
Thu, 28 Jul 2011 15:04:08 +0000 (15:04 +0000)
committerBart Van Assche <bvanassche@acm.org>
Thu, 28 Jul 2011 15:04:08 +0000 (15:04 +0000)
FUTEX_PRIVATE_FLAG has not been defined in <linux/futex.h>, e.g. older System z
systems.

git-svn-id: svn://svn.valgrind.org/valgrind/trunk@11929

drd/drd_pthread_intercepts.c

index ebc2cc85596675557f4a6d28e2bbdfdd19133d73..b3fa771dfc56e74ae02ed85c90cad80948dd7cf9 100644 (file)
@@ -49,6 +49,7 @@
 #endif
 
 #include <assert.h>         /* assert() */
+#include <errno.h>
 #include <pthread.h>        /* pthread_mutex_t */
 #include <semaphore.h>      /* sem_t */
 #include <stdint.h>         /* uintptr_t */
@@ -58,6 +59,9 @@
 #ifdef __linux__
 #include <asm/unistd.h>     /* __NR_futex */
 #include <linux/futex.h>    /* FUTEX_WAIT */
+#ifndef FUTEX_PRIVATE_FLAG
+#define FUTEX_PRIVATE_FLAG 0
+#endif
 #endif
 #include "config.h"         /* HAVE_PTHREAD_MUTEX_ADAPTIVE_NP etc. */
 #include "drd_basics.h"     /* DRD_() */
@@ -187,13 +191,26 @@ static void DRD_(sema_destroy)(DrdSema* sema)
 
 static void DRD_(sema_down)(DrdSema* sema)
 {
+   int res = ENOSYS;
+
    while (sema->counter == 0) {
-#ifdef __linux__
-      syscall(__NR_futex, (UWord)&sema->counter,
-              FUTEX_WAIT | FUTEX_PRIVATE_FLAG, 0);
-#else
-      sched_yield();
+#if defined(__linux__) && defined(__NR_futex)
+      if (syscall(__NR_futex, (UWord)&sema->counter,
+                  FUTEX_WAIT | FUTEX_PRIVATE_FLAG, 0) == 0) {
+         res = 0;
+      } else {
+         res = errno;
+         assert(res == EWOULDBLOCK || res == ENOSYS);
+      }
 #endif
+      /*
+       * Invoke sched_yield() on non-Linux systems, if the futex syscall has
+       * not been invoked or if this code has been built on a Linux system
+       * where __NR_futex is defined and is run on a Linux system that does
+       * not support the futex syscall.
+       */
+      if (res == ENOSYS)
+         sched_yield();
    }
    sema->counter--;
 }
@@ -201,7 +218,7 @@ static void DRD_(sema_down)(DrdSema* sema)
 static void DRD_(sema_up)(DrdSema* sema)
 {
    sema->counter++;
-#ifdef __linux__
+#if defined(__linux__) && defined(__NR_futex)
    syscall(__NR_futex, (UWord)&sema->counter,
            FUTEX_WAKE | FUTEX_PRIVATE_FLAG, 1);
 #endif