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