add test for strftime
[libc-test.git] / src / functional / pthread_cancel-points.c
blob8b169a0e247c79076bf3a3c31dea2d59bd575486
1 // testing cancellation points
2 #include <errno.h>
3 #include <pthread.h>
4 #include <semaphore.h>
5 #include <string.h>
6 #include <sys/mman.h>
7 #include <fcntl.h>
8 #include <unistd.h>
9 #include "test.h"
11 #define TESTC(c, m) ( (c) || (t_error(#c " failed (%s, " m ")\n", cdescr), 0) )
12 #define TESTR(f, m) do {int r; \
13 if ((r = (f))) t_error(#f " failed: %s (%s, " m ")\n", strerror(r), cdescr); } while (0)
14 #define TESTE(f, m) do { \
15 if ((f)==-1) t_error(#f " failed: %s (%s, " m ")\n", strerror(errno), cdescr); } while (0)
17 static sem_t sem_seq, sem_test;
19 static int seqno;
21 static const char *cdescr = "global initialization";
23 static void prepare_sem(void *arg)
25 TESTR(sem_init(&sem_test, 0, (long)arg), "creating semaphore");
28 static void cleanup_sem(void *arg)
30 TESTR(sem_destroy(&sem_test), "destroying semaphore");
33 static void execute_sem_wait(void *arg)
35 TESTR(sem_wait(&sem_test), "waiting on semaphore in the canceled thread");
38 static void execute_sem_timedwait(void *arg)
40 struct timespec ts;
41 clock_gettime(CLOCK_REALTIME, &ts);
42 ts.tv_sec += 1;
43 TESTR(sem_timedwait(&sem_test, &ts), "timed-waiting on semaphore in the canceled thread");
46 static pthread_t td_test;
48 static void *run_test(void *arg)
50 while (sem_wait(&sem_test));
51 return 0;
54 static void prepare_thread(void *arg)
56 prepare_sem(arg);
57 TESTR(pthread_create(&td_test, 0, run_test, 0), "creating auxiliary thread");
60 static void cleanup_thread(void *arg)
62 void *res;
63 if (td_test) {
64 TESTR(sem_post(&sem_test), "posting semaphore");
65 TESTR(pthread_join(td_test, &res), "joining auxiliary thread");
66 TESTC(res == 0, "auxiliary thread exit status");
68 cleanup_sem(arg);
71 static void execute_thread_join(void *arg)
73 TESTR(pthread_join(td_test, 0), "joining in the canceled thread");
74 td_test = 0;
77 static void prepare_dummy(void *arg)
81 static void execute_shm_open(void *arg)
83 int *fd = arg;
84 TESTE(*fd = shm_open("/testshm", O_RDWR|O_CREAT, 0666), "");
87 static void cleanup_shm(void *arg)
89 int *fd = arg;
90 if (*fd > 0) {
91 TESTE(close(*fd), "shm fd");
92 TESTE(shm_unlink("/testshm"), "");
96 static struct {
97 int want_cancel;
98 void (*prepare)(void *);
99 void (*execute)(void *);
100 void (*cleanup)(void *);
101 void *arg;
102 const char *descr;
103 } scenarios[] = {
104 {1, prepare_sem, execute_sem_wait, cleanup_sem, 0, "blocking sem_wait"},
105 {1, prepare_sem, execute_sem_wait, cleanup_sem, (void*)1, "non-blocking sem_wait"},
106 {1, prepare_sem, execute_sem_timedwait, cleanup_sem, 0, "blocking sem_timedwait"},
107 {1, prepare_sem, execute_sem_timedwait, cleanup_sem, (void*)1, "non-blocking sem_timedwait"},
108 {1, prepare_thread, execute_thread_join, cleanup_thread, 0, "blocking pthread_join"},
109 {1, prepare_thread, execute_thread_join, cleanup_thread, (void*)1, "non-blocking pthread_join"},
110 {0, prepare_dummy, execute_shm_open, cleanup_shm, &(int){0}, "shm_open"},
111 { 0 }
112 }, *cur_sc = scenarios;
114 static void *run_execute(void *arg)
116 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, 0);
117 while (sem_wait(&sem_seq));
118 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, 0);
119 seqno = 1;
120 cur_sc->execute(cur_sc->arg);
121 seqno = 2;
122 return 0;
125 int main(void)
127 TESTR(sem_init(&sem_seq, 0, 0), "creating semaphore");
129 for (; cur_sc->prepare; cur_sc++) {
130 pthread_t td;
131 void *res;
133 cdescr = cur_sc->descr;
134 cur_sc->prepare(cur_sc->arg);
135 seqno = 0;
136 TESTR(pthread_create(&td, 0, run_execute, 0), "creating thread to be canceled");
137 TESTR(pthread_cancel(td), "canceling");
138 TESTR(sem_post(&sem_seq), "unblocking canceled thread");
139 TESTR(pthread_join(td, &res), "joining canceled thread");
140 if (cur_sc->want_cancel) {
141 TESTC(res == PTHREAD_CANCELED, "canceled thread exit status")
142 && TESTC(seqno == 1, "seqno");
143 } else {
144 TESTC(res != PTHREAD_CANCELED, "canceled thread exit status")
145 && TESTC(seqno == 2, "seqno");
147 cur_sc->cleanup(cur_sc->arg);
150 return t_status;