]> git.ipfire.org Git - thirdparty/valgrind.git/commitdiff
- Replaced pthread_cond_wait() by pthread_cond_timedwait() calls.
authorBart Van Assche <bvanassche@acm.org>
Tue, 21 Jul 2009 11:36:46 +0000 (11:36 +0000)
committerBart Van Assche <bvanassche@acm.org>
Tue, 21 Jul 2009 11:36:46 +0000 (11:36 +0000)
- An error message is now printed in case pthread_cond_timedwait()
  fails.
- Refactoring: joined thread1() and thread2() into one function.

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

drd/tests/pth_inconsistent_cond_wait.c
drd/tests/pth_inconsistent_cond_wait.stderr.exp1
drd/tests/pth_inconsistent_cond_wait.stderr.exp2

index 06c476b0ecd827366fee5fe8ff179ae502a01eb3..41a24545245ef076afff38df7023c983994f1448 100644 (file)
@@ -5,8 +5,13 @@
  */
 
 
+#include <errno.h>     // ETIMEDOUT
 #include <pthread.h>
 #include <semaphore.h>
+#include <stdio.h>
+#include <string.h>    // memset()
+#include <sys/time.h>  // gettimeofday()
+#include <time.h>      // struct timespec
 #include <unistd.h>
 
 pthread_cond_t  s_cond;
@@ -14,21 +19,25 @@ pthread_mutex_t s_mutex1;
 pthread_mutex_t s_mutex2;
 sem_t           s_sem;
 
-void* thread1(void* arg)
+static void* thread_func(void* mutex)
 {
-  pthread_mutex_lock(&s_mutex1);
-  sem_post(&s_sem);
-  pthread_cond_wait(&s_cond, &s_mutex1);
-  pthread_mutex_unlock(&s_mutex1);
-  return 0;
-}
+  int err;
+  struct timeval now;
+  struct timespec deadline;
 
-void* thread2(void* arg)
-{
-  pthread_mutex_lock(&s_mutex2);
+  pthread_mutex_lock(mutex);
   sem_post(&s_sem);
-  pthread_cond_wait(&s_cond, &s_mutex2);
-  pthread_mutex_unlock(&s_mutex2);
+  gettimeofday(&now, 0);
+  memset(&deadline, 0, sizeof(deadline));
+  deadline.tv_sec  = now.tv_sec + 2;
+  deadline.tv_nsec = now.tv_usec * 1000;
+  err = pthread_cond_timedwait(&s_cond, mutex, &deadline);
+  if (err != 0)
+    fprintf(stderr,
+           "pthread_cond_timedwait() call returned error code %d (%s)\n",
+           err,
+           strerror(err));
+  pthread_mutex_unlock(mutex);
   return 0;
 }
 
@@ -44,8 +53,8 @@ int main(int argc, char** argv)
   pthread_mutex_init(&s_mutex2, 0);
 
   /* Create two threads. */
-  pthread_create(&tid1, 0, &thread1, 0);
-  pthread_create(&tid2, 0, &thread2, 0);
+  pthread_create(&tid1, 0, &thread_func, &s_mutex1);
+  pthread_create(&tid2, 0, &thread_func, &s_mutex2);
 
   /* Wait until both threads have called sem_post(). */
   sem_wait(&s_sem);
index db4c68fa152bcd4ff2ba661666265b0a8c6e33e4..ab65a361b093c29ef21e801b356921625cb7581e 100644 (file)
@@ -1,8 +1,8 @@
 
 Thread 3:
 Inconsistent association of condition variable and mutex: condition variable 0x........, mutexes 0x........ and 0x........
-   at 0x........: pthread_cond_wait* (drd_pthread_intercepts.c:?)
-   by 0x........: thread2 (pth_inconsistent_cond_wait.c:?)
+   at 0x........: pthread_cond_timedwait* (drd_pthread_intercepts.c:?)
+   by 0x........: thread_func (pth_inconsistent_cond_wait.c:?)
    by 0x........: vgDrd_thread_wrapper (drd_pthread_intercepts.c:?)
    by 0x........: (within libpthread-?.?.so)
    by 0x........: clone (in /...libc...)
index f6962c7c03f0d238eb77186d67437384edaec309..7ec0471f9e950e6d17d79329386f41c741ca8260 100644 (file)
@@ -1,8 +1,8 @@
 
 Thread 2:
 Inconsistent association of condition variable and mutex: condition variable 0x........, mutexes 0x........ and 0x........
-   at 0x........: pthread_cond_wait* (drd_pthread_intercepts.c:?)
-   by 0x........: thread1 (pth_inconsistent_cond_wait.c:?)
+   at 0x........: pthread_cond_timedwait* (drd_pthread_intercepts.c:?)
+   by 0x........: thread_func (pth_inconsistent_cond_wait.c:?)
    by 0x........: vgDrd_thread_wrapper (drd_pthread_intercepts.c:?)
    by 0x........: (within libpthread-?.?.so)
    by 0x........: clone (in /...libc...)