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) )
10 static void *start_async(void *arg
)
12 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS
, 0);
18 static void cleanup1(void *arg
)
23 static void cleanup2(void *arg
)
28 static void cleanup3(void *arg
)
33 static void cleanup4(void *arg
)
38 static void *start_single(void *arg
)
40 pthread_cleanup_push(cleanup1
, arg
);
42 pthread_cleanup_pop(0);
46 static void *start_nested(void *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);
54 pthread_cleanup_pop(0);
55 pthread_cleanup_pop(0);
56 pthread_cleanup_pop(0);
57 pthread_cleanup_pop(0);
69 TESTR(r
, sem_init(&sem1
, 0, 0), "creating semaphore");
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");
78 /* Cancellation cleanup handlers */
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");
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");