-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrwlock_test.cpp
More file actions
320 lines (259 loc) · 6.84 KB
/
Copy pathrwlock_test.cpp
File metadata and controls
320 lines (259 loc) · 6.84 KB
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#include "sys.h"
#include "mysys.h"
#include "mylock.h"
#include "tls.h"
#include "util.h"
#include "mysignal.h"
#include <signal.h>
#include <stdlib.h>
#include <assert.h>
#include <sys/mman.h>
#include <bits/ioctl.h>
#include <unistd.h>
#include <stdio.h>
#include <sched.h>
#define num_threads (16)
#define stack_size (512 * 1024)
#if defined(__x86_64__)
#define stack_grows_down
#elif defined(__aarch64__)
#define stack_grows_down
#else
#error Unsupported Architecture
#endif
static RwLock rwlocka = {};
static RwLock rwlockb = {};
static int stage[2];
static uint64_t stage_counter = 0;
static uint64_t counter = 0;
static int pipefd[2];
static pid_t tid[num_threads];
static int sem[2];
static int quit = 0;
extern "C" {
int __main_prepare_threaded();
int __external_thread_register_maybe();
}
__thread Tls _tls = {};
static Tls* get_tls() {
int reti = __external_thread_register_maybe();
if (reti < 0) {
abort();
}
__asm volatile("" ::: "memory");
Tls* tls = &_tls;
if (!tls->pid) {
tls->pid = getpid();
tls->tid = gettid();
}
return tls;
}
__attribute__((noinline)) static pid_t _thread_new(void (*fn)(),
void* stack,
void* fs_ptr) {
pid_t tid = my_syscall5(__NR_clone,
CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND |
CLONE_THREAD | CLONE_SYSVSEM | CLONE_SETTLS,
stack, 0, 0, fs_ptr);
if (tid) {
return tid;
}
fn();
sys_exit(0);
}
static pid_t thread_new(void (*fn)()) {
sys_mmap(nullptr, 4096, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
char* stack =
(char*)sys_mmap(nullptr, stack_size, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK, -1, 0);
sys_mmap(nullptr, 4096, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
char** fs_ptr =
(char**)sys_mmap(nullptr, 4096, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK, -1, 0);
*fs_ptr = stack;
#ifdef stack_grows_down
stack += stack_size;
// compilers call fn() with 8 byte stack alignement, but we need 16 byte
// alignement
stack -= 8;
#else
stack += 8;
#endif
pid_t tid = _thread_new(fn, stack, fs_ptr);
return tid;
}
static void handler(int sig, siginfo_t* info, void* ucontext);
static void install_sighandler() {
struct sigaction sig = {};
sig.sa_handler = (decltype(sig.sa_handler))handler;
sigemptyset(&sig.sa_mask);
sig.sa_flags = SA_NODEFER | SA_SIGINFO;
sigset_t unblock;
sigemptyset(&unblock);
sigaddset(&unblock, SIGSYS);
sys_rt_sigprocmask(SIG_UNBLOCK, &unblock, nullptr);
sigaction(SIGSYS, &sig, nullptr);
}
static void rwrite(int fd, uint64_t val) {
ssize_t ret;
ret = sys_write(fd, &val, sizeof(val));
if (ret != sizeof(val)) {
abort();
}
}
static void rsplice(int from, int to) {
ssize_t ret;
ret = sys_splice(from, nullptr, to, nullptr, sizeof(uint64_t), 0);
if (ret != sizeof(uint64_t)) {
abort();
}
}
static int rpending(int fd) {
int ret, pending;
ret = sys_ioctl(fd, FIONREAD, &pending);
if (ret < 0) {
abort();
}
return pending;
}
static void do_recover(Tls* tls) {
int pending = rpending(stage[0]);
int same = counter == stage_counter;
if (pending) {
assert(pending == sizeof(uint64_t));
if (!same) {
WRITE_ONCE(counter, stage_counter);
}
__asm volatile("" ::: "memory");
rsplice(stage[0], pipefd[1]);
} else {
uint64_t tmp;
if (same) {
tmp = counter + 1;
WRITE_ONCE(stage_counter, tmp);
} else {
tmp = stage_counter;
}
__asm volatile("" ::: "memory");
rwrite(stage[1], tmp);
__asm volatile("" ::: "memory");
WRITE_ONCE(counter, tmp);
__asm volatile("" ::: "memory");
rsplice(stage[0], pipefd[1]);
}
}
static void do_work(Tls* tls) {
int lock_write = tls->tid % 8 == 0;
if (lock_write) {
rwlock_lock_write(tls, &rwlocka);
} else {
rwlock_lock_read(tls, &rwlocka);
}
int ownerdead = rwlock_lock_write(tls, &rwlockb);
if (ownerdead) {
do_recover(tls);
} else {
uint64_t tmp = counter + 1;
WRITE_ONCE(stage_counter, tmp);
__asm volatile("" ::: "memory");
rwrite(stage[1], tmp);
__asm volatile("" ::: "memory");
WRITE_ONCE(counter, tmp);
__asm volatile("" ::: "memory");
rsplice(stage[0], pipefd[1]);
}
rwlock_unlock_write(tls, &rwlockb);
if (lock_write) {
rwlock_unlock_write(tls, &rwlocka);
} else {
rwlock_unlock_read(tls, &rwlocka);
}
}
static void handler(int sig, siginfo_t* info, void* ucontext) {
Tls* tls = get_tls();
assert(sig == SIGSYS);
mutex_recover(tls);
do_work(tls);
__builtin_longjmp((void**)tls->jumpbuf, 1);
}
__attribute__((noinline)) static void thread_loop() {
Tls* tls = get_tls();
while (1) {
do_work(tls);
}
}
static void thread() {
Tls* tls = get_tls();
if (!__builtin_setjmp((void**)tls->jumpbuf)) {
const char tmp = 'c';
int ret = sys_write(sem[1], &tmp, 1);
if (ret != 1) {
abort();
}
}
thread_loop();
}
static void signal_thread() {
int i, ret;
pid_t pid = sys_getpid();
while (1) {
msleep(10);
for (i = 0; i < num_threads; i++) {
ret = sys_tgkill(pid, tid[i], SIGSYS);
if (ret < 0) {
abort();
}
}
}
}
static void verifier_thread() {
uint64_t last = 0;
while (!__atomic_load_n(&quit, __ATOMIC_ACQUIRE)) {
uint64_t tmp;
int ret = sys_read(pipefd[0], &tmp, sizeof(tmp));
if (ret != sizeof(tmp)) {
abort();
}
assert(tmp == last + 1);
last = tmp;
}
printf("count: %lu\n", last);
sys_exit_group(0);
}
int main(int argc, char** argv) {
int ret;
ret = __main_prepare_threaded();
if (ret != 0) {
abort();
}
install_sighandler();
mutex_init();
ret = sys_pipe2(stage, O_CLOEXEC);
if (ret < 0) {
abort();
}
ret = sys_pipe2(pipefd, O_CLOEXEC);
if (ret < 0) {
abort();
}
ret = sys_pipe2(sem, O_CLOEXEC);
if (ret < 0) {
abort();
}
int i;
for (i = 0; i < num_threads; i++) {
tid[i] = thread_new(thread);
}
for (i = 0; i < num_threads; i++) {
char tmp;
ret = sys_read(sem[0], &tmp, 1);
if (ret != 1) {
abort();
}
}
thread_new(signal_thread);
thread_new(verifier_thread);
sleep(10);
WRITE_ONCE(quit, 1);
return 0;
}