xref: /openbsd-src/regress/lib/libpthread/cancel2/cancel2.c (revision ab56e9609dc0b9e4813b71fd79b42475b6e5a4a4)
1 /* $OpenBSD: cancel2.c,v 1.3 2015/09/14 08:35:44 guenther Exp $ */
2 /* PUBLIC DOMAIN <marc@snafu.org> */
3 
4 /*
5  * Check that a thread waiting on a select or poll without timeout can be
6  * cancelled.
7  */
8 
9 #include <sys/types.h>
10 #include <sys/time.h>
11 
12 #include <poll.h>
13 #include <pthread.h>
14 #include <unistd.h>
15 
16 #include "test.h"
17 
18 static void *
select_thread(void * arg)19 select_thread(void *arg)
20 {
21 	int read_fd = *(int*) arg;
22 	fd_set read_fds;
23 	int result;
24 
25 	FD_ZERO(&read_fds);
26 	FD_SET(read_fd, &read_fds);
27 	result = select(read_fd + 1, &read_fds, NULL, NULL, NULL);
28 	printf("select returned %d\n", result);
29 	return 0;
30 }
31 
32 
33 static void *
pselect_thread(void * arg)34 pselect_thread(void *arg)
35 {
36 	int read_fd = *(int*) arg;
37 	fd_set read_fds;
38 	int result;
39 
40 	FD_ZERO(&read_fds);
41 	FD_SET(read_fd, &read_fds);
42 	result = pselect(read_fd + 1, &read_fds, NULL, NULL, NULL, NULL);
43 	printf("pselect returned %d\n", result);
44 	return 0;
45 }
46 
47 static void *
poll_thread(void * arg)48 poll_thread(void *arg)
49 {
50 	int read_fd = *(int*) arg;
51 	struct pollfd pfd;
52 	int result;
53 
54 	pfd.fd = read_fd;
55 	pfd.events = POLLIN;
56 
57 	result = poll(&pfd, 1, -1);
58 	printf("poll returned %d\n", result);
59 	return arg;
60 }
61 
62 
63 static void *
ppoll_thread(void * arg)64 ppoll_thread(void *arg)
65 {
66 	int read_fd = *(int*) arg;
67 	struct pollfd pfd;
68 	int result;
69 
70 	pfd.fd = read_fd;
71 	pfd.events = POLLIN;
72 
73 	result = ppoll(&pfd, 1, NULL, NULL);
74 	printf("ppoll returned %d\n", result);
75 	return arg;
76 }
77 
78 int
main(int argc,char * argv[])79 main(int argc, char *argv[])
80 {
81 	pthread_t thread;
82 	void *result = NULL;
83 	int pipe_fd[2];
84 
85 	CHECKe(pipe(pipe_fd));
86 
87 	printf("trying select\n");
88 	CHECKr(pthread_create(&thread, NULL, select_thread, pipe_fd));
89 	sleep(1);
90 	CHECKr(pthread_cancel(thread));
91 	CHECKr(pthread_join(thread, &result));
92 	ASSERT(result == PTHREAD_CANCELED);
93 
94 	printf("trying pselect\n");
95 	CHECKr(pthread_create(&thread, NULL, pselect_thread, pipe_fd));
96 	sleep(1);
97 	CHECKr(pthread_cancel(thread));
98 	CHECKr(pthread_join(thread, &result));
99 	ASSERT(result == PTHREAD_CANCELED);
100 
101 	printf("trying poll\n");
102 	CHECKr(pthread_create(&thread, NULL, poll_thread, pipe_fd));
103 	sleep(1);
104 	CHECKr(pthread_cancel(thread));
105 	CHECKr(pthread_join(thread, &result));
106 	ASSERT(result == PTHREAD_CANCELED);
107 
108 	printf("trying ppoll\n");
109 	CHECKr(pthread_create(&thread, NULL, ppoll_thread, pipe_fd));
110 	sleep(1);
111 	CHECKr(pthread_cancel(thread));
112 	CHECKr(pthread_join(thread, &result));
113 	ASSERT(result == PTHREAD_CANCELED);
114 
115 	SUCCEED;
116 }
117