-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmylock.cpp
More file actions
548 lines (446 loc) · 14.2 KB
/
Copy pathmylock.cpp
File metadata and controls
548 lines (446 loc) · 14.2 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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
#include "mylock.h"
#include "util.h"
#include "mysys.h"
#include "linux/futex.h"
#include <stdlib.h>
#include <sys/mman.h>
typedef struct LocalMutexes LocalMutexes;
struct LocalMutexes {
Spinlock size;
uint32_t alloc;
RobustMutex data[];
};
static LocalMutexes* local_mutexes = nullptr;
void spinlock_lock(Spinlock* lock) {
Spinlock expected = 0;
while (!__atomic_compare_exchange_n(lock, &expected, 1, 0, __ATOMIC_ACQUIRE,
__ATOMIC_RELAXED)) {
expected = 0;
}
}
int spinlock_trylock(Spinlock* lock) {
Spinlock expected = 0;
if (!__atomic_compare_exchange_n(lock, &expected, 1, 0, __ATOMIC_ACQUIRE,
__ATOMIC_RELAXED)) {
return -1;
}
return 0;
}
void spinlock_unlock(Spinlock* lock) {
__atomic_store_n(lock, 0, __ATOMIC_RELEASE);
}
int futex_wait(Mutex* mutex, Mutex expected) {
signed long ret;
struct timespec timeout = {1, 0};
ret = sys_futex(mutex, FUTEX_WAIT, expected, &timeout, nullptr, 0);
if (ret < 0) {
if (ret == -EAGAIN) {
return 0;
} else if (ret == -ETIMEDOUT) {
return -ETIMEDOUT;
} else {
abort();
}
}
return 0;
}
void futex_wake(Mutex* mutex, int waiters) {
signed long ret =
sys_futex(mutex, FUTEX_WAKE, waiters, nullptr, nullptr, 0);
if (ret < 0) {
abort();
}
}
static int _mutex_lock(const pid_t tid, Mutex* mutex) {
Mutex expected = 0;
int tries = 0;
int ownerdead = 0;
while (!__atomic_compare_exchange_n(mutex, &expected, tid, 0,
__ATOMIC_ACQUIRE, __ATOMIC_RELAXED)) {
if (tries++ > 1000) {
tries = 0;
if (is_tid_dead(expected)) {
ownerdead = 1;
continue;
}
futex_wait(mutex, expected);
}
expected = 0;
ownerdead = 0;
}
return ownerdead;
}
static void __mutex_unlock(Mutex* mutex, pid_t val) {
__atomic_store_n(mutex, val, __ATOMIC_RELEASE);
futex_wake(mutex, 1);
}
static void _mutex_unlock(Mutex* mutex) {
__mutex_unlock(mutex, 0);
}
static int _mutex_wait(Mutex* mutex) {
Mutex expected = __atomic_load_n(mutex, __ATOMIC_ACQUIRE);
return futex_wait(mutex, expected);
}
int mutex_lock(Tls* tls, RobustMutex* mutex) {
RobustMutexList* list = &tls->my_robust_mutex_list;
assert(!list->pending);
WRITE_ONCE(list->pending, mutex);
__asm volatile("" ::: "memory");
int ownerdead = _mutex_lock(tls->tid, &mutex->mutex);
__asm volatile("" ::: "memory");
RLIST_INSERT_HEAD(&list->head, mutex, next);
__asm volatile("" ::: "memory");
WRITE_ONCE(list->pending, nullptr);
__asm volatile("" ::: "memory");
return ownerdead;
}
void mutex_unlock(Tls* tls, RobustMutex* mutex) {
RobustMutexList* list = &tls->my_robust_mutex_list;
assert(!list->pending);
WRITE_ONCE(list->pending, mutex);
__asm volatile("" ::: "memory");
RLIST_REMOVE(&list->head, mutex, next);
__asm volatile("" ::: "memory");
_mutex_unlock(&mutex->mutex);
__asm volatile("" ::: "memory");
WRITE_ONCE(list->pending, nullptr);
__asm volatile("" ::: "memory");
}
int mutex_locked(Tls* tls, RobustMutex* mutex) {
uint32_t val = __atomic_load_n(&mutex->mutex, __ATOMIC_RELAXED);
return val == (uint32_t)tls->tid;
}
static void mutex_recover_pending(Tls* tls) {
RobustMutexList* list = &tls->my_robust_mutex_list;
RobustMutex* mutex = list->pending;
if (!mutex) {
return;
}
pid_t mutex_tid = __atomic_load_n(&mutex->mutex, __ATOMIC_RELAXED);
int found = 0;
RobustMutex *elm, *temp;
RLIST_FOREACH(elm, &list->head, next, temp) {
if (elm == mutex) {
found = 1;
break;
}
}
__asm volatile("" ::: "memory");
if (found) {
RLIST_REMOVE(&list->head, mutex, next);
}
__asm volatile("" ::: "memory");
if (tls->tid == mutex_tid) {
__mutex_unlock(&mutex->mutex, FUTEX_TID_MASK);
}
__asm volatile("" ::: "memory");
WRITE_ONCE(list->pending, nullptr);
__asm volatile("" ::: "memory");
}
static void mutex_recover_one(Tls* tls, RobustMutex* mutex) {
RobustMutexList* list = &tls->my_robust_mutex_list;
pid_t mutex_tid = __atomic_load_n(&mutex->mutex, __ATOMIC_RELAXED);
assert(!list->pending);
assert(tls->tid == mutex_tid);
WRITE_ONCE(list->pending, mutex);
__asm volatile("" ::: "memory");
RLIST_REMOVE(&list->head, mutex, next);
__asm volatile("" ::: "memory");
__mutex_unlock(&mutex->mutex, FUTEX_TID_MASK);
__asm volatile("" ::: "memory");
WRITE_ONCE(list->pending, nullptr);
__asm volatile("" ::: "memory");
}
static void rwlock_recover_pending(Tls* tls);
static void rwlock_recover_one(Tls* tls, RwLock* lock);
void mutex_recover(Tls* tls) {
RobustMutexList* list = &tls->my_robust_mutex_list;
RwLockList* rwlock_list = &tls->my_rwlock_list;
mutex_recover_pending(tls);
RobustMutex *elm, *temp;
RLIST_FOREACH(elm, &list->head, next, temp) {
mutex_recover_one(tls, elm);
}
rwlock_recover_pending(tls);
RwLockHolder *relm, *rtemp;
RLIST_FOREACH(relm, &rwlock_list->head, next, rtemp) {
rwlock_recover_one(tls, relm->lock);
}
}
void mutex_init() {
if (local_mutexes) {
abort();
}
// map shared, so we don't deadlock after fork()
void* alloc = sys_mmap(nullptr, 4096, PROT_READ | PROT_WRITE,
MAP_ANONYMOUS | MAP_SHARED, -1, 0);
if ((unsigned long)alloc >= -4095UL) {
abort();
}
local_mutexes = (LocalMutexes*)alloc;
local_mutexes->alloc = (4096 - sizeof(LocalMutexes)) / sizeof(RobustMutex);
assert(sizeof(LocalMutexes) + local_mutexes->alloc * sizeof(RobustMutex) <=
4096);
}
RobustMutex* mutex_alloc() {
uint32_t idx =
__atomic_fetch_add(&local_mutexes->size, 1, __ATOMIC_ACQUIRE);
if (idx >= local_mutexes->alloc) {
return nullptr;
}
return local_mutexes->data + idx;
}
static int rwlock_cleanup_dead(RwLock* lock) {
int dead = 0;
if (lock->writer && is_tid_dead(lock->writer)) {
dead = 1;
__asm volatile("" ::: "memory");
WRITE_ONCE(lock->writer, FUTEX_TID_MASK);
__asm volatile("" ::: "memory");
}
if (lock->writer_waiter && is_tid_dead(lock->writer_waiter)) {
__asm volatile("" ::: "memory");
WRITE_ONCE(lock->writer_waiter, 0);
__asm volatile("" ::: "memory");
}
for (int i = 0; i < holders_alloc; i++) {
uint32_t* reader = lock->reader + i;
if (*reader && is_tid_dead(*reader)) {
dead = 1;
lock->num_readers--;
__asm volatile("" ::: "memory");
WRITE_ONCE(*reader, 0);
__asm volatile("" ::: "memory");
}
}
return dead;
}
static void _rwlock_recover(RwLock* lock) {
uint32_t num_readers = 0;
for (int i = 0; i < holders_alloc; i++) {
if (lock->reader[i]) {
num_readers++;
}
}
__asm volatile("" ::: "memory");
WRITE_ONCE(lock->num_readers, num_readers);
__asm volatile("" ::: "memory");
}
static void __rwlock_lock(Tls* tls,
RwLock* lock,
uint32_t* thelock,
RwLockHolder* entry) {
RwLockList* list = &tls->my_rwlock_list;
assert(thelock);
assert(!list->pending);
__asm volatile("" ::: "memory");
WRITE_ONCE(list->pending, lock);
__asm volatile("" ::: "memory");
WRITE_ONCE(*thelock, tls->tid);
__asm volatile("" ::: "memory");
RLIST_INSERT_HEAD(&list->head, entry, next);
__asm volatile("" ::: "memory");
WRITE_ONCE(list->pending, nullptr);
}
static void __rwlock_unlock(Tls* tls,
RwLock* lock,
uint32_t* thelock,
RwLockHolder* entry) {
RwLockList* list = &tls->my_rwlock_list;
assert(thelock);
assert(!list->pending);
__asm volatile("" ::: "memory");
WRITE_ONCE(list->pending, lock);
__asm volatile("" ::: "memory");
RLIST_REMOVE(&list->head, entry, next);
__asm volatile("" ::: "memory");
WRITE_ONCE(*thelock, 0);
__asm volatile("" ::: "memory");
WRITE_ONCE(list->pending, nullptr);
}
static void _rwlock_lock_read(Tls* tls, RwLock* lock, int i) {
uint32_t* reader = lock->reader + i;
RwLockHolder* reader_entry = lock->reader_entry + i;
lock->num_readers++;
reader_entry->lock = lock;
__rwlock_lock(tls, lock, reader, reader_entry);
}
static void _rwlock_unlock_read(Tls* tls, RwLock* lock, int i) {
uint32_t* reader = lock->reader + i;
RwLockHolder* reader_entry = lock->reader_entry + i;
assert(*reader == (uint32_t)tls->tid);
lock->num_readers--;
__rwlock_unlock(tls, lock, reader, reader_entry);
}
static void _rwlock_lock_write(Tls* tls, RwLock* lock) {
lock->writer_entry.lock = lock;
__rwlock_lock(tls, lock, &lock->writer, &lock->writer_entry);
}
static void _rwlock_unlock_write(Tls* tls, RwLock* lock) {
assert(lock->writer == (uint32_t)tls->tid);
__rwlock_unlock(tls, lock, &lock->writer, &lock->writer_entry);
}
void rwlock_lock_read(Tls* tls, RwLock* lock) {
int defer_twice = 2;
while (1) {
int ownerdead = mutex_lock(tls, &lock->mutex);
if (ownerdead) {
_rwlock_recover(lock);
}
if (lock->writer || lock->num_readers == holders_alloc ||
(lock->writer_waiter && lock->num_readers && defer_twice > 0)) {
int dead = rwlock_cleanup_dead(lock);
mutex_unlock(tls, &lock->mutex);
if (!dead) {
int ret = _mutex_wait(&lock->waiters);
if (ret == -ETIMEDOUT) {
defer_twice = 0;
} else {
defer_twice--;
}
}
continue;
}
for (int i = 0; i < holders_alloc; i++) {
if (!lock->reader[i]) {
_rwlock_lock_read(tls, lock, i);
mutex_unlock(tls, &lock->mutex);
return;
}
}
// mutex_unlock(tls, &lock->mutex);
abort();
}
}
void rwlock_unlock_read(Tls* tls, RwLock* lock) {
int ownerdead = mutex_lock(tls, &lock->mutex);
if (ownerdead) {
_rwlock_recover(lock);
}
for (int i = 0; i < holders_alloc; i++) {
if (lock->reader[i] == (uint32_t)tls->tid) {
_rwlock_unlock_read(tls, lock, i);
mutex_unlock(tls, &lock->mutex);
futex_wake(&lock->waiters, 1);
return;
}
}
// mutex_unlock(tls, &lock->mutex);
abort();
}
int rwlock_lock_write(Tls* tls, RwLock* lock) {
while (1) {
int ownerdead = mutex_lock(tls, &lock->mutex);
if (ownerdead) {
_rwlock_recover(lock);
}
if (lock->writer) {
int ownerdead = is_tid_dead(lock->writer);
if (ownerdead) {
_rwlock_lock_write(tls, lock);
if (lock->writer_waiter == (uint32_t)tls->tid) {
WRITE_ONCE(lock->writer_waiter, 0);
}
mutex_unlock(tls, &lock->mutex);
return 1;
}
}
if (lock->writer || lock->num_readers) {
int dead = rwlock_cleanup_dead(lock);
if (!lock->writer_waiter) {
WRITE_ONCE(lock->writer_waiter, tls->tid);
}
mutex_unlock(tls, &lock->mutex);
if (!dead) {
_mutex_wait(&lock->waiters);
}
continue;
}
_rwlock_lock_write(tls, lock);
if (lock->writer_waiter == (uint32_t)tls->tid) {
WRITE_ONCE(lock->writer_waiter, 0);
}
mutex_unlock(tls, &lock->mutex);
return 0;
}
}
void rwlock_unlock_write(Tls* tls, RwLock* lock) {
int ownerdead = mutex_lock(tls, &lock->mutex);
if (ownerdead) {
_rwlock_recover(lock);
}
_rwlock_unlock_write(tls, lock);
mutex_unlock(tls, &lock->mutex);
}
static void rwlock_recover_pending(Tls* tls) {
RwLockList* list = &tls->my_rwlock_list;
RwLock* lock = list->pending;
if (!lock) {
return;
}
int ownerdead = mutex_lock(tls, &lock->mutex);
if (ownerdead) {
_rwlock_recover(lock);
}
uint32_t* thelock = nullptr;
RwLockHolder* entry = nullptr;
if (lock->writer == (uint32_t)tls->tid) {
thelock = &lock->writer;
entry = &lock->writer_entry;
}
for (int i = 0; i < holders_alloc; i++) {
if (lock->reader[i] == (uint32_t)tls->tid) {
thelock = lock->reader + i;
entry = lock->reader_entry + i;
}
}
int found = 0;
RwLockHolder *elm, *temp;
RLIST_FOREACH(elm, &list->head, next, temp) {
if (elm == entry) {
found = 1;
break;
}
}
__asm volatile("" ::: "memory");
if (found) {
RLIST_REMOVE(&list->head, entry, next);
}
__asm volatile("" ::: "memory");
if (thelock) {
WRITE_ONCE(*thelock, FUTEX_TID_MASK);
}
__asm volatile("" ::: "memory");
WRITE_ONCE(list->pending, nullptr);
__asm volatile("" ::: "memory");
mutex_unlock(tls, &lock->mutex);
}
static void rwlock_recover_one(Tls* tls, RwLock* lock) {
RwLockList* list = &tls->my_rwlock_list;
assert(!list->pending);
int ownerdead = mutex_lock(tls, &lock->mutex);
if (ownerdead) {
_rwlock_recover(lock);
}
uint32_t* thelock = nullptr;
RwLockHolder* entry = nullptr;
if (lock->writer == (uint32_t)tls->tid) {
thelock = &lock->writer;
entry = &lock->writer_entry;
}
for (int i = 0; i < holders_alloc; i++) {
if (lock->reader[i] == (uint32_t)tls->tid) {
thelock = lock->reader + i;
entry = lock->reader_entry + i;
}
}
__asm volatile("" ::: "memory");
WRITE_ONCE(list->pending, lock);
__asm volatile("" ::: "memory");
RLIST_REMOVE(&list->head, entry, next);
__asm volatile("" ::: "memory");
WRITE_ONCE(*thelock, FUTEX_TID_MASK);
__asm volatile("" ::: "memory");
WRITE_ONCE(list->pending, nullptr);
mutex_unlock(tls, &lock->mutex);
}