fix shm subtest in cancellation points test
[libc-test.git] / src / functional / pthread_cancel-points.c
blobeb7dac75305da0162438f94154a320d315db0ceb
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 "test.h"
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;
18 static int seqno;
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)
39 struct timespec ts;
40 clock_gettime(CLOCK_REALTIME, &ts);
41 ts.tv_sec += 1;
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));
50 return 0;
53 static void prepare_thread(void *arg)
55 prepare_sem(arg);
56 TESTR(pthread_create(&td_test, 0, run_test, 0), "creating auxiliary thread");
59 static void cleanup_thread(void *arg)
61 void *res;
62 if (td_test) {
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");
67 cleanup_sem(arg);
70 static void execute_thread_join(void *arg)
72 TESTR(pthread_join(td_test, 0), "joining in the canceled thread");
73 td_test = 0;
76 static void prepare_dummy(void *arg)
80 static void execute_shm_open(void *arg)
82 int *fd = arg;
83 TESTE(*fd = shm_open("/testshm", O_RDWR|O_CREAT, 0666), "");
86 static void cleanup_shm(void *arg)
88 int *fd = arg;
89 if (*fd > 0) {
90 TESTE(close(*fd), "shm fd");
91 TESTE(shm_unlink("/testshm"), "");
95 static struct {
96 int want_cancel;
97 void (*prepare)(void *);
98 void (*execute)(void *);
99 void (*cleanup)(void *);
100 void *arg;
101 const char *descr;
102 } scenarios[] = {
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"},
110 { 0 }
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);
118 seqno = 1;
119 cur_sc->execute(cur_sc->arg);
120 seqno = 2;
121 return 0;
124 int main(void)
126 TESTR(sem_init(&sem_seq, 0, 0), "creating semaphore");
128 for (; cur_sc->prepare; cur_sc++) {
129 pthread_t td;
130 void *res;
132 cdescr = cur_sc->descr;
133 cur_sc->prepare(cur_sc->arg);
134 seqno = 0;
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");
142 } else {
143 TESTC(res != PTHREAD_CANCELED, "canceled thread exit status")
144 && TESTC(seqno == 2, "seqno");
146 cur_sc->cleanup(cur_sc->arg);
149 return t_status;