]> git.ipfire.org Git - thirdparty/valgrind.git/commitdiff
Added regression test for pthread cleanup handling.
authorBart Van Assche <bvanassche@acm.org>
Sat, 4 Jul 2009 12:20:04 +0000 (12:20 +0000)
committerBart Van Assche <bvanassche@acm.org>
Sat, 4 Jul 2009 12:20:04 +0000 (12:20 +0000)
git-svn-id: svn://svn.valgrind.org/valgrind/trunk@10408

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

index 5f9625e7e3bcc5f0d54113042034d27ce92b3291..9686b426ea8c6ae2a08c9951d665e4a3d230b154 100644 (file)
@@ -98,6 +98,8 @@ EXTRA_DIST =                                        \
        pth_broadcast.vgtest                        \
        pth_cancel_locked.stderr.exp                \
        pth_cancel_locked.vgtest                    \
+       pth_cleanup_handler.stderr.exp              \
+       pth_cleanup_handler.vgtest                  \
        pth_cond_race.stderr.exp                    \
        pth_cond_race.vgtest                        \
        pth_cond_race2.stderr.exp                   \
@@ -228,6 +230,7 @@ check_PROGRAMS =      \
   new_delete          \
   pth_broadcast       \
   pth_cancel_locked   \
+  pth_cleanup_handler \
   pth_cond_race       \
   pth_create_chain    \
   pth_detached        \
@@ -286,6 +289,8 @@ LDADD = -lpthread
 monitor_example_SOURCES     = monitor_example.cpp
 new_delete_SOURCES          = new_delete.cpp
 
+pth_cleanup_handler_CFLAGS  = @FLAG_W_NO_EMPTY_BODY@
+
 tsan_unittest_SOURCES       = tsan_unittest.cpp
 tsan_unittest_CXXFLAGS      = $(AM_CXXFLAGS) \
                        -DTHREAD_WRAPPERS='"tsan_thread_wrappers_pthread.h"'
diff --git a/drd/tests/pth_cleanup_handler.c b/drd/tests/pth_cleanup_handler.c
new file mode 100644 (file)
index 0000000..963414c
--- /dev/null
@@ -0,0 +1,66 @@
+/*
+ * Test program for verifying whether pthread cleanup handlers are invoked
+ * correctly.
+ */
+
+
+#include <stdbool.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <pthread.h>
+#include <stdlib.h>
+
+
+static pthread_rwlock_t rwl;
+
+
+static void cleanup_handler(void* param)
+{
+  fprintf(stderr, "Cleanup handler has been called.\n");
+  pthread_rwlock_unlock(&rwl);
+}
+
+static void* f(void *p)
+{
+  if (pthread_rwlock_rdlock(&rwl) != 0)
+  {
+    fprintf(stderr, "pthread_rwlock_rdlock()\n");
+    exit(1);
+  }
+
+  pthread_cleanup_push(cleanup_handler, NULL);
+  pthread_exit(0);
+  pthread_cleanup_pop(true);
+}
+
+
+int main()
+{
+  pthread_t pt1, pt2;
+
+  // Make sure the program exits in case a deadlock has been triggered.
+  alarm(2);
+
+  if (pthread_rwlock_init(&rwl, NULL) != 0)
+  {
+    fprintf(stderr, "pthread_rwlock_init()\n");
+    exit(1);
+  }
+  if (pthread_create(&pt1, NULL, f, NULL) != 0)
+  {
+    fprintf(stderr, "pthread_create()\n");
+    exit(1);
+  }
+  if (pthread_create(&pt2, NULL, f, NULL) != 0)
+  {
+    fprintf(stderr, "pthread_create()\n");
+    exit(1);
+  }
+
+  pthread_join(pt1, 0);
+  pthread_join(pt2, 0);
+
+  fprintf(stderr, "Test succeeded.\n");
+
+  return 0;
+}
diff --git a/drd/tests/pth_cleanup_handler.stderr.exp b/drd/tests/pth_cleanup_handler.stderr.exp
new file mode 100644 (file)
index 0000000..d218bb9
--- /dev/null
@@ -0,0 +1,6 @@
+
+Cleanup handler has been called.
+Cleanup handler has been called.
+Test succeeded.
+
+ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
diff --git a/drd/tests/pth_cleanup_handler.vgtest b/drd/tests/pth_cleanup_handler.vgtest
new file mode 100644 (file)
index 0000000..895161a
--- /dev/null
@@ -0,0 +1,3 @@
+prereq: test -e pth_cleanup_handler && ./supported_libpthread
+vgopts: --var-info=yes --check-stack-var=yes
+prog: pth_cleanup_handler