Skip to content

Commit 8722e54

Browse files
committed
threads: add thread names to deadlock debugging message
Also refactor CLockLocation to use an initialization list.
1 parent 383b186 commit 8722e54

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

src/sync.cpp

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

55
#include <sync.h>
6+
#include <tinyformat.h>
67

78
#include <logging.h>
89
#include <util/strencodings.h>
10+
#include <util/threadnames.h>
911

1012
#include <stdio.h>
1113

@@ -37,23 +39,30 @@ void PrintLockContention(const char* pszName, const char* pszFile, int nLine)
3739
//
3840

3941
struct CLockLocation {
40-
CLockLocation(const char* pszName, const char* pszFile, int nLine, bool fTryIn)
41-
{
42-
mutexName = pszName;
43-
sourceFile = pszFile;
44-
sourceLine = nLine;
45-
fTry = fTryIn;
46-
}
42+
CLockLocation(
43+
const char* pszName,
44+
const char* pszFile,
45+
int nLine,
46+
bool fTryIn,
47+
const std::string& thread_name)
48+
: fTry(fTryIn),
49+
mutexName(pszName),
50+
sourceFile(pszFile),
51+
m_thread_name(thread_name),
52+
sourceLine(nLine) {}
4753

4854
std::string ToString() const
4955
{
50-
return mutexName + " " + sourceFile + ":" + itostr(sourceLine) + (fTry ? " (TRY)" : "");
56+
return tfm::format(
57+
"%s %s:%s%s (in thread %s)",
58+
mutexName, sourceFile, itostr(sourceLine), (fTry ? " (TRY)" : ""), m_thread_name);
5159
}
5260

5361
private:
5462
bool fTry;
5563
std::string mutexName;
5664
std::string sourceFile;
65+
const std::string& m_thread_name;
5766
int sourceLine;
5867
};
5968

@@ -125,7 +134,7 @@ static void push_lock(void* c, const CLockLocation& locklocation)
125134
std::pair<void*, void*> p1 = std::make_pair(i.first, c);
126135
if (lockdata.lockorders.count(p1))
127136
continue;
128-
lockdata.lockorders[p1] = g_lockstack;
137+
lockdata.lockorders.emplace(p1, g_lockstack);
129138

130139
std::pair<void*, void*> p2 = std::make_pair(c, i.first);
131140
lockdata.invlockorders.insert(p2);
@@ -141,7 +150,7 @@ static void pop_lock()
141150

142151
void EnterCritical(const char* pszName, const char* pszFile, int nLine, void* cs, bool fTry)
143152
{
144-
push_lock(cs, CLockLocation(pszName, pszFile, nLine, fTry));
153+
push_lock(cs, CLockLocation(pszName, pszFile, nLine, fTry, util::ThreadGetInternalName()));
145154
}
146155

147156
void LeaveCritical()

0 commit comments

Comments
 (0)