-
Notifications
You must be signed in to change notification settings - Fork 790
/
locks.hpp
334 lines (281 loc) · 6.9 KB
/
locks.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
#pragma once
#define USING_NANO_TIMED_LOCKS (NANO_TIMED_LOCKS > 0)
#if USING_NANO_TIMED_LOCKS
#include <nano/lib/timer.hpp>
#endif
#include <condition_variable>
#include <mutex>
namespace nano
{
class mutex;
extern nano::mutex * mutex_to_filter;
extern nano::mutex mutex_to_filter_mutex;
bool should_be_filtered (const char * name);
bool any_filters_registered ();
enum class mutexes
{
active,
alarm,
block_arrival,
block_processor,
block_uniquer,
blockstore_cache,
confirmation_height_processor,
dropped_elections,
election_winner_details,
gap_cache,
network_filter,
observer_set,
request_aggregator,
state_block_signature_verification,
telemetry,
vote_generator,
vote_processor,
vote_uniquer,
votes_cache,
work_pool,
worker
};
char const * mutex_identifier (mutexes mutex);
class mutex
{
public:
mutex () = default;
mutex (const char * name_a)
#if USING_NANO_TIMED_LOCKS
:
name (name_a)
#endif
{
#if USING_NANO_TIMED_LOCKS
// This mutex should be filtered
if (name && should_be_filtered (name))
{
std::lock_guard guard (mutex_to_filter_mutex);
mutex_to_filter = this;
}
#endif
}
#if USING_NANO_TIMED_LOCKS
~mutex ()
{
// Unfilter this destroyed mutex
if (name && should_be_filtered (name))
{
// Unregister the mutex
std::lock_guard guard (mutex_to_filter_mutex);
mutex_to_filter = nullptr;
}
}
#endif
void lock ()
{
mutex_m.lock ();
}
void unlock ()
{
mutex_m.unlock ();
}
bool try_lock ()
{
return mutex_m.try_lock ();
}
#if USING_NANO_TIMED_LOCKS
const char * get_name () const
{
return name ? name : "";
}
#endif
private:
#if USING_NANO_TIMED_LOCKS
const char * name{ nullptr };
#endif
std::mutex mutex_m;
};
#if USING_NANO_TIMED_LOCKS
template <typename Mutex>
void output (const char * str, std::chrono::milliseconds time, Mutex & mutex);
template <typename Mutex>
void output_if_held_long_enough (nano::timer<std::chrono::milliseconds> & timer, Mutex & mutex);
#ifndef NANO_TIMED_LOCKS_IGNORE_BLOCKED
template <typename Mutex>
void output_if_blocked_long_enough (nano::timer<std::chrono::milliseconds> & timer, Mutex & mutex);
#endif
template <typename Mutex>
class lock_guard final
{
public:
explicit lock_guard (Mutex & mutex_a) :
guard (mutex_a)
{
}
lock_guard (const lock_guard &) = delete;
lock_guard & operator= (const lock_guard &) = delete;
private:
std::lock_guard<Mutex> guard;
};
template <>
class lock_guard<nano::mutex> final
{
public:
explicit lock_guard (nano::mutex & mutex_a);
~lock_guard () noexcept;
lock_guard (const lock_guard &) = delete;
lock_guard & operator= (const lock_guard &) = delete;
private:
nano::mutex & mut;
nano::timer<std::chrono::milliseconds> timer;
};
template <typename Mutex, typename = std::enable_if_t<std::is_same<Mutex, nano::mutex>::value>>
class unique_lock final
{
public:
unique_lock () = default;
explicit unique_lock (Mutex & mutex_a);
unique_lock (Mutex & mutex_a, std::defer_lock_t) noexcept;
unique_lock (unique_lock && other) = delete;
unique_lock & operator= (unique_lock && other) noexcept;
~unique_lock () noexcept;
unique_lock (const unique_lock &) = delete;
unique_lock & operator= (const unique_lock &) = delete;
void lock ();
bool try_lock ();
void unlock ();
bool owns_lock () const noexcept;
explicit operator bool () const noexcept;
Mutex * mutex () const noexcept;
private:
Mutex * mut{ nullptr };
bool owns{ false };
nano::timer<std::chrono::milliseconds> timer;
void validate () const;
void lock_impl ();
friend class condition_variable;
};
/** Assumes std implementations of std::condition_variable never actually call nano::unique_lock::lock/unlock,
but instead use OS intrinsics with the mutex handle directly. Due to this we also do not account for any
time the condition variable is blocked on another holder of the mutex. */
class condition_variable final
{
public:
condition_variable () = default;
condition_variable (condition_variable const &) = delete;
condition_variable & operator= (condition_variable const &) = delete;
void notify_one () noexcept;
void notify_all () noexcept;
void wait (nano::unique_lock<nano::mutex> & lt);
template <typename Pred>
void wait (nano::unique_lock<nano::mutex> & lk, Pred pred)
{
while (!pred ())
{
wait (lk);
}
}
template <typename Clock, typename Duration>
std::cv_status wait_until (nano::unique_lock<nano::mutex> & lk, std::chrono::time_point<Clock, Duration> const & timeout_time)
{
if (!lk.mut || !lk.owns)
{
throw (std::system_error (std::make_error_code (std::errc::operation_not_permitted)));
}
output_if_held_long_enough (lk.timer, *lk.mut);
// Start again in case cnd.wait calls unique_lock::lock/unlock () depending on some implementations
lk.timer.start ();
auto cv_status = cnd.wait_until (lk, timeout_time);
lk.timer.restart ();
return cv_status;
}
template <typename Clock, typename Duration, typename Pred>
bool wait_until (nano::unique_lock<nano::mutex> & lk, std::chrono::time_point<Clock, Duration> const & timeout_time, Pred pred)
{
while (!pred ())
{
if (wait_until (lk, timeout_time) == std::cv_status::timeout)
{
return pred ();
}
}
return true;
}
template <typename Rep, typename Period>
void wait_for (nano::unique_lock<nano::mutex> & lk, std::chrono::duration<Rep, Period> const & rel_time)
{
wait_until (lk, std::chrono::steady_clock::now () + rel_time);
}
template <typename Rep, typename Period, typename Pred>
bool wait_for (nano::unique_lock<nano::mutex> & lk, std::chrono::duration<Rep, Period> const & rel_time, Pred pred)
{
return wait_until (lk, std::chrono::steady_clock::now () + rel_time, std::move (pred));
}
private:
std::condition_variable_any cnd;
};
#else
template <typename Mutex>
using lock_guard = std::lock_guard<Mutex>;
template <typename Mutex>
using unique_lock = std::unique_lock<Mutex>;
// For consistency wrapping the less well known _any variant which can be used with any lockable type
using condition_variable = std::condition_variable_any;
#endif
/** A general purpose monitor template */
template <class T>
class locked
{
public:
using value_type = T;
template <typename... Args>
locked (Args &&... args) :
obj (std::forward<Args> (args)...)
{
}
struct scoped_lock final
{
scoped_lock (locked * owner_a) :
owner (owner_a)
{
owner->mutex.lock ();
}
~scoped_lock ()
{
owner->mutex.unlock ();
}
T * operator-> ()
{
return &owner->obj;
}
T & get () const
{
return owner->obj;
}
T & operator* () const
{
return get ();
}
locked * owner{ nullptr };
};
scoped_lock operator-> ()
{
return scoped_lock (this);
}
T & operator= (T const & other)
{
nano::unique_lock<nano::mutex> lk (mutex);
obj = other;
return obj;
}
operator T () const
{
return obj;
}
/** Returns a scoped lock wrapper, allowing multiple calls to the underlying object under the same lock */
scoped_lock lock ()
{
return scoped_lock (this);
}
private:
T obj;
nano::mutex mutex;
};
}