summaryrefslogtreecommitdiffstats
path: root/regress/lib/libpthread/cancel2/cancel2.c
blob: 99801ceaee102114ae01528b8461304e0f9b8cf3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/* $OpenBSD: cancel2.c,v 1.3 2015/09/14 08:35:44 guenther Exp $ */
/* PUBLIC DOMAIN <marc@snafu.org> */

/*
 * Check that a thread waiting on a select or poll without timeout can be
 * cancelled.
 */

#include <sys/types.h>
#include <sys/time.h>

#include <poll.h>
#include <pthread.h>
#include <unistd.h>

#include "test.h"

static void *
select_thread(void *arg)
{
	int read_fd = *(int*) arg;
	fd_set read_fds;
	int result;

	FD_ZERO(&read_fds);
	FD_SET(read_fd, &read_fds);
	result = select(read_fd + 1, &read_fds, NULL, NULL, NULL);
	printf("select returned %d\n", result);
	return 0;
}


static void *
pselect_thread(void *arg)
{
	int read_fd = *(int*) arg;
	fd_set read_fds;
	int result;

	FD_ZERO(&read_fds);
	FD_SET(read_fd, &read_fds);
	result = pselect(read_fd + 1, &read_fds, NULL, NULL, NULL, NULL);
	printf("pselect returned %d\n", result);
	return 0;
}

static void *
poll_thread(void *arg)
{
	int read_fd = *(int*) arg;
	struct pollfd pfd;
	int result;

	pfd.fd = read_fd;
	pfd.events = POLLIN;

	result = poll(&pfd, 1, -1);
	printf("poll returned %d\n", result);
	return arg;
}


static void *
ppoll_thread(void *arg)
{
	int read_fd = *(int*) arg;
	struct pollfd pfd;
	int result;

	pfd.fd = read_fd;
	pfd.events = POLLIN;

	result = ppoll(&pfd, 1, NULL, NULL);
	printf("ppoll returned %d\n", result);
	return arg;
}

int
main(int argc, char *argv[])
{
	pthread_t thread;
	void *result = NULL;
	int pipe_fd[2];

	CHECKe(pipe(pipe_fd));

	printf("trying select\n");
	CHECKr(pthread_create(&thread, NULL, select_thread, pipe_fd));
	sleep(1);
	CHECKr(pthread_cancel(thread));
	CHECKr(pthread_join(thread, &result));
	ASSERT(result == PTHREAD_CANCELED);

	printf("trying pselect\n");
	CHECKr(pthread_create(&thread, NULL, pselect_thread, pipe_fd));
	sleep(1);
	CHECKr(pthread_cancel(thread));
	CHECKr(pthread_join(thread, &result));
	ASSERT(result == PTHREAD_CANCELED);

	printf("trying poll\n");
	CHECKr(pthread_create(&thread, NULL, poll_thread, pipe_fd));
	sleep(1);
	CHECKr(pthread_cancel(thread));
	CHECKr(pthread_join(thread, &result));
	ASSERT(result == PTHREAD_CANCELED);

	printf("trying ppoll\n");
	CHECKr(pthread_create(&thread, NULL, ppoll_thread, pipe_fd));
	sleep(1);
	CHECKr(pthread_cancel(thread));
	CHECKr(pthread_join(thread, &result));
	ASSERT(result == PTHREAD_CANCELED);

	SUCCEED;
}