1 // testing cancellation points
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
;
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
)
41 clock_gettime(CLOCK_REALTIME
, &ts
);
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
));
54 static void prepare_thread(void *arg
)
57 TESTR(pthread_create(&td_test
, 0, run_test
, 0), "creating auxiliary thread");
60 static void cleanup_thread(void *arg
)
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");
71 static void execute_thread_join(void *arg
)
73 TESTR(pthread_join(td_test
, 0), "joining in the canceled thread");
77 static void prepare_dummy(void *arg
)
81 static void execute_shm_open(void *arg
)
84 TESTE(*fd
= shm_open("/testshm", O_RDWR
|O_CREAT
, 0666), "");
87 static void cleanup_shm(void *arg
)
91 TESTE(close(*fd
), "shm fd");
92 TESTE(shm_unlink("/testshm"), "");
98 void (*prepare
)(void *);
99 void (*execute
)(void *);
100 void (*cleanup
)(void *);
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"},
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);
120 cur_sc
->execute(cur_sc
->arg
);
127 TESTR(sem_init(&sem_seq
, 0, 0), "creating semaphore");
129 for (; cur_sc
->prepare
; cur_sc
++) {
133 cdescr
= cur_sc
->descr
;
134 cur_sc
->prepare(cur_sc
->arg
);
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");
144 TESTC(res
!= PTHREAD_CANCELED
, "canceled thread exit status")
145 && TESTC(seqno
== 2, "seqno");
147 cur_sc
->cleanup(cur_sc
->arg
);