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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
/* $OpenBSD: cancel.c,v 1.2 2001/08/23 04:26:05 fgsch Exp $ */
/* David Leonard <d@openbsd.org>, 1999. Public Domain. */
#include <pthread.h>
#include <pthread_np.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include "test.h"
static pthread_cond_t cond;
static pthread_mutex_t mutex;
static struct timespec expiretime;
static int pv_state = 0;
void p() {
CHECKr(pthread_mutex_lock(&mutex));
if (pv_state <= 0) {
CHECKr(pthread_cond_timedwait(&cond, &mutex, &expiretime));
}
pv_state--;
CHECKr(pthread_mutex_unlock(&mutex));
}
void v() {
int needsignal;
CHECKr(pthread_mutex_lock(&mutex));
pv_state++;
needsignal = (pv_state == 1);
if (needsignal)
CHECKr(pthread_cond_signal(&cond));
CHECKr(pthread_mutex_unlock(&mutex));
}
void
c1handler(void *arg)
{
CHECKe(close(*(int *)arg));
v();
}
void *
child1fn(arg)
void *arg;
{
int fd;
char buf[1024];
int len;
SET_NAME("c1");
CHECKr(pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL));
/* something that will block */
CHECKe(fd = open("/dev/tty", O_RDONLY));
pthread_cleanup_push(c1handler, (void *)&fd);
v();
while (1) {
CHECKe(len = read(fd, &buf, sizeof buf));
printf("child 1 read %d bytes\n", len);
}
PANIC("child 1");
}
static int c2_in_test = 0;
void
c2handler(void *arg)
{
ASSERT(c2_in_test);
v();
}
static int message_seen = 0;
void *
child2fn(arg)
void *arg;
{
SET_NAME("c2");
CHECKr(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL));
pthread_cleanup_push(c2handler, NULL);
v();
while (1) {
struct timespec now;
struct timespec end;
/*
* XXX Be careful not to call any cancellation points
* until pthread_testcancel()
*/
CHECKe(clock_gettime(CLOCK_REALTIME, &end));
end.tv_sec ++;
while (1) {
CHECKe(clock_gettime(CLOCK_REALTIME, &now));
if (timespeccmp(&now, &end, >=))
break;
pthread_yield();
}
/* XXX write() contains a cancellation point */
/* printf("child 2 testing for cancel\n"); */
c2_in_test = 1;
pthread_testcancel();
printf("you should see this message exactly once\n");
message_seen++;
c2_in_test = 0;
ASSERT(message_seen == 1);
}
PANIC("child 2");
}
static int c3_cancel_survived;
void
c3handler(void *arg)
{
printf("(fyi, cancellation of self %s instantaneous)\n",
(c3_cancel_survived ? "was not" : "was"));
v();
}
void *
child3fn(arg)
void *arg;
{
SET_NAME("c3");
pthread_cleanup_push(c3handler, NULL);
/* Cancel myself */
CHECKr(pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL));
c3_cancel_survived = 0;
pthread_cancel(pthread_self());
c3_cancel_survived = 1;
pthread_testcancel();
PANIC("child 3");
}
int
main()
{
pthread_t child1, child2, child3;
/* Set up our control flow */
CHECKr(pthread_mutex_init(&mutex, NULL));
CHECKr(pthread_cond_init(&cond, NULL));
CHECKe(clock_gettime(CLOCK_REALTIME, &expiretime));
expiretime.tv_sec += 5; /* this test shouldn't run over 5 seconds */
CHECKr(pthread_create(&child1, NULL, child1fn, NULL));
CHECKr(pthread_create(&child2, NULL, child2fn, NULL));
p();
p();
CHECKr(pthread_cancel(child1));
p();
/* Give thread 2 a chance to go through its deferred loop once */
sleep(2);
CHECKr(pthread_cancel(child2));
p();
/* Child 3 cancels itself */
CHECKr(pthread_create(&child3, NULL, child3fn, NULL));
p();
/* Make sure they're all gone */
CHECKr(pthread_join(child3, NULL));
CHECKr(pthread_join(child2, NULL));
CHECKr(pthread_join(child1, NULL));
exit(0);
}
|