-
Notifications
You must be signed in to change notification settings - Fork 58
/
p-testthread.cc
213 lines (171 loc) · 4.55 KB
/
p-testthread.cc
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include "u-lib.hh"
#include <atomic>
extern uint8_t end[];
std::atomic_flag message_lock;
std::atomic<int> phase = 0;
int pfd[2] = {-1, -1};
const char* shared;
static void message(const char* x) {
while (message_lock.test_and_set()) {
pause();
}
console_printf("T%d (P%d): %s\n", sys_gettid(), sys_getpid(), x);
message_lock.clear();
}
static int thread1a(void* x) {
message("starting thread1a");
// wait for phase 1
while (phase != 1) {
sys_yield();
}
assert_memeq(shared, "Message to child\n", 17);
// enter phase 2
message("sending to main");
shared = "Message to parent\n";
phase = 2;
// wait for phase 3
while (phase != 3) {
sys_yield();
}
// read from pipe, write to pipe
char buf[100];
memset(buf, 0, sizeof(buf));
ssize_t n = sys_read(pfd[0], buf, sizeof(buf));
assert_eq(n, 2);
assert_memeq(buf, "Yo", 2);
phase = 4;
message("piping to main");
n = sys_write(pfd[1], "Hi", 2);
assert_eq(n, 2);
sys_texit(0);
}
static int thread1b(void*) {
// the argument to `sys_texit` is thrown away; we're just
// checking that nothing goes badly wrong
return 0;
}
static void test1() {
// create thread
message("clone");
char* stack1 = reinterpret_cast<char*>(
round_up(reinterpret_cast<uintptr_t>(end), PAGESIZE) + 16 * PAGESIZE
);
int r = sys_page_alloc(stack1);
assert_eq(r, 0);
pid_t t = sys_clone(thread1a, pfd, stack1 + PAGESIZE);
assert_gt(t, 0);
// enter phase 1
message("sending to secondary");
shared = "Message to child\n";
phase = 1;
// wait for phase 2
while (phase != 2) {
sys_yield();
}
assert_memeq(shared, "Message to parent\n", 18);
// enter phase 3
message("piping to secondary");
r = sys_pipe(pfd);
assert_eq(r, 0);
assert(pfd[0] > 2 && pfd[1] > 2);
assert(pfd[0] != pfd[1]);
phase = 3;
r = sys_write(pfd[1], "Yo", 2);
// enter phase 4
while (phase != 4) {
sys_yield();
}
char buf[100];
memset(buf, 0, sizeof(buf));
r = sys_read(pfd[0], buf, sizeof(buf));
assert_eq(r, 2);
assert_memeq(buf, "Hi", 2);
// wait for thread to exit
sys_msleep(10);
message("simple thread tests succeed!");
// start a new thread to check thread returning doesn't go wrong
message("checking automated texit");
t = sys_clone(thread1b, pfd, stack1 + PAGESIZE);
assert_gt(t, 0);
sys_msleep(10);
}
static int thread2a(void*) {
// this blocks forever
char buf[20];
ssize_t n = sys_read(pfd[0], buf, sizeof(buf));
assert_ne(n, 0);
sys_yield();
assert(false);
}
static void test2() {
// create thread
char* stack1 = reinterpret_cast<char*>(
round_up(reinterpret_cast<uintptr_t>(end), PAGESIZE) + 16 * PAGESIZE
);
int r = sys_page_alloc(stack1);
assert_eq(r, 0);
pid_t t = sys_clone(thread2a, pfd, stack1 + PAGESIZE);
assert_gt(t, 0);
// this should quit the `thread2a` thread too
sys_exit(161);
}
static int thread3a(void*) {
sys_msleep(10);
return 161;
}
static void test3() {
// create thread
char* stack1 = reinterpret_cast<char*>(
round_up(reinterpret_cast<uintptr_t>(end), PAGESIZE) + 16 * PAGESIZE
);
int r = sys_page_alloc(stack1);
assert_eq(r, 0);
pid_t t = sys_clone(thread3a, pfd, stack1 + PAGESIZE);
assert_gt(t, 0);
// this should quit the `thread2a` thread too
sys_texit(0);
}
void process_main() {
// test1
pid_t p = sys_fork();
assert_ge(p, 0);
if (p == 0) {
test1();
sys_exit(0);
}
pid_t ch = sys_waitpid(p);
assert_eq(ch, p);
// test2
message("checking that exit exits all threads");
int r = sys_pipe(pfd);
assert_eq(r, 0);
p = sys_fork();
assert_ge(p, 0);
if (p == 0) {
test2();
}
int status = 0;
ch = sys_waitpid(p, &status);
assert_eq(ch, p);
assert_eq(status, 161);
// check that `thread2a` really exited; if it did not, then
// the read end of the pipe will still be open (because `thread2a`
// has the write end open)
sys_close(pfd[1]);
char buf[20];
ssize_t n = sys_read(pfd[0], buf, sizeof(buf));
assert_eq(n, 0);
// test3
message("checking that implicit texit sets status");
p = sys_fork();
assert_ge(p, 0);
if (p == 0) {
test3();
}
status = 0;
ch = sys_waitpid(p, &status);
assert_eq(ch, p);
assert_eq(status, 161);
console_printf("testthread succeeded.\n");
sys_exit(0);
}