]> git.ipfire.org Git - thirdparty/valgrind.git/commitdiff
Added regression test for POSIX spinlocks.
authorBart Van Assche <bvanassche@acm.org>
Mon, 7 Jul 2008 08:04:08 +0000 (08:04 +0000)
committerBart Van Assche <bvanassche@acm.org>
Mon, 7 Jul 2008 08:04:08 +0000 (08:04 +0000)
git-svn-id: svn://svn.valgrind.org/valgrind/trunk@8370

drd/tests/Makefile.am
drd/tests/pth_spinlock.c [new file with mode: 0644]
drd/tests/pth_spinlock.stderr.exp [new file with mode: 0644]
drd/tests/pth_spinlock.vgtest [new file with mode: 0644]

index cafb77cc167cc470e9d4b9ab89d86148eb6b8250..2a89af68c22a7a45828b4da53cb05053baf9c9ae 100644 (file)
@@ -90,6 +90,8 @@ EXTRA_DIST =                                        \
        pth_detached_sem.vgtest                     \
        pth_inconsistent_cond_wait.stderr.exp       \
        pth_inconsistent_cond_wait.vgtest           \
+       pth_spinlock.stderr.exp                     \
+       pth_spinlock.vgtest                         \
        recursive_mutex.stderr.exp                  \
        recursive_mutex.stdout.exp                  \
        recursive_mutex.vgtest                      \
@@ -196,6 +198,7 @@ check_PROGRAMS_COMMON = \
   pth_detached        \
   pth_detached_sem    \
   pth_inconsistent_cond_wait \
+  pth_spinlock        \
   recursive_mutex     \
   rwlock_race         \
   rwlock_test         \
@@ -300,6 +303,9 @@ pth_detached_sem_LDADD      = -lpthread
 pth_inconsistent_cond_wait_SOURCES = pth_inconsistent_cond_wait.c
 pth_inconsistent_cond_wait_LDADD   = -lpthread
 
+pth_spinlock_SOURCES        = pth_spinlock.c
+pth_spinlock_LDADD          = -lpthread
+
 recursive_mutex_SOURCES     = recursive_mutex.c
 recursive_mutex_LDADD       = -lpthread
 
diff --git a/drd/tests/pth_spinlock.c b/drd/tests/pth_spinlock.c
new file mode 100644 (file)
index 0000000..0f56a0b
--- /dev/null
@@ -0,0 +1,57 @@
+/** pthread_spinloc_t test. */
+
+
+/* Make sure pthread_spinlock_t is available when compiling with older glibc
+ * versions (2.3 or before).
+ */
+#define _GNU_SOURCE
+
+#include <pthread.h>
+#include <stdio.h>   /* fprintf() */
+#include <stdlib.h>  /* atoi() */
+
+
+static pthread_barrier_t  s_barrier;
+static pthread_spinlock_t s_spinlock;
+static int s_iterations;
+static int s_counter;
+
+
+static void* thread_func(void* arg)
+{
+  int i;
+
+  pthread_barrier_wait(&s_barrier);
+  for (i = s_iterations; i > 0; i--)
+  {
+    pthread_spin_lock(&s_spinlock);
+    s_counter++;
+    pthread_spin_unlock(&s_spinlock);
+  }
+  return 0;
+}
+
+int main(int argc, char** argv)
+{
+  int i;
+  const int n_threads = 10;
+  pthread_t tid[n_threads];
+
+  s_iterations = argc > 1 ? atoi(argv[1]) : 1000;
+
+  fprintf(stderr, "Start of test.\n");
+  pthread_barrier_init(&s_barrier, 0, n_threads);
+  pthread_spin_init(&s_spinlock, 0);
+  for (i = 0; i < n_threads; i++)
+    pthread_create(&tid[i], 0, thread_func, 0);
+  for (i = 0; i < n_threads; i++)
+    pthread_join(tid[i], 0);
+  pthread_spin_destroy(&s_spinlock);
+  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/pth_spinlock.stderr.exp b/drd/tests/pth_spinlock.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/pth_spinlock.vgtest b/drd/tests/pth_spinlock.vgtest
new file mode 100644 (file)
index 0000000..9b9333f
--- /dev/null
@@ -0,0 +1,3 @@
+prereq: ./supported_libpthread
+vgopts: --var-info=yes --check-stack-var=yes
+prog: pth_spinlock