From owner-svn-src-head@FreeBSD.ORG Sat Sep 25 04:26:40 2010 Return-Path: Delivered-To: svn-src-head@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 768C2106566B; Sat, 25 Sep 2010 04:26:40 +0000 (UTC) (envelope-from davidxu@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:4f8:fff6::2c]) by mx1.freebsd.org (Postfix) with ESMTP id 645DB8FC0C; Sat, 25 Sep 2010 04:26:40 +0000 (UTC) Received: from svn.freebsd.org (localhost [127.0.0.1]) by svn.freebsd.org (8.14.3/8.14.3) with ESMTP id o8P4QeBI049387; Sat, 25 Sep 2010 04:26:40 GMT (envelope-from davidxu@svn.freebsd.org) Received: (from davidxu@localhost) by svn.freebsd.org (8.14.3/8.14.3/Submit) id o8P4QeDZ049378; Sat, 25 Sep 2010 04:26:40 GMT (envelope-from davidxu@svn.freebsd.org) Message-Id: <201009250426.o8P4QeDZ049378@svn.freebsd.org> From: David Xu Date: Sat, 25 Sep 2010 04:26:40 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cc: Subject: svn commit: r213155 - head/tools/regression/pthread/unwind X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 25 Sep 2010 04:26:40 -0000 Author: davidxu Date: Sat Sep 25 04:26:40 2010 New Revision: 213155 URL: http://svn.freebsd.org/changeset/base/213155 Log: Add test cases for stack unwinding. Added: head/tools/regression/pthread/unwind/ head/tools/regression/pthread/unwind/Makefile (contents, props changed) head/tools/regression/pthread/unwind/Test.cpp (contents, props changed) head/tools/regression/pthread/unwind/catch_pthread_exit.cpp (contents, props changed) head/tools/regression/pthread/unwind/cond_wait_cancel.cpp (contents, props changed) head/tools/regression/pthread/unwind/cond_wait_cancel2.cpp (contents, props changed) head/tools/regression/pthread/unwind/main_thread_exit.cpp (contents, props changed) head/tools/regression/pthread/unwind/sem_wait_cancel.cpp (contents, props changed) head/tools/regression/pthread/unwind/thread_normal_exit.cpp (contents, props changed) Added: head/tools/regression/pthread/unwind/Makefile ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/tools/regression/pthread/unwind/Makefile Sat Sep 25 04:26:40 2010 (r213155) @@ -0,0 +1,17 @@ +# $FreeBSD$ +all: main_thread_exit thread_normal_exit sem_wait_cancel \ + cond_wait_cancel cond_wait_cancel2 catch_pthread_exit + +.cpp: + c++ -o $@ $< -lpthread + +main_thread_exit: main_thread_exit.cpp +thread_normal_exit: thread_normal_exit.cpp +sem_wait_cancel: sem_wait_cancel.cpp +cond_wait_cancel: cond_wait_cancel.cpp +cond_wait_cancel2: cond_wait_cancel2.cpp +catch_pthread_exit: catch_pthread_exit.cpp + +clean: .PHONY + rm -rf main_thread_exit thread_normal_exit sem_wait_cancel \ + cond_wait_cancel cond_wait_cancel2 catch_pthread_exit Added: head/tools/regression/pthread/unwind/Test.cpp ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/tools/regression/pthread/unwind/Test.cpp Sat Sep 25 04:26:40 2010 (r213155) @@ -0,0 +1,37 @@ +/* $FreeBSD$ */ + +int destructed; +int destructed2; + +class Test { +public: + Test() { printf("Test::Test()\n"); } + ~Test() { printf("Test::~Test()\n"); destructed = 1; } +}; + +void +cleanup_handler(void *arg) +{ + destructed2 = 1; + printf("%s()\n", __func__); +} + +void +check_destruct(void) +{ + if (!destructed) + printf("Bug, object destructor is not called\n"); + else + printf("OK\n"); +} + +void +check_destruct2(void) +{ + if (!destructed) + printf("Bug, object destructor is not called\n"); + else if (!destructed2) + printf("Bug, cleanup handler is not called\n"); + else + printf("OK\n"); +} Added: head/tools/regression/pthread/unwind/catch_pthread_exit.cpp ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/tools/regression/pthread/unwind/catch_pthread_exit.cpp Sat Sep 25 04:26:40 2010 (r213155) @@ -0,0 +1,35 @@ +/* $FreeBSD$ */ +/* try to catch thread exiting, and rethrow the exception */ + +#include +#include +#include + +int caught; + +void * +thr_routine(void *arg) +{ + try { + pthread_exit(NULL); + } catch (...) { + caught = 1; + printf("thread exiting exception caught\n"); + /* rethrow */ + throw; + } +} + +int +main() +{ + pthread_t td; + + pthread_create(&td, NULL, thr_routine, NULL); + pthread_join(td, NULL); + if (caught) + printf("OK\n"); + else + printf("failure\n"); + return (0); +} Added: head/tools/regression/pthread/unwind/cond_wait_cancel.cpp ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/tools/regression/pthread/unwind/cond_wait_cancel.cpp Sat Sep 25 04:26:40 2010 (r213155) @@ -0,0 +1,39 @@ +/* $FreeBSD$ */ +/* Test stack unwinding for pthread_cond_wait function */ + +#include +#include +#include +#include + +#include "Test.cpp" + +pthread_mutex_t mtx; +pthread_cond_t cv; + +void * +thr(void *arg) +{ + Test t; + + pthread_mutex_lock(&mtx); + pthread_cond_wait(&cv, &mtx); + pthread_mutex_unlock(&mtx); + printf("Bug, thread shouldn't be here.\n"); + return (0); +} + +int +main() +{ + pthread_t td; + + pthread_mutex_init(&mtx, NULL); + pthread_cond_init(&cv, NULL); + pthread_create(&td, NULL, thr, NULL); + sleep(1); + pthread_cancel(td); + pthread_join(td, NULL); + check_destruct(); + return (0); +} Added: head/tools/regression/pthread/unwind/cond_wait_cancel2.cpp ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/tools/regression/pthread/unwind/cond_wait_cancel2.cpp Sat Sep 25 04:26:40 2010 (r213155) @@ -0,0 +1,56 @@ +/* + * $FreeBSD$ + * + * Test stack unwinding for mixed pthread_cleanup_push/pop and C++ + * object, both should work together. + * + */ + +#include +#include +#include +#include + +#include "Test.cpp" + +pthread_mutex_t mtx; +pthread_cond_t cv; + +void f() +{ + Test t; + + pthread_mutex_lock(&mtx); + pthread_cond_wait(&cv, &mtx); + pthread_mutex_unlock(&mtx); + printf("Bug, thread shouldn't be here.\n"); +} + +void g() +{ + f(); +} + +void * +thr(void *arg) +{ + pthread_cleanup_push(cleanup_handler, NULL); + g(); + pthread_cleanup_pop(0); + return (0); +} + +int +main() +{ + pthread_t td; + + pthread_mutex_init(&mtx, NULL); + pthread_cond_init(&cv, NULL); + pthread_create(&td, NULL, thr, NULL); + sleep(1); + pthread_cancel(td); + pthread_join(td, NULL); + check_destruct2(); + return (0); +} Added: head/tools/regression/pthread/unwind/main_thread_exit.cpp ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/tools/regression/pthread/unwind/main_thread_exit.cpp Sat Sep 25 04:26:40 2010 (r213155) @@ -0,0 +1,18 @@ +/* $FreeBSD$ */ +/* check unwinding for main thread */ + +#include +#include +#include + +#include "Test.cpp" + +int +main() +{ + Test test; + + atexit(check_destruct); + pthread_exit((void *)1); + return (0); +} Added: head/tools/regression/pthread/unwind/sem_wait_cancel.cpp ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/tools/regression/pthread/unwind/sem_wait_cancel.cpp Sat Sep 25 04:26:40 2010 (r213155) @@ -0,0 +1,35 @@ +/* $FreeBSD$ */ +/* Test stack unwinding for libc's sem */ + +#include +#include +#include +#include + +#include "Test.cpp" + +sem_t sem; + +void * +thr(void *arg) +{ + Test t; + + sem_wait(&sem); + printf("Bug, thread shouldn't be here.\n"); + return (0); +} + +int +main() +{ + pthread_t td; + + sem_init(&sem, 0, 0); + pthread_create(&td, NULL, thr, NULL); + sleep(1); + pthread_cancel(td); + pthread_join(td, NULL); + check_destruct(); + return (0); +} Added: head/tools/regression/pthread/unwind/thread_normal_exit.cpp ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/tools/regression/pthread/unwind/thread_normal_exit.cpp Sat Sep 25 04:26:40 2010 (r213155) @@ -0,0 +1,28 @@ +/* $FreeBSD$ */ +/* test stack unwinding for a new thread */ + +#include +#include +#include + +#include "Test.cpp" + +void * +thr_routine(void *arg) +{ + Test test; + + pthread_exit(NULL); + printf("Bug, thread shouldn't be here\n"); +} + +int +main() +{ + pthread_t td; + + pthread_create(&td, NULL, thr_routine, NULL); + pthread_join(td, NULL); + check_destruct(); + return (0); +}