5 #if defined(HAVE_CONFIG_H) 16 #include <boost/thread/mutex.hpp> 21 #include <system_error> 23 #include <type_traits> 24 #include <unordered_map> 28 #ifdef DEBUG_LOCKCONTENTION 29 #if !defined(HAVE_THREAD_LOCAL) 30 static_assert(
false,
"thread_local is not supported");
32 void PrintLockContention(
const char* pszName,
const char* pszFile,
int nLine)
34 LogPrintf(
"LOCKCONTENTION: %s\n", pszName);
35 LogPrintf(
"Locker: %s:%d\n", pszFile, nLine);
39 #ifdef DEBUG_LOCKORDER 51 struct CLockLocation {
57 const std::string& thread_name)
61 m_thread_name(thread_name),
67 "'%s' in %s:%s%s (in thread '%s')",
68 mutexName, sourceFile, sourceLine, (fTry ?
" (TRY)" :
""), m_thread_name);
71 std::string Name()
const 78 std::string mutexName;
79 std::string sourceFile;
80 const std::string& m_thread_name;
84 using LockStackItem = std::pair<void*, CLockLocation>;
85 using LockStack = std::vector<LockStackItem>;
86 using LockStacks = std::unordered_map<std::thread::id, LockStack>;
88 using LockPair = std::pair<void*, void*>;
89 using LockOrders = std::map<LockPair, LockStack>;
90 using InvLockOrders = std::set<LockPair>;
93 LockStacks m_lock_stacks;
94 LockOrders lockorders;
95 InvLockOrders invlockorders;
99 LockData& GetLockData() {
104 static LockData& lock_data = *
new LockData();
108 static void potential_deadlock_detected(
const LockPair& mismatch,
const LockStack& s1,
const LockStack& s2)
110 LogPrintf(
"POTENTIAL DEADLOCK DETECTED\n");
112 for (
const LockStackItem& i : s1) {
113 if (i.first == mismatch.first) {
116 if (i.first == mismatch.second) {
122 std::string mutex_a, mutex_b;
124 for (
const LockStackItem& i : s2) {
125 if (i.first == mismatch.first) {
127 mutex_a = i.second.Name();
129 if (i.first == mismatch.second) {
131 mutex_b = i.second.Name();
135 if (g_debug_lockorder_abort) {
136 tfm::format(std::cerr,
"Assertion failed: detected inconsistent lock order for %s, details in debug log.\n", s2.back().second.ToString());
139 throw std::logic_error(
strprintf(
"potential deadlock detected: %s -> %s -> %s", mutex_b, mutex_a, mutex_b));
142 static void double_lock_detected(
const void* mutex,
const LockStack& lock_stack)
146 for (
const LockStackItem& i : lock_stack) {
147 if (i.first == mutex) {
152 if (g_debug_lockorder_abort) {
154 "Assertion failed: detected double lock for %s, details in debug log.\n",
155 lock_stack.back().second.ToString());
158 throw std::logic_error(
"double lock detected");
161 template <
typename MutexType>
162 static void push_lock(MutexType* c,
const CLockLocation& locklocation)
164 constexpr
bool is_recursive_mutex =
165 std::is_base_of<RecursiveMutex, MutexType>::value ||
166 std::is_base_of<std::recursive_mutex, MutexType>::value;
168 LockData& lockdata = GetLockData();
169 std::lock_guard<std::mutex> lock(lockdata.dd_mutex);
171 LockStack& lock_stack = lockdata.m_lock_stacks[std::this_thread::get_id()];
172 lock_stack.emplace_back(c, locklocation);
173 for (
size_t j = 0; j < lock_stack.size() - 1; ++j) {
174 const LockStackItem& i = lock_stack[j];
176 if (is_recursive_mutex) {
183 auto lock_stack_copy = lock_stack;
184 lock_stack.pop_back();
185 double_lock_detected(c, lock_stack_copy);
189 const LockPair p1 = std::make_pair(i.first, c);
190 if (lockdata.lockorders.count(p1))
193 const LockPair p2 = std::make_pair(c, i.first);
194 if (lockdata.lockorders.count(p2)) {
195 auto lock_stack_copy = lock_stack;
196 lock_stack.pop_back();
197 potential_deadlock_detected(p1, lockdata.lockorders[p2], lock_stack_copy);
201 lockdata.lockorders.emplace(p1, lock_stack);
202 lockdata.invlockorders.insert(p2);
206 static void pop_lock()
208 LockData& lockdata = GetLockData();
209 std::lock_guard<std::mutex> lock(lockdata.dd_mutex);
211 LockStack& lock_stack = lockdata.m_lock_stacks[std::this_thread::get_id()];
212 lock_stack.pop_back();
213 if (lock_stack.empty()) {
214 lockdata.m_lock_stacks.erase(std::this_thread::get_id());
218 template <
typename MutexType>
219 void EnterCritical(
const char* pszName,
const char* pszFile,
int nLine, MutexType*
cs,
bool fTry)
225 template void EnterCritical(
const char*,
const char*,
int, std::mutex*,
bool);
226 template void EnterCritical(
const char*,
const char*,
int, std::recursive_mutex*,
bool);
227 template void EnterCritical(
const char*,
const char*,
int, boost::mutex*,
bool);
229 void CheckLastCritical(
void* cs, std::string& lockname,
const char* guardname,
const char* file,
int line)
231 LockData& lockdata = GetLockData();
232 std::lock_guard<std::mutex> lock(lockdata.dd_mutex);
234 const LockStack& lock_stack = lockdata.m_lock_stacks[std::this_thread::get_id()];
235 if (!lock_stack.empty()) {
236 const auto& lastlock = lock_stack.back();
237 if (lastlock.first == cs) {
238 lockname = lastlock.second.Name();
243 LogPrintf(
"INCONSISTENT LOCK ORDER DETECTED\n");
244 LogPrintf(
"Current lock order (least recent first) is:\n");
245 for (
const LockStackItem& i : lock_stack) {
248 if (g_debug_lockorder_abort) {
249 tfm::format(std::cerr,
"%s:%s %s was not most recent critical section locked, details in debug log.\n", file, line, guardname);
252 throw std::logic_error(
strprintf(
"%s was not most recent critical section locked", guardname));
260 std::string LocksHeld()
262 LockData& lockdata = GetLockData();
263 std::lock_guard<std::mutex> lock(lockdata.dd_mutex);
265 const LockStack& lock_stack = lockdata.m_lock_stacks[std::this_thread::get_id()];
267 for (
const LockStackItem& i : lock_stack)
268 result += i.second.ToString() + std::string(
"\n");
272 static bool LockHeld(
void* mutex)
274 LockData& lockdata = GetLockData();
275 std::lock_guard<std::mutex> lock(lockdata.dd_mutex);
277 const LockStack& lock_stack = lockdata.m_lock_stacks[std::this_thread::get_id()];
278 for (
const LockStackItem& i : lock_stack) {
279 if (i.first == mutex)
return true;
285 template <
typename MutexType>
288 if (LockHeld(cs))
return;
289 tfm::format(std::cerr,
"Assertion failed: lock %s not held in %s:%i; locks held:\n%s", pszName, pszFile, nLine, LocksHeld());
295 template <
typename MutexType>
298 if (!LockHeld(cs))
return;
299 tfm::format(std::cerr,
"Assertion failed: lock %s held in %s:%i; locks held:\n%s", pszName, pszFile, nLine, LocksHeld());
307 LockData& lockdata = GetLockData();
308 std::lock_guard<std::mutex> lock(lockdata.dd_mutex);
309 const LockPair item = std::make_pair(cs,
nullptr);
310 LockOrders::iterator
it = lockdata.lockorders.lower_bound(item);
311 while (it != lockdata.lockorders.end() && it->first.first ==
cs) {
312 const LockPair invitem = std::make_pair(it->first.second, it->first.first);
313 lockdata.invlockorders.erase(invitem);
314 lockdata.lockorders.erase(it++);
316 InvLockOrders::iterator invit = lockdata.invlockorders.lower_bound(item);
317 while (invit != lockdata.invlockorders.end() && invit->first ==
cs) {
318 const LockPair invinvitem = std::make_pair(invit->second, invit->first);
319 lockdata.lockorders.erase(invinvitem);
320 lockdata.invlockorders.erase(invit++);
326 LockData& lockdata = GetLockData();
327 std::lock_guard<std::mutex> lock(lockdata.dd_mutex);
328 const auto it = lockdata.m_lock_stacks.find(std::this_thread::get_id());
329 if (it == lockdata.m_lock_stacks.end()) {
332 return it->second.empty();
335 bool g_debug_lockorder_abort =
true;
std::deque< CInv >::iterator it
void AssertLockNotHeldInternal(const char *pszName, const char *pszFile, int nLine, MutexType *cs) EXCLUSIVE_LOCKS_REQUIRED(!cs)
static void LogPrintf(const char *fmt, const Args &... args)
void CheckLastCritical(void *cs, std::string &lockname, const char *guardname, const char *file, int line)
std::string ToString(const T &t)
Locale-independent version of std::to_string.
void DeleteLock(void *cs)
void EnterCritical(const char *pszName, const char *pszFile, int nLine, MutexType *cs, bool fTry=false)
void AssertLockHeldInternal(const char *pszName, const char *pszFile, int nLine, MutexType *cs) EXCLUSIVE_LOCKS_REQUIRED(cs)
const std::string & ThreadGetInternalName()
Get the thread's internal (in-memory) name; used e.g.