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