-
Notifications
You must be signed in to change notification settings - Fork 2
/
syncope.hpp
532 lines (455 loc) · 15.3 KB
/
syncope.hpp
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
#ifndef SYNCOPE_HPP
#define SYNCOPE_HPP
#ifndef SYNCOPE_NUM_LOCKS
# define SYNCOPE_NUM_LOCKS 0x100
#endif
#ifndef SYNCOPE_READ_SIDE_PARALLELISM
# define SYNCOPE_READ_SIDE_PARALLELISM 0x8
#endif
#ifndef SYNCOPE_MAX_LAYERS
# define SYNCOPE_MAX_LAYERS 100
#endif
#ifndef SYNCOPE_MAX_DEPTH
# define SYNCOPE_MAX_DEPTH 0x10
#endif
#ifdef SYNCOPE_DETECT_DEADLOCKS
#include <iostream>
#include <string>
#include <exception>
#include <sstream>
#endif
#include <mutex>
#include <memory>
#include <algorithm>
#include <thread>
#include <atomic>
#include <cassert>
namespace syncope {
namespace detail {
static const int CACHE_LINE_BITS = 6;
class LockLayerImpl;
struct TraceRoot {
typedef std::tuple<LockLayerImpl*, const char*> Owner;
std::unique_ptr<Owner[]> owners;
int top;
TraceRoot()
: owners(new Owner[SYNCOPE_MAX_DEPTH])
, top(0)
{
}
};
class Detector {
static const int TRANSITIONS_SIZE = SYNCOPE_MAX_LAYERS * SYNCOPE_MAX_LAYERS;
struct CounterWithPad {
std::atomic<size_t> counter;
char pad[64 - sizeof(counter)];
CounterWithPad()
: counter{0}
{
}
};
std::array<CounterWithPad, TRANSITIONS_SIZE> transitions;
Detector() {}
public:
static Detector& inst() {
static Detector d;
return d;
}
void on_lock(TraceRoot::Owner const& prev, TraceRoot::Owner const& curr);
void on_deadlock(LockLayerImpl* curr, const char* message);
};
class LockLayerImpl {
enum {
N = SYNCOPE_NUM_LOCKS
};
static_assert((N & (N - 1)) == 0, "N (SYNCOPE_NUM_LOCKS) must be a power of two");
static const int MASK = N - 1;
typedef std::mutex MutexT;
mutable std::array<MutexT, N> mutexes_;
const char* name_;
int level_;
const int id_;
static thread_local TraceRoot tls_root;
static std::atomic<int> layers_counter;
public:
LockLayerImpl(const char* name, int level)
: name_(name)
, level_(level)
, id_(layers_counter++)
{
}
LockLayerImpl(LockLayerImpl const&) = delete;
LockLayerImpl& operator = (LockLayerImpl const&) = delete;
void lock(size_t hash) {
size_t ix = hash & MASK;
mutexes_[ix].lock();
}
void unlock(size_t hash) {
size_t ix = hash & MASK;
mutexes_[ix].unlock();
}
int get_id() const {
return id_;
}
#ifdef SYNCOPE_DETECT_DEADLOCKS
void detector_lock(const char* loc) {
TraceRoot& root = tls_root;
if (root.top > SYNCOPE_MAX_DEPTH) {
report_error("max depth reached");
}
root.owners[root.top] = std::make_tuple(this, loc);
root.top++;
auto top = root.top;
for (int i = top - 2; i >= 0; i--) {
Detector::inst().on_lock( root.owners[i]
, root.owners[root.top-1]);
break;
}
}
void detector_unlock() {
TraceRoot& root = tls_root;
if (root.top == 0) {
report_error("double unlock");
}
root.top--;
}
void report_error(const char* message) const {
TraceRoot& root = tls_root;
std::stringstream sstream;
sstream << "Deadlock detector - " << message << std::endl;
for (auto i = 0; i < root.top; i++) {
auto layer = std::get<0>(root.owners[i]);
auto loc = std::get<1>(root.owners[i]);
sstream << "layer[" << i << "] is " << layer->name_ << " at " << loc << std::endl;
}
std::cout << sstream.str() << std::endl;
std::terminate();
}
#endif
};
std::atomic<int> LockLayerImpl::layers_counter{0};
thread_local TraceRoot LockLayerImpl::tls_root;
void Detector::on_deadlock(LockLayerImpl* curr, const char* message) {
#ifdef SYNCOPE_DETECT_DEADLOCKS
curr->report_error(message);
#endif
}
void Detector::on_lock(TraceRoot::Owner const& prev, TraceRoot::Owner const& curr) {
LockLayerImpl* prev_layer = std::get<0>(prev);
LockLayerImpl* curr_layer = std::get<0>(curr);
auto id_prev = prev_layer->get_id();
auto id_curr = curr_layer->get_id();
int x, y, dir;
if (id_prev > id_curr) {
x = id_prev;
y = id_curr;
dir = 1;
} else if (id_prev < id_curr){
y = id_prev;
x = id_curr;
dir = 2;
} else {
on_deadlock(curr_layer, "recursion detected");
return;
}
int addr = y*SYNCOPE_MAX_LAYERS + x;
int res = transitions[addr].counter.exchange(dir);
if (res && res != dir) {
on_deadlock(curr_layer, "deadlock detected");
}
}
} // namespace detail
// namespace locks
template<class T>
class LockGuard {
size_t value_;
bool owns_lock_;
detail::LockLayerImpl& lock_pool_;
#ifdef SYNCOPE_DETECT_DEADLOCKS
const char* loc_;
#endif
void lock() {
#ifdef SYNCOPE_DETECT_DEADLOCKS
lock_pool_.detector_lock(loc_);
#endif
lock_pool_.lock(value_);
owns_lock_ = true;
}
void unlock() {
#ifdef SYNCOPE_DETECT_DEADLOCKS
lock_pool_.detector_unlock();
#endif
lock_pool_.unlock(value_);
owns_lock_ = false;
}
public:
template<typename Hash>
LockGuard( T const* ptr
, detail::LockLayerImpl& lockpool
#ifdef SYNCOPE_DETECT_DEADLOCKS
, const char* loc
#endif
, Hash const& hash)
: value_(hash(reinterpret_cast<size_t>(ptr)))
, owns_lock_(false)
, lock_pool_(lockpool)
#ifdef SYNCOPE_DETECT_DEADLOCKS
, loc_(loc)
#endif
{
lock();
}
LockGuard(LockGuard const&) = delete;
LockGuard& operator = (LockGuard const&) = delete;
LockGuard(LockGuard&& other)
: value_(other.value_)
, owns_lock_(other.owns_lock_)
, lock_pool_(other.lock_pool_)
#ifdef SYNCOPE_DETECT_DEADLOCKS
, loc_(other.loc_)
#endif
{
other.owns_lock_ = false;
}
LockGuard& operator = (LockGuard&& other) {
value_ = other.value_;
other.owns_lock_ = false;
assert(&lock_pool_ == &other.lock_pool_);
#ifdef SYNCOPE_DETECT_DEADLOCKS
loc_ = other.loc_;
#endif
}
~LockGuard() {
if (owns_lock_) {
unlock();
}
}
};
template<int P, typename... T>
class LockGuardMany {
enum {
H = sizeof...(T)*P // hashes array size (can be greater than sizeof...(T))
};
detail::LockLayerImpl& impl_;
std::array<size_t, H> hashes_;
size_t hashes_count_;
bool owns_lock_;
#ifdef SYNCOPE_DETECT_DEADLOCKS
const char* loc_;
#endif
template<int I, int M, int H>
struct fill_hashes
{
template<typename Hash>
void operator () (std::array<size_t, H>& hashes, std::tuple<T const*...> const& items, Hash const& hash) {
const auto p = std::get<I>(items);
for (int i = 0; i < P; i++) {
size_t h = hash(reinterpret_cast<size_t>(p), i);
hashes[I*P + i] = h;
}
fill_hashes<I + 1, M, H> fh;
fh(hashes, items, hash);
}
};
template<int M, int H>
struct fill_hashes<M, M, H>{
template<typename Hash>
void operator() (std::array<size_t, H>& hashes, std::tuple<T const*...> const& items, Hash const& hash) {}
};
void lock() {
#ifdef SYNCOPE_DETECT_DEADLOCKS
impl_.detector_lock(loc_);
#endif
for (size_t i = 0; i < hashes_count_; i++) {
impl_.lock(hashes_[i]);
}
owns_lock_ = true;
}
void unlock() {
#ifdef SYNCOPE_DETECT_DEADLOCKS
impl_.detector_unlock();
#endif
for (size_t i = hashes_count_ - 1; i != size_t(0u) - 1; i--) {
impl_.unlock(hashes_[i]);
}
owns_lock_ = false;
}
public:
template<typename Hash>
LockGuardMany( detail::LockLayerImpl& impl
#ifdef SYNCOPE_DETECT_DEADLOCKS
, const char* loc
#endif
, Hash const& hash
, T const*... others)
: impl_(impl)
, owns_lock_(false)
#ifdef SYNCOPE_DETECT_DEADLOCKS
, loc_(loc)
#endif
{
auto all = std::tie(others...);
fill_hashes<0, sizeof...(others), H> fill_all;
fill_all(hashes_, all, hash);
std::sort(hashes_.begin(), hashes_.end());
auto it = std::unique(hashes_.begin(), hashes_.end());
hashes_count_ = std::distance(hashes_.begin(), it);
lock();
}
~LockGuardMany() {
if (owns_lock_) {
unlock();
}
}
LockGuardMany(LockGuardMany const&) = delete;
LockGuardMany& operator = (LockGuardMany const&) = delete;
LockGuardMany(LockGuardMany&& other)
: impl_(other.impl_)
, hashes_count_(other.hashes_count_)
, owns_lock_(other.owns_lock_)
{
std::swap(hashes_, other.hashes_);
other.owns_lock_ = false;
}
LockGuardMany& operator = (LockGuardMany&& other) {
assert(&other.impl_ == &impl_);
std::swap(hashes_, other.hashes_);
hashes_count_ = other.hashes_count_;
owns_lock_ = other.owns_lock_;
other.owns_lock_ = false;
}
};
namespace detail {
class StaticString {
const char* str_;
public:
StaticString(const char* s) : str_(s) {
// TODO: assert(string is static)
}
const char* str() const { return str_; }
};
//! Simple hash - simply returns it's argument
struct SimpleHash {
size_t operator() (size_t value) const {
return value >> CACHE_LINE_BITS;
}
};
//! Simple hash
struct SimpleHash2 {
size_t operator() (size_t value, int bias) const {
return value >> CACHE_LINE_BITS;
}
};
template<int P>
struct BiasedHash {
static_assert((P & (P - 1)) == 0, "P must be a power of two");
size_t operator() (size_t value) const {
std::hash<std::thread::id> hash;
auto id = std::this_thread::get_id();
size_t bias = hash(id);
return (value >> CACHE_LINE_BITS) + (bias & (P - 1));
}
};
template<int P>
struct BiasedHash2 {
static_assert((P & (P - 1)) == 0, "P must be a power of two");
size_t operator() (size_t value, int bias) const {
return (value >> CACHE_LINE_BITS) + (bias & (P - 1));
}
};
}
/** Lock hierarchy layer.
*/
class SymmetricLockLayer {
detail::LockLayerImpl impl_;
public:
/** C-tor
* @param name statically initialized string
*/
SymmetricLockLayer(detail::StaticString name, int level = -1) : impl_(name.str(), level) {}
#ifdef SYNCOPE_DETECT_DEADLOCKS
template<class T>
LockGuard<T> synchronize(
const char* loc,
T const* ptr) {
return std::move(LockGuard<T>(ptr, impl_, loc, detail::SimpleHash()));
}
#else
template<class T>
LockGuard<T> synchronize(T const* ptr) {
return std::move(LockGuard<T>(ptr, impl_, detail::SimpleHash()));
}
#endif
#ifdef SYNCOPE_DETECT_DEADLOCKS
template<typename... T>
LockGuardMany<1, T...> synchronize_all(
const char* loc,
T const*... args) {
return std::move(LockGuardMany<1, T...>(impl_, loc, detail::SimpleHash2(), args...));
}
#else
template<typename... T>
LockGuardMany<1, T...> synchronize_all(T const*... args) {
return std::move(LockGuardMany<1, T...>(impl_, detail::SimpleHash2(), args...));
}
#endif
};
/** Asymmetric lock hierarchy layer.
*/
class AsymmetricLockLayer {
detail::LockLayerImpl impl_;
enum {
P = SYNCOPE_READ_SIDE_PARALLELISM // Parallelism factor for readers and writers
};
public:
/** C-tor
* @param name statically initialized string
*/
AsymmetricLockLayer(detail::StaticString name, int level = -1) : impl_(name.str(), level) {}
#ifdef SYNCOPE_DETECT_DEADLOCKS
template<class T>
LockGuard<T> synchronize_read(
const char* loc,
T const* ptr) {
return std::move(LockGuard<T>(ptr, impl_, loc, detail::BiasedHash<P>()));
}
#else
template<class T>
LockGuard<T> synchronize_read(T const* ptr) {
return std::move(LockGuard<T>(ptr, impl_, detail::BiasedHash<P>()));
}
#endif
#ifdef SYNCOPE_DETECT_DEADLOCKS
template<typename T>
LockGuardMany<P, T> synchronize_write(
const char* loc,
T const* arg) {
return std::move(LockGuardMany<P, T>(impl_, loc, detail::BiasedHash2<P>(), arg));
}
#else
template<typename T>
LockGuardMany<P, T> synchronize_write(T const* arg) {
return std::move(LockGuardMany<P, T>(impl_, detail::BiasedHash2<P>(), arg));
}
#endif
};
} // namespace syncope
#define STATIC_STRING(x) syncope::detail::StaticString(x"")
#define SYNCOPE_STRINGIFY_DETAIL(x) #x
#define SYNCOPE_STRINGIFY(x) SYNCOPE_STRINGIFY_DETAIL(x)
#ifdef SYNCOPE_DETECT_DEADLOCKS
#define _SYNCOPE_LOCK_IMPL(layer, msg, ptr) auto __scope_lock_guard_##layer = layer.synchronize(msg, ptr)
#define SYNCOPE_LOCK(layer, ptr) _SYNCOPE_LOCK_IMPL(layer, __FILE__ ":" SYNCOPE_STRINGIFY(__LINE__), ptr);
#define _SYNCOPE_LOCK_ALL_IMPL(layer, msg, ...) auto __scope_lock_guard_##layer = layer.synchronize_all(msg, __VA_ARGS__)
#define SYNCOPE_LOCK_ALL(layer, ...) _SYNCOPE_LOCK_ALL_IMPL(layer, __FILE__ ":" SYNCOPE_STRINGIFY(__LINE__), __VA_ARGS__);
#define _SYNCOPE_LOCK_READ_IMPL(layer, msg, ptr) auto __scope_lock_guard_##layer = layer.synchronize_read(msg, ptr)
#define SYNCOPE_LOCK_READ(layer, ptr) _SYNCOPE_LOCK_READ_IMPL(layer, __FILE__ ":" SYNCOPE_STRINGIFY(__LINE__), ptr);
#define _SYNCOPE_LOCK_WRITE_IMPL(layer, msg, ptr) auto __scope_lock_guard_##layer = layer.synchronize_write(msg, ptr)
#define SYNCOPE_LOCK_WRITE(layer, ptr) _SYNCOPE_LOCK_WRITE_IMPL(layer, __FILE__ ":" SYNCOPE_STRINGIFY(__LINE__), ptr);
#else
#define SYNCOPE_LOCK(layer, ptr) auto __scope_lock_guard_##layer = layer.synchronize(ptr);
#define SYNCOPE_LOCK_ALL(layer, ...) auto __scope_lock_guard_##layer = layer.synchronize_all(__VA_ARGS__);
#define SYNCOPE_LOCK_READ(layer, ptr) auto __scope_lock_guard_##layer = layer.synchronize_read(ptr);
#define SYNCOPE_LOCK_WRITE(layer, ptr) auto __scope_lock_guard_##layer = layer.synchronize_write(ptr);
#endif
#endif