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