xref: /freebsd/tools/regression/pthread/unwind/cond_wait_cancel2.cpp (revision ef135466f81bc3f203efa77d1eb81132f7646196)
1e04b953eSDavid Xu /*
2e04b953eSDavid Xu  * $FreeBSD$
3e04b953eSDavid Xu  *
4e04b953eSDavid Xu  * Test stack unwinding for mixed pthread_cleanup_push/pop and C++
5e04b953eSDavid Xu  * object, both should work together.
6e04b953eSDavid Xu  *
7e04b953eSDavid Xu  */
8e04b953eSDavid Xu 
9e04b953eSDavid Xu #include <pthread.h>
10e04b953eSDavid Xu #include <stdio.h>
11e04b953eSDavid Xu #include <semaphore.h>
12e04b953eSDavid Xu #include <unistd.h>
13e04b953eSDavid Xu 
14e04b953eSDavid Xu #include "Test.cpp"
15e04b953eSDavid Xu 
16*ef135466SEd Maste static pthread_mutex_t mtx;
17*ef135466SEd Maste static pthread_cond_t cv;
18e04b953eSDavid Xu 
19*ef135466SEd Maste static void f()
20e04b953eSDavid Xu {
21e04b953eSDavid Xu 	Test t;
22e04b953eSDavid Xu 
23e04b953eSDavid Xu 	pthread_mutex_lock(&mtx);
24e04b953eSDavid Xu 	pthread_cond_wait(&cv, &mtx);
25e04b953eSDavid Xu 	pthread_mutex_unlock(&mtx);
26e04b953eSDavid Xu 	printf("Bug, thread shouldn't be here.\n");
27e04b953eSDavid Xu }
28e04b953eSDavid Xu 
29*ef135466SEd Maste static void g()
30e04b953eSDavid Xu {
31e04b953eSDavid Xu 	f();
32e04b953eSDavid Xu }
33e04b953eSDavid Xu 
34*ef135466SEd Maste static void *
35*ef135466SEd Maste thr(void *arg __unused)
36e04b953eSDavid Xu {
37e04b953eSDavid Xu 	pthread_cleanup_push(cleanup_handler, NULL);
38e04b953eSDavid Xu 	g();
39e04b953eSDavid Xu 	pthread_cleanup_pop(0);
40e04b953eSDavid Xu 	return (0);
41e04b953eSDavid Xu }
42e04b953eSDavid Xu 
43e04b953eSDavid Xu int
44e04b953eSDavid Xu main()
45e04b953eSDavid Xu {
46e04b953eSDavid Xu 	pthread_t td;
47e04b953eSDavid Xu 
48e04b953eSDavid Xu 	pthread_mutex_init(&mtx, NULL);
49e04b953eSDavid Xu 	pthread_cond_init(&cv, NULL);
50e04b953eSDavid Xu 	pthread_create(&td, NULL, thr, NULL);
51e04b953eSDavid Xu 	sleep(1);
52e04b953eSDavid Xu 	pthread_cancel(td);
53e04b953eSDavid Xu 	pthread_join(td, NULL);
54e04b953eSDavid Xu 	check_destruct2();
55e04b953eSDavid Xu 	return (0);
56e04b953eSDavid Xu }
57