1 #include <pthread.h>
2 #include <semaphore.h>
3 #include <string.h>
4 #include "test.h"
5
6 #define TESTC(c, m) ( (c) || (t_error("%s failed (" m ")\n", #c), 0) )
7 #define TESTR(r, f, m) ( \
8 ((r) = (f)) == 0 || (t_error("%s failed: %s (" m ")\n", #f, strerror(r)), 0) )
9
start_async(void * arg)10 static void *start_async(void *arg)
11 {
12 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, 0);
13 sem_post(arg);
14 for (;;);
15 return 0;
16 }
17
cleanup1(void * arg)18 static void cleanup1(void *arg)
19 {
20 *(int *)arg = 1;
21 }
22
cleanup2(void * arg)23 static void cleanup2(void *arg)
24 {
25 *(int *)arg += 2;
26 }
27
cleanup3(void * arg)28 static void cleanup3(void *arg)
29 {
30 *(int *)arg += 3;
31 }
32
cleanup4(void * arg)33 static void cleanup4(void *arg)
34 {
35 *(int *)arg += 4;
36 }
37
start_single(void * arg)38 static void *start_single(void *arg)
39 {
40 pthread_cleanup_push(cleanup1, arg);
41 sleep(3);
42 pthread_cleanup_pop(0);
43 return 0;
44 }
45
start_nested(void * arg)46 static void *start_nested(void *arg)
47 {
48 int *foo = arg;
49 pthread_cleanup_push(cleanup1, foo);
50 pthread_cleanup_push(cleanup2, foo+1);
51 pthread_cleanup_push(cleanup3, foo+2);
52 pthread_cleanup_push(cleanup4, foo+3);
53 sleep(3);
54 pthread_cleanup_pop(0);
55 pthread_cleanup_pop(0);
56 pthread_cleanup_pop(0);
57 pthread_cleanup_pop(0);
58 return 0;
59 }
60
main(void)61 int main(void)
62 {
63 pthread_t td;
64 sem_t sem1;
65 int r;
66 void *res;
67 int foo[4];
68
69 TESTR(r, sem_init(&sem1, 0, 0), "creating semaphore");
70
71 /* Asynchronous cancellation */
72 TESTR(r, pthread_create(&td, 0, start_async, &sem1), "failed to create thread");
73 while (sem_wait(&sem1));
74 TESTR(r, pthread_cancel(td), "canceling");
75 TESTR(r, pthread_join(td, &res), "joining canceled thread");
76 TESTC(res == PTHREAD_CANCELED, "canceled thread exit status");
77
78 /* Cancellation cleanup handlers */
79 foo[0] = 0;
80 TESTR(r, pthread_create(&td, 0, start_single, foo), "failed to create thread");
81 TESTR(r, pthread_cancel(td), "cancelling");
82 TESTR(r, pthread_join(td, &res), "joining canceled thread");
83 TESTC(res == PTHREAD_CANCELED, "canceled thread exit status");
84 TESTC(foo[0] == 1, "cleanup handler failed to run");
85
86 /* Nested cleanup handlers */
87 memset(foo, 0, sizeof foo);
88 TESTR(r, pthread_create(&td, 0, start_nested, foo), "failed to create thread");
89 TESTR(r, pthread_cancel(td), "cancelling");
90 TESTR(r, pthread_join(td, &res), "joining canceled thread");
91 TESTC(res == PTHREAD_CANCELED, "canceled thread exit status");
92 TESTC(foo[0] == 1, "cleanup handler failed to run");
93 TESTC(foo[1] == 2, "cleanup handler failed to run");
94 TESTC(foo[2] == 3, "cleanup handler failed to run");
95 TESTC(foo[3] == 4, "cleanup handler failed to run");
96
97 return t_status;
98 }
99