1 // testing cancellation points
10 #define TESTC(c, m) ( (c) || (t_error(#c " failed (%s, " m ")\n", cdescr), 0) )
11 #define TESTR(f, m) do {int r; \
12 if ((r = (f))) t_error(#f " failed: %s (%s, " m ")\n", strerror(r), cdescr); } while (0)
13 #define TESTE(f, m) do { \
14 if ((f)==-1) t_error(#f " failed: %s (%s, " m ")\n", strerror(errno), cdescr); } while (0)
16 static sem_t sem_seq
, sem_test
;
20 static const char *cdescr
= "global initialization";
22 static void prepare_sem(void *arg
)
24 TESTR(sem_init(&sem_test
, 0, (long)arg
), "creating semaphore");
27 static void cleanup_sem(void *arg
)
29 TESTR(sem_destroy(&sem_test
), "destroying semaphore");
32 static void execute_sem_wait(void *arg
)
34 TESTR(sem_wait(&sem_test
), "waiting on semaphore in the canceled thread");
37 static void execute_sem_timedwait(void *arg
)
40 clock_gettime(CLOCK_REALTIME
, &ts
);
42 TESTR(sem_timedwait(&sem_test
, &ts
), "timed-waiting on semaphore in the canceled thread");
45 static pthread_t td_test
;
47 static void *run_test(void *arg
)
49 while (sem_wait(&sem_test
));
53 static void prepare_thread(void *arg
)
56 TESTR(pthread_create(&td_test
, 0, run_test
, 0), "creating auxiliary thread");
59 static void cleanup_thread(void *arg
)
63 TESTR(sem_post(&sem_test
), "posting semaphore");
64 TESTR(pthread_join(td_test
, &res
), "joining auxiliary thread");
65 TESTC(res
== 0, "auxiliary thread exit status");
70 static void execute_thread_join(void *arg
)
72 TESTR(pthread_join(td_test
, 0), "joining in the canceled thread");
76 static void prepare_dummy(void *arg
)
80 static void execute_shm_open(void *arg
)
83 TESTE(*fd
= shm_open("/testshm", O_RDWR
|O_CREAT
, 0666), "");
86 static void cleanup_shm(void *arg
)
90 TESTE(close(*fd
), "shm fd");
91 TESTE(shm_unlink("/testshm"), "");
97 void (*prepare
)(void *);
98 void (*execute
)(void *);
99 void (*cleanup
)(void *);
103 {1, prepare_sem
, execute_sem_wait
, cleanup_sem
, 0, "blocking sem_wait"},
104 {1, prepare_sem
, execute_sem_wait
, cleanup_sem
, (void*)1, "non-blocking sem_wait"},
105 {1, prepare_sem
, execute_sem_timedwait
, cleanup_sem
, 0, "blocking sem_timedwait"},
106 {1, prepare_sem
, execute_sem_timedwait
, cleanup_sem
, (void*)1, "non-blocking sem_timedwait"},
107 {1, prepare_thread
, execute_thread_join
, cleanup_thread
, 0, "blocking pthread_join"},
108 {1, prepare_thread
, execute_thread_join
, cleanup_thread
, (void*)1, "non-blocking pthread_join"},
109 {0, prepare_dummy
, execute_shm_open
, cleanup_shm
, &(int){0}, "shm_open"},
111 }, *cur_sc
= scenarios
;
113 static void *run_execute(void *arg
)
115 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE
, 0);
116 while (sem_wait(&sem_seq
));
117 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE
, 0);
119 cur_sc
->execute(cur_sc
->arg
);
126 TESTR(sem_init(&sem_seq
, 0, 0), "creating semaphore");
128 for (; cur_sc
->prepare
; cur_sc
++) {
132 cdescr
= cur_sc
->descr
;
133 cur_sc
->prepare(cur_sc
->arg
);
135 TESTR(pthread_create(&td
, 0, run_execute
, 0), "creating thread to be canceled");
136 TESTR(pthread_cancel(td
), "canceling");
137 TESTR(sem_post(&sem_seq
), "unblocking canceled thread");
138 TESTR(pthread_join(td
, &res
), "joining canceled thread");
139 if (cur_sc
->want_cancel
) {
140 TESTC(res
== PTHREAD_CANCELED
, "canceled thread exit status")
141 && TESTC(seqno
== 1, "seqno");
143 TESTC(res
!= PTHREAD_CANCELED
, "canceled thread exit status")
144 && TESTC(seqno
== 2, "seqno");
146 cur_sc
->cleanup(cur_sc
->arg
);