-
Notifications
You must be signed in to change notification settings - Fork 0
/
locks.cpp
290 lines (250 loc) · 6.88 KB
/
locks.cpp
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
#include <vxldollar/lib/config.hpp>
#include <vxldollar/lib/locks.hpp>
#include <vxldollar/lib/utility.hpp>
#include <boost/format.hpp>
#include <cstring>
#include <iostream>
#if USING_VXLDOLLAR_TIMED_LOCKS
namespace vxldollar
{
// These mutexes must have std::mutex interface in addition to "char const * get_name ()" method
template <typename Mutex>
void output (char const * str, std::chrono::milliseconds time, Mutex & mutex)
{
static vxldollar::mutex cout_mutex;
auto stacktrace = vxldollar::generate_stacktrace ();
// Guard standard out to keep the output from being interleaved
std::lock_guard guard (cout_mutex);
std::cout << (boost::format ("%1% Mutex %2% %3% for %4%ms\n%5%") % std::addressof (mutex) % mutex.get_name () % str % time.count () % stacktrace).str ()
<< std::endl;
}
template <typename Mutex>
void output_if_held_long_enough (vxldollar::timer<std::chrono::milliseconds> & timer, Mutex & mutex)
{
auto time_held = timer.since_start ();
if (time_held >= std::chrono::milliseconds (VXLDOLLAR_TIMED_LOCKS))
{
std::unique_lock lk (vxldollar::mutex_to_filter_mutex);
if (!vxldollar::any_filters_registered () || (vxldollar::mutex_to_filter == &mutex))
{
lk.unlock ();
output ("held", time_held, mutex);
}
}
if (timer.current_state () != vxldollar::timer_state::stopped)
{
timer.stop ();
}
}
#ifndef VXLDOLLAR_TIMED_LOCKS_IGNORE_BLOCKED
template <typename Mutex>
void output_if_blocked_long_enough (vxldollar::timer<std::chrono::milliseconds> & timer, Mutex & mutex)
{
auto time_blocked = timer.since_start ();
if (time_blocked >= std::chrono::milliseconds (VXLDOLLAR_TIMED_LOCKS))
{
std::unique_lock lk (vxldollar::mutex_to_filter_mutex);
if (!vxldollar::any_filters_registered () || (vxldollar::mutex_to_filter == &mutex))
{
lk.unlock ();
output ("blocked", time_blocked, mutex);
}
}
}
#endif
lock_guard<vxldollar::mutex>::lock_guard (vxldollar::mutex & mutex) :
mut (mutex)
{
timer.start ();
mut.lock ();
#ifndef VXLDOLLAR_TIMED_LOCKS_IGNORE_BLOCKED
output_if_blocked_long_enough (timer, mut);
#endif
}
lock_guard<vxldollar::mutex>::~lock_guard () noexcept
{
mut.unlock ();
output_if_held_long_enough (timer, mut);
}
template <typename Mutex, typename U>
unique_lock<Mutex, U>::unique_lock (Mutex & mutex) :
mut (std::addressof (mutex))
{
lock_impl ();
}
template <typename Mutex, typename U>
unique_lock<Mutex, U>::unique_lock (Mutex & mutex, std::defer_lock_t) noexcept :
mut (std::addressof (mutex))
{
}
template <typename Mutex, typename U>
void unique_lock<Mutex, U>::lock_impl ()
{
timer.start ();
mut->lock ();
owns = true;
#ifndef VXLDOLLAR_TIMED_LOCKS_IGNORE_BLOCKED
output_if_blocked_long_enough (timer, *mut);
#endif
}
template <typename Mutex, typename U>
unique_lock<Mutex, U> & unique_lock<Mutex, U>::operator= (unique_lock<Mutex, U> && other) noexcept
{
if (this != std::addressof (other))
{
if (owns)
{
mut->unlock ();
owns = false;
output_if_held_long_enough (timer, *mut);
}
mut = other.mut;
owns = other.owns;
timer = other.timer;
other.mut = nullptr;
other.owns = false;
}
return *this;
}
template <typename Mutex, typename U>
unique_lock<Mutex, U>::~unique_lock () noexcept
{
if (owns)
{
mut->unlock ();
owns = false;
output_if_held_long_enough (timer, *mut);
}
}
template <typename Mutex, typename U>
void unique_lock<Mutex, U>::lock ()
{
validate ();
lock_impl ();
}
template <typename Mutex, typename U>
bool unique_lock<Mutex, U>::try_lock ()
{
validate ();
owns = mut->try_lock ();
if (owns)
{
timer.start ();
}
return owns;
}
template <typename Mutex, typename U>
void unique_lock<Mutex, U>::unlock ()
{
if (!mut || !owns)
{
throw (std::system_error (std::make_error_code (std::errc::operation_not_permitted)));
}
mut->unlock ();
owns = false;
output_if_held_long_enough (timer, *mut);
}
template <typename Mutex, typename U>
bool unique_lock<Mutex, U>::owns_lock () const noexcept
{
return owns;
}
template <typename Mutex, typename U>
unique_lock<Mutex, U>::operator bool () const noexcept
{
return owns;
}
template <typename Mutex, typename U>
Mutex * unique_lock<Mutex, U>::mutex () const noexcept
{
return mut;
}
template <typename Mutex, typename U>
void unique_lock<Mutex, U>::validate () const
{
if (!mut)
{
throw (std::system_error (std::make_error_code (std::errc::operation_not_permitted)));
}
if (owns)
{
throw (std::system_error (std::make_error_code (std::errc::resource_deadlock_would_occur)));
}
}
// Explicit instantiations for allowed types
template class unique_lock<vxldollar::mutex>;
void condition_variable::notify_one () noexcept
{
cnd.notify_one ();
}
void condition_variable::notify_all () noexcept
{
cnd.notify_all ();
}
void condition_variable::wait (vxldollar::unique_lock<vxldollar::mutex> & lk)
{
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 ();
cnd.wait (lk);
lk.timer.restart ();
}
vxldollar::mutex * mutex_to_filter{ nullptr };
vxldollar::mutex mutex_to_filter_mutex;
bool should_be_filtered (char const * name)
{
return std::strcmp (name, xstr (VXLDOLLAR_TIMED_LOCKS_FILTER)) == 0;
}
bool any_filters_registered ()
{
return std::strcmp ("", xstr (VXLDOLLAR_TIMED_LOCKS_FILTER)) != 0;
}
}
#endif
char const * vxldollar::mutex_identifier (mutexes mutex)
{
switch (mutex)
{
case mutexes::active:
return "active";
case mutexes::block_arrival:
return "block_arrival";
case mutexes::block_processor:
return "block_processor";
case mutexes::block_uniquer:
return "block_uniquer";
case mutexes::blockstore_cache:
return "blockstore_cache";
case mutexes::confirmation_height_processor:
return "confirmation_height_processor";
case mutexes::election_winner_details:
return "election_winner_details";
case mutexes::gap_cache:
return "gap_cache";
case mutexes::network_filter:
return "network_filter";
case mutexes::observer_set:
return "observer_set";
case mutexes::request_aggregator:
return "request_aggregator";
case mutexes::state_block_signature_verification:
return "state_block_signature_verification";
case mutexes::telemetry:
return "telemetry";
case mutexes::vote_generator:
return "vote_generator";
case mutexes::vote_processor:
return "vote_processor";
case mutexes::vote_uniquer:
return "vote_uniquer";
case mutexes::votes_cache:
return "votes_cache";
case mutexes::work_pool:
return "work_pool";
}
throw std::runtime_error ("Invalid mutexes enum specified");
}