Merge #12075: [scripts] Add missing univalue file to copyright_header.py
[bitcoinplatinum.git] / src / sync.cpp
blobae6e721466add0142a9cd69e0e8b8b0b9e4619bc
1 // Copyright (c) 2011-2017 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 #include <sync.h>
7 #include <set>
8 #include <util.h>
9 #include <utilstrencodings.h>
11 #include <stdio.h>
13 #ifdef DEBUG_LOCKCONTENTION
14 #if !defined(HAVE_THREAD_LOCAL)
15 static_assert(false, "thread_local is not supported");
16 #endif
17 void PrintLockContention(const char* pszName, const char* pszFile, int nLine)
19 LogPrintf("LOCKCONTENTION: %s\n", pszName);
20 LogPrintf("Locker: %s:%d\n", pszFile, nLine);
22 #endif /* DEBUG_LOCKCONTENTION */
24 #ifdef DEBUG_LOCKORDER
26 // Early deadlock detection.
27 // Problem being solved:
28 // Thread 1 locks A, then B, then C
29 // Thread 2 locks D, then C, then A
30 // --> may result in deadlock between the two threads, depending on when they run.
31 // Solution implemented here:
32 // Keep track of pairs of locks: (A before B), (A before C), etc.
33 // Complain if any thread tries to lock in a different order.
36 struct CLockLocation {
37 CLockLocation(const char* pszName, const char* pszFile, int nLine, bool fTryIn)
39 mutexName = pszName;
40 sourceFile = pszFile;
41 sourceLine = nLine;
42 fTry = fTryIn;
45 std::string ToString() const
47 return mutexName + " " + sourceFile + ":" + itostr(sourceLine) + (fTry ? " (TRY)" : "");
50 private:
51 bool fTry;
52 std::string mutexName;
53 std::string sourceFile;
54 int sourceLine;
57 typedef std::vector<std::pair<void*, CLockLocation> > LockStack;
58 typedef std::map<std::pair<void*, void*>, LockStack> LockOrders;
59 typedef std::set<std::pair<void*, void*> > InvLockOrders;
61 struct LockData {
62 // Very ugly hack: as the global constructs and destructors run single
63 // threaded, we use this boolean to know whether LockData still exists,
64 // as DeleteLock can get called by global CCriticalSection destructors
65 // after LockData disappears.
66 bool available;
67 LockData() : available(true) {}
68 ~LockData() { available = false; }
70 LockOrders lockorders;
71 InvLockOrders invlockorders;
72 std::mutex dd_mutex;
73 } static lockdata;
75 static thread_local std::unique_ptr<LockStack> lockstack;
77 static void potential_deadlock_detected(const std::pair<void*, void*>& mismatch, const LockStack& s1, const LockStack& s2)
79 LogPrintf("POTENTIAL DEADLOCK DETECTED\n");
80 LogPrintf("Previous lock order was:\n");
81 for (const std::pair<void*, CLockLocation> & i : s2) {
82 if (i.first == mismatch.first) {
83 LogPrintf(" (1)");
85 if (i.first == mismatch.second) {
86 LogPrintf(" (2)");
88 LogPrintf(" %s\n", i.second.ToString());
90 LogPrintf("Current lock order is:\n");
91 for (const std::pair<void*, CLockLocation> & i : s1) {
92 if (i.first == mismatch.first) {
93 LogPrintf(" (1)");
95 if (i.first == mismatch.second) {
96 LogPrintf(" (2)");
98 LogPrintf(" %s\n", i.second.ToString());
100 assert(false);
103 static void push_lock(void* c, const CLockLocation& locklocation)
105 if (!lockstack)
106 lockstack.reset(new LockStack);
108 std::lock_guard<std::mutex> lock(lockdata.dd_mutex);
110 lockstack->push_back(std::make_pair(c, locklocation));
112 for (const std::pair<void*, CLockLocation> & i : (*lockstack)) {
113 if (i.first == c)
114 break;
116 std::pair<void*, void*> p1 = std::make_pair(i.first, c);
117 if (lockdata.lockorders.count(p1))
118 continue;
119 lockdata.lockorders[p1] = (*lockstack);
121 std::pair<void*, void*> p2 = std::make_pair(c, i.first);
122 lockdata.invlockorders.insert(p2);
123 if (lockdata.lockorders.count(p2))
124 potential_deadlock_detected(p1, lockdata.lockorders[p2], lockdata.lockorders[p1]);
128 static void pop_lock()
130 (*lockstack).pop_back();
133 void EnterCritical(const char* pszName, const char* pszFile, int nLine, void* cs, bool fTry)
135 push_lock(cs, CLockLocation(pszName, pszFile, nLine, fTry));
138 void LeaveCritical()
140 pop_lock();
143 std::string LocksHeld()
145 std::string result;
146 for (const std::pair<void*, CLockLocation> & i : *lockstack)
147 result += i.second.ToString() + std::string("\n");
148 return result;
151 void AssertLockHeldInternal(const char* pszName, const char* pszFile, int nLine, void* cs)
153 for (const std::pair<void*, CLockLocation> & i : *lockstack)
154 if (i.first == cs)
155 return;
156 fprintf(stderr, "Assertion failed: lock %s not held in %s:%i; locks held:\n%s", pszName, pszFile, nLine, LocksHeld().c_str());
157 abort();
160 void AssertLockNotHeldInternal(const char* pszName, const char* pszFile, int nLine, void* cs)
162 for (const std::pair<void*, CLockLocation>& i : *lockstack) {
163 if (i.first == cs) {
164 fprintf(stderr, "Assertion failed: lock %s held in %s:%i; locks held:\n%s", pszName, pszFile, nLine, LocksHeld().c_str());
165 abort();
170 void DeleteLock(void* cs)
172 if (!lockdata.available) {
173 // We're already shutting down.
174 return;
176 std::lock_guard<std::mutex> lock(lockdata.dd_mutex);
177 std::pair<void*, void*> item = std::make_pair(cs, nullptr);
178 LockOrders::iterator it = lockdata.lockorders.lower_bound(item);
179 while (it != lockdata.lockorders.end() && it->first.first == cs) {
180 std::pair<void*, void*> invitem = std::make_pair(it->first.second, it->first.first);
181 lockdata.invlockorders.erase(invitem);
182 lockdata.lockorders.erase(it++);
184 InvLockOrders::iterator invit = lockdata.invlockorders.lower_bound(item);
185 while (invit != lockdata.invlockorders.end() && invit->first == cs) {
186 std::pair<void*, void*> invinvitem = std::make_pair(invit->second, invit->first);
187 lockdata.lockorders.erase(invinvitem);
188 lockdata.invlockorders.erase(invit++);
192 #endif /* DEBUG_LOCKORDER */