add test for strftime
[libc-test.git] / src / functional / pthread_cancel.c
blob26db8f3eef8df0184f66a6a521d75c1252a9637f
1 #include <pthread.h>
2 #include <semaphore.h>
3 #include <string.h>
4 #include "test.h"
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);
13 sem_post(arg);
14 for (;;);
15 return 0;
18 static void cleanup1(void *arg)
20 *(int *)arg = 1;
23 static void cleanup2(void *arg)
25 *(int *)arg += 2;
28 static void cleanup3(void *arg)
30 *(int *)arg += 3;
33 static void cleanup4(void *arg)
35 *(int *)arg += 4;
38 static void *start_single(void *arg)
40 pthread_cleanup_push(cleanup1, arg);
41 sleep(3);
42 pthread_cleanup_pop(0);
43 return 0;
46 static void *start_nested(void *arg)
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;
61 int main(void)
63 pthread_t td;
64 sem_t sem1;
65 int r;
66 void *res;
67 int foo[4];
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 */
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");
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");
97 return t_status;