• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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"
10 
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)
16 
17 static sem_t sem_seq, sem_test;
18 
19 static int seqno;
20 
21 static const char *cdescr = "global initialization";
22 
prepare_sem(void * arg)23 static void prepare_sem(void *arg)
24 {
25 	TESTR(sem_init(&sem_test, 0, (long)arg), "creating semaphore");
26 }
27 
cleanup_sem(void * arg)28 static void cleanup_sem(void *arg)
29 {
30 	TESTR(sem_destroy(&sem_test), "destroying semaphore");
31 }
32 
execute_sem_wait(void * arg)33 static void execute_sem_wait(void *arg)
34 {
35 	TESTR(sem_wait(&sem_test), "waiting on semaphore in the canceled thread");
36 }
37 
execute_sem_timedwait(void * arg)38 static void execute_sem_timedwait(void *arg)
39 {
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");
44 }
45 
46 static pthread_t td_test;
47 
run_test(void * arg)48 static void *run_test(void *arg)
49 {
50 	while (sem_wait(&sem_test));
51 	return 0;
52 }
53 
prepare_thread(void * arg)54 static void prepare_thread(void *arg)
55 {
56 	prepare_sem(arg);
57 	TESTR(pthread_create(&td_test, 0, run_test, 0), "creating auxiliary thread");
58 }
59 
cleanup_thread(void * arg)60 static void cleanup_thread(void *arg)
61 {
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");
67 	}
68 	cleanup_sem(arg);
69 }
70 
execute_thread_join(void * arg)71 static void execute_thread_join(void *arg)
72 {
73 	TESTR(pthread_join(td_test, 0), "joining in the canceled thread");
74 	td_test = 0;
75 }
76 
prepare_dummy(void * arg)77 static void prepare_dummy(void *arg)
78 {
79 }
80 
execute_shm_open(void * arg)81 static void execute_shm_open(void *arg)
82 {
83 	int *fd = arg;
84 	TESTE(*fd = shm_open("/testshm", O_RDWR|O_CREAT, 0666), "");
85 }
86 
cleanup_shm(void * arg)87 static void cleanup_shm(void *arg)
88 {
89 	int *fd = arg;
90 	if (*fd > 0) {
91 		TESTE(close(*fd), "shm fd");
92 		TESTE(shm_unlink("/testshm"), "");
93 	}
94 }
95 
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;
113 
run_execute(void * arg)114 static void *run_execute(void *arg)
115 {
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;
123 }
124 
main(void)125 int main(void)
126 {
127 	TESTR(sem_init(&sem_seq, 0, 0), "creating semaphore");
128 
129 	for (; cur_sc->prepare; cur_sc++) {
130 		pthread_t td;
131 		void *res;
132 
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");
146 		}
147 		cur_sc->cleanup(cur_sc->arg);
148 	}
149 
150 	return t_status;
151 }
152