]> git.ipfire.org Git - thirdparty/valgrind.git/commitdiff
Added Qt4 mutex test.
authorBart Van Assche <bvanassche@acm.org>
Mon, 7 Jul 2008 16:57:38 +0000 (16:57 +0000)
committerBart Van Assche <bvanassche@acm.org>
Mon, 7 Jul 2008 16:57:38 +0000 (16:57 +0000)
git-svn-id: svn://svn.valgrind.org/valgrind/trunk@8379

drd/tests/qt4_mutex.cpp [new file with mode: 0644]
drd/tests/qt4_mutex.stderr.exp [new file with mode: 0644]
drd/tests/qt4_mutex.vgtest [new file with mode: 0644]

diff --git a/drd/tests/qt4_mutex.cpp b/drd/tests/qt4_mutex.cpp
new file mode 100644 (file)
index 0000000..f90efa5
--- /dev/null
@@ -0,0 +1,82 @@
+/// Qt4 mutex test.
+
+#ifndef _GNU_SOURCE
+#define _GNU_SOURCE
+#endif
+
+#include <QtCore/QThread>  // class QMutex
+#include <QtCore/QMutex>
+#include <cstdio>       // fprintf()
+#include <cstdlib>      // atoi()
+#include <new>
+#include <pthread.h>    // pthread_barrier_t
+#include <vector>
+
+
+static pthread_barrier_t s_barrier;
+static QMutex* s_pMutex;
+static int s_iterations;
+static int s_counter;
+
+
+class IncThread: public QThread
+{
+  virtual void run();
+};
+
+void IncThread::run()
+{
+  int i;
+
+  pthread_barrier_wait(&s_barrier);
+  for (i = s_iterations; i > 0; i--)
+  {
+    s_pMutex->lock();
+    s_counter++;
+    s_pMutex->unlock();
+  }
+}
+
+int main(int argc, char** argv)
+{
+  int i;
+  const int n_threads = 10;
+  std::vector<QThread*> tid(n_threads);
+
+  s_iterations = argc > 1 ? atoi(argv[1]) : 1000;
+
+  fprintf(stderr, "Start of test.\n");
+
+  {
+    // Stack-allocated mutex.
+    QMutex M(QMutex::Recursive);
+    M.lock();
+    M.tryLock();
+    M.unlock();
+    M.unlock();
+  }
+
+  pthread_barrier_init(&s_barrier, 0, n_threads);
+  s_pMutex = new QMutex();
+  for (i = 0; i < n_threads; i++)
+  {
+    tid[i] = new IncThread;
+    tid[i]->start();
+  }
+  for (i = 0; i < n_threads; i++)
+  {
+    tid[i]->wait();
+    delete tid[i];
+  }
+  delete s_pMutex;
+  s_pMutex = 0;
+  pthread_barrier_destroy(&s_barrier);
+
+  if (s_counter == n_threads * s_iterations)
+    fprintf(stderr, "Test successful.\n");
+  else
+    fprintf(stderr, "Test failed: counter = %d, should be %d\n",
+            s_counter, n_threads * s_iterations);
+
+  return 0;
+}
diff --git a/drd/tests/qt4_mutex.stderr.exp b/drd/tests/qt4_mutex.stderr.exp
new file mode 100644 (file)
index 0000000..21dba22
--- /dev/null
@@ -0,0 +1,5 @@
+
+Start of test.
+Test successful.
+
+ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
diff --git a/drd/tests/qt4_mutex.vgtest b/drd/tests/qt4_mutex.vgtest
new file mode 100644 (file)
index 0000000..9dc7a49
--- /dev/null
@@ -0,0 +1,2 @@
+vgopts: --check-stack-var=yes
+prog: qt4_mutex