From 5c2594cec190001eacbb1b9b087e375ce6fa231a Mon Sep 17 00:00:00 2001 From: Bart Van Assche Date: Sat, 4 Jul 2009 12:20:04 +0000 Subject: [PATCH] Added regression test for pthread cleanup handling. git-svn-id: svn://svn.valgrind.org/valgrind/trunk@10408 --- drd/tests/Makefile.am | 5 ++ drd/tests/pth_cleanup_handler.c | 66 ++++++++++++++++++++++++ drd/tests/pth_cleanup_handler.stderr.exp | 6 +++ drd/tests/pth_cleanup_handler.vgtest | 3 ++ 4 files changed, 80 insertions(+) create mode 100644 drd/tests/pth_cleanup_handler.c create mode 100644 drd/tests/pth_cleanup_handler.stderr.exp create mode 100644 drd/tests/pth_cleanup_handler.vgtest diff --git a/drd/tests/Makefile.am b/drd/tests/Makefile.am index 5f9625e7e3..9686b426ea 100644 --- a/drd/tests/Makefile.am +++ b/drd/tests/Makefile.am @@ -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 index 0000000000..963414c853 --- /dev/null +++ b/drd/tests/pth_cleanup_handler.c @@ -0,0 +1,66 @@ +/* + * Test program for verifying whether pthread cleanup handlers are invoked + * correctly. + */ + + +#include +#include +#include +#include +#include + + +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 index 0000000000..d218bb9def --- /dev/null +++ b/drd/tests/pth_cleanup_handler.stderr.exp @@ -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 index 0000000000..895161a917 --- /dev/null +++ b/drd/tests/pth_cleanup_handler.vgtest @@ -0,0 +1,3 @@ +prereq: test -e pth_cleanup_handler && ./supported_libpthread +vgopts: --var-info=yes --check-stack-var=yes +prog: pth_cleanup_handler -- 2.47.3