-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextend_mutex.h
More file actions
224 lines (176 loc) · 5.26 KB
/
extend_mutex.h
File metadata and controls
224 lines (176 loc) · 5.26 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
/**
* @file extend_mutex.h
* @brief POSIX Lock extensions for C++
* @version 0.1
* @date 2022-05-27
*/
#ifndef __EXTEND_MUTEX_H__
#define __EXTEND_MUTEX_H__
#include <atomic>
#include <pthread.h>
#include <thread>
class shared_mutex {
pthread_rwlock_t rwlock;
public:
shared_mutex() { pthread_rwlock_init(&rwlock, nullptr); }
~shared_mutex() { pthread_rwlock_destroy(&rwlock); }
void lock_shared() { pthread_rwlock_rdlock(&rwlock); }
bool try_lock_shared() { return pthread_rwlock_tryrdlock(&rwlock) == 0; }
bool try_lock() { return pthread_rwlock_trywrlock(&rwlock) == 0; }
void lock() { pthread_rwlock_wrlock(&rwlock); }
void unlock() { pthread_rwlock_unlock(&rwlock); }
};
class spin_mutex {
pthread_spinlock_t spinlock;
public:
spin_mutex(int pshared = PTHREAD_PROCESS_PRIVATE) {
pthread_spin_init(&spinlock, pshared);
}
~spin_mutex() { pthread_spin_destroy(&spinlock); }
void lock() { pthread_spin_lock(&spinlock); }
bool try_lock() { return pthread_spin_trylock(&spinlock) == 0; }
void unlock() { pthread_spin_unlock(&spinlock); }
};
class barrier_t {
pthread_barrier_t b;
public:
barrier_t() { pthread_barrier_init(&b, nullptr, 0); }
barrier_t(unsigned int count) { pthread_barrier_init(&b, nullptr, count); }
~barrier_t() { pthread_barrier_destroy(&b); }
void wait() { pthread_barrier_wait(&b); }
};
template <typename D> class __spin_mutex_impl {
public:
__spin_mutex_impl() : l(0) {}
void lock(uint8_t i) {
while (!try_lock(i)) {
std::this_thread::yield();
}
}
void lock() { lock(0); }
bool try_lock(uint8_t i) {
if (check_lock(l.load(std::memory_order_relaxed), i))
return false;
D l_ = l.fetch_or(1UL << i, std::memory_order_acquire);
return !check_lock(l_, i);
}
bool try_lock() { return try_lock(0); }
void unlock(uint8_t i) { l.fetch_xor(1UL << i, std::memory_order_release); }
void unlock() { unlock(0); }
private:
static bool check_lock(D l_, uint8_t i) { return (l_ & (1UL << i)) != 0; }
std::atomic<D> l;
};
using spin_mutex_b8 = __spin_mutex_impl<uint8_t>;
using spin_mutex_b16 = __spin_mutex_impl<uint16_t>;
using spin_mutex_b32 = __spin_mutex_impl<uint32_t>;
using spin_mutex_b64 = __spin_mutex_impl<uint64_t>;
class ticket_mutex {
public:
void lock() {
uint16_t ct = v.h.fetch_add(1, std::memory_order_acquire);
while (v.l.load(std::memory_order_acquire) != ct) {
std::this_thread::yield();
}
}
bool try_lock() {
raw_val_t ov, nv;
ov.raw = v.raw.load(std::memory_order_relaxed);
if (ov.h != ov.l)
return false;
nv = ov;
++nv.h;
return v.raw.compare_exchange_weak(ov.raw, nv.raw,
std::memory_order_acquire);
}
void unlock() { v.l.fetch_add(1, std::memory_order_release); }
private:
union atomic_val_t {
struct {
std::atomic<uint16_t> h;
std::atomic<uint16_t> l;
};
std::atomic<uint32_t> raw;
atomic_val_t() : raw(0) {}
};
union raw_val_t {
struct {
uint16_t h;
uint16_t l;
};
uint32_t raw;
};
atomic_val_t v;
};
// high read priority
template <typename D> class __shared_mutex_impl {
public:
static constexpr size_t size = sizeof(D);
__shared_mutex_impl() : l(0) {}
void lock() {
while (!try_lock()) {
std::this_thread::yield();
}
}
void lock_shared() {
D l_ = l.fetch_add(2, std::memory_order_acquire);
while (l_ & 1) {
std::this_thread::yield();
l_ = l.load(std::memory_order_acquire);
}
}
bool try_lock() {
D l_ = 0;
return l.compare_exchange_weak(l_, 1, std::memory_order_acquire);
}
bool try_lock_shared() {
D l_ = l.fetch_add(2, std::memory_order_acquire);
if (l_ & 1) {
l.fetch_sub(2, std::memory_order_release);
return false;
}
return true;
}
void unlock() { l.fetch_xor(1, std::memory_order_release); }
void unlock_shared() { l.fetch_sub(2, std::memory_order_release); }
private:
std::atomic<D> l;
};
using shared_mutex_b8 = __shared_mutex_impl<uint8_t>;
using shared_mutex_b16 = __shared_mutex_impl<uint16_t>;
using shared_mutex_b32 = __shared_mutex_impl<uint32_t>;
using shared_mutex_b64 = __shared_mutex_impl<uint64_t>;
template <typename D> struct __intention_mutex_impl {
__intention_mutex_impl() : l(0) {}
void lock_shared() {
D l_ = l.load(std::memory_order_acquire);
while (1) {
if (l_ & 1) {
std::this_thread::yield();
l_ = l.load(std::memory_order_acquire);
} else if (l.compare_exchange_weak(l_, l_ + 2,
std::memory_order_acquire)) {
break;
}
}
}
bool try_lock_prompt() {
D l_ = l.fetch_or(1, std::memory_order_acquire);
if (l_ & 1)
return false;
unlock_shared();
while (l.load(std::memory_order_acquire) & ~(1UL)) {
std::this_thread::yield();
}
return true;
}
void unlock() { l.store(0, std::memory_order_release); }
void unlock_shared() { l.fetch_sub(2, std::memory_order_release); }
private:
std::atomic<D> l;
};
using intention_mutex_b8 = __intention_mutex_impl<uint8_t>;
using intention_mutex_b16 = __intention_mutex_impl<uint16_t>;
using intention_mutex_b32 = __intention_mutex_impl<uint32_t>;
using intention_mutex_b64 = __intention_mutex_impl<uint64_t>;
#endif // __EXTEND_MUTEX_H__