-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathfairlocks.cpp
More file actions
152 lines (125 loc) · 3.59 KB
/
Copy pathfairlocks.cpp
File metadata and controls
152 lines (125 loc) · 3.59 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
#include "fairlocks.hpp"
#include <linux/futex.h>
#include <unistd.h>
#include <sys/syscall.h>
#ifdef __x86_64__
#include <immintrin.h>
inline void cpu_relax() { _mm_pause(); }
#elif defined(__aarch64__)
inline void cpu_relax() { asm volatile ("yield"); }
#else
inline void cpu_relax() {}
#endif
using namespace locks;
void spin_base::unlock() {
islocked.store(false, std::memory_order_release);
}
template <typename S>
static inline void spin_impl(std::atomic<bool>& is_locked, S spinner) {
do {
while (is_locked.load(std::memory_order_relaxed)) {
spinner();
}
} while (is_locked.exchange(true, std::memory_order_acquire));
}
void spinlock_hot::lock() { spin_impl(islocked, []{}); }
void spinlock_pause::lock() { spin_impl(islocked, cpu_relax); }
void spinlock_yield::lock() { spin_impl(islocked, []{ sched_yield(); }); }
void blocking_ticket::lock() {
auto ticket = dispenser.fetch_add(1, std::memory_order_relaxed);
if (ticket == serving.load(std::memory_order_acquire))
return;
std::unique_lock<std::mutex> lock(mutex);
while (ticket != serving.load(std::memory_order_acquire)) {
cvar.wait(lock);
}
}
void blocking_ticket::unlock() {
std::unique_lock<std::mutex> lock(mutex);
auto s = serving.load(std::memory_order_relaxed) + 1;
serving.store(s, std::memory_order_release);
auto d = dispenser.load(std::memory_order_relaxed);
assert(s <= d);
if (s < d) {
// wake waiters
cvar.notify_all();
}
}
struct fifo_queued::queue_elem {
std::condition_variable cvar;
bool owner = false;
};
void fifo_queued::lock() {
std::unique_lock<std::mutex> guard(mutex);
if (!locked) {
locked = true;
return;
}
queue_elem node;
cvar_queue.push_back(&node);
do {
node.cvar.wait(guard);
} while (!node.owner);
assert(locked && cvar_queue.front() == &node);
cvar_queue.pop_front();
}
void fifo_queued::unlock() {
std::unique_lock<std::mutex> guard(mutex);
if (cvar_queue.empty()) {
locked = false;
} else {
auto& next = cvar_queue.front();
next->owner = true;
next->cvar.notify_one();
}
}
int cmpxchg(int& var, int old, int desired) {
return __sync_val_compare_and_swap(&var, old, desired);
}
int xchg(int& var, int val) {
return __atomic_exchange_n(&var, val, __ATOMIC_ACQUIRE);
}
int atomic_dec(int& var) {
return __sync_fetch_and_sub(&var, 1);
}
/**
* The futex related calls are cribbed from:
* // https://github.com/eliben/code-for-blog/blob/master/2018/futex-basics/futex-basic-process.c
*/
int futex(int* uaddr, int futex_op, int val, const struct timespec* timeout,
int* uaddr2, int val3) {
return syscall(SYS_futex, uaddr, futex_op, val, timeout, uaddr2, val3);
}
void futex_wait(int* futex_addr, int val) {
int ret = futex(futex_addr, FUTEX_WAIT, val, NULL, NULL, 0);
(void)ret;
assert(ret == 0 || (ret == -1 && errno == EAGAIN));
}
void futex_wake(int* futex_addr, int nwait) {
int futex_rc = futex(futex_addr, FUTEX_WAKE, nwait, NULL, NULL, 0);
if (futex_rc == -1) {
perror("futex wake");
exit(1);
}
}
void mutex3::lock() {
int c;
if ((c = cmpxchg(val, 0, 1)) != 0) {
if (c != 2) {
c = xchg(val, 2);
}
while (c != 0) {
futex_wait(&val, 2);
c = xchg(val, 2);
}
}
}
void mutex3::unlock() {
if (atomic_dec(val) != 1) {
// printf("%d unlock wake\n", tid);
val = 0;
futex_wake(&val, 1);
} else {
// printf("%d unlock fast\n", tid);
}
}