Use unique_ptr for pdbCopy (Db) and fix potential memory leak
[bitcoinplatinum.git] / src / sync.cpp
blob87024ccdf2810d726234e18c26f6cec259fab217
1 // Copyright (c) 2011-2016 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 "util.h"
8 #include "utilstrencodings.h"
10 #include <stdio.h>
12 #include <boost/thread.hpp>
14 #ifdef DEBUG_LOCKCONTENTION
15 void PrintLockContention(const char* pszName, const char* pszFile, int nLine)
17 LogPrintf("LOCKCONTENTION: %s\n", pszName);
18 LogPrintf("Locker: %s:%d\n", pszFile, nLine);
20 #endif /* DEBUG_LOCKCONTENTION */
22 #ifdef DEBUG_LOCKORDER
24 // Early deadlock detection.
25 // Problem being solved:
26 // Thread 1 locks A, then B, then C
27 // Thread 2 locks D, then C, then A
28 // --> may result in deadlock between the two threads, depending on when they run.
29 // Solution implemented here:
30 // Keep track of pairs of locks: (A before B), (A before C), etc.
31 // Complain if any thread tries to lock in a different order.
34 struct CLockLocation {
35 CLockLocation(const char* pszName, const char* pszFile, int nLine, bool fTryIn)
37 mutexName = pszName;
38 sourceFile = pszFile;
39 sourceLine = nLine;
40 fTry = fTryIn;
43 std::string ToString() const
45 return mutexName + " " + sourceFile + ":" + itostr(sourceLine) + (fTry ? " (TRY)" : "");
48 bool fTry;
49 private:
50 std::string mutexName;
51 std::string sourceFile;
52 int sourceLine;
55 typedef std::vector<std::pair<void*, CLockLocation> > LockStack;
56 typedef std::map<std::pair<void*, void*>, LockStack> LockOrders;
57 typedef std::set<std::pair<void*, void*> > InvLockOrders;
59 struct LockData {
60 // Very ugly hack: as the global constructs and destructors run single
61 // threaded, we use this boolean to know whether LockData still exists,
62 // as DeleteLock can get called by global CCriticalSection destructors
63 // after LockData disappears.
64 bool available;
65 LockData() : available(true) {}
66 ~LockData() { available = false; }
68 LockOrders lockorders;
69 InvLockOrders invlockorders;
70 boost::mutex dd_mutex;
71 } static lockdata;
73 boost::thread_specific_ptr<LockStack> lockstack;
75 static void potential_deadlock_detected(const std::pair<void*, void*>& mismatch, const LockStack& s1, const LockStack& s2)
77 LogPrintf("POTENTIAL DEADLOCK DETECTED\n");
78 LogPrintf("Previous lock order was:\n");
79 for (const std::pair<void*, CLockLocation> & i : s2) {
80 if (i.first == mismatch.first) {
81 LogPrintf(" (1)");
83 if (i.first == mismatch.second) {
84 LogPrintf(" (2)");
86 LogPrintf(" %s\n", i.second.ToString());
88 LogPrintf("Current lock order is:\n");
89 for (const std::pair<void*, CLockLocation> & i : s1) {
90 if (i.first == mismatch.first) {
91 LogPrintf(" (1)");
93 if (i.first == mismatch.second) {
94 LogPrintf(" (2)");
96 LogPrintf(" %s\n", i.second.ToString());
98 assert(false);
101 static void push_lock(void* c, const CLockLocation& locklocation)
103 if (lockstack.get() == nullptr)
104 lockstack.reset(new LockStack);
106 boost::unique_lock<boost::mutex> lock(lockdata.dd_mutex);
108 (*lockstack).push_back(std::make_pair(c, locklocation));
110 for (const std::pair<void*, CLockLocation> & i : (*lockstack)) {
111 if (i.first == c)
112 break;
114 std::pair<void*, void*> p1 = std::make_pair(i.first, c);
115 if (lockdata.lockorders.count(p1))
116 continue;
117 lockdata.lockorders[p1] = (*lockstack);
119 std::pair<void*, void*> p2 = std::make_pair(c, i.first);
120 lockdata.invlockorders.insert(p2);
121 if (lockdata.lockorders.count(p2))
122 potential_deadlock_detected(p1, lockdata.lockorders[p2], lockdata.lockorders[p1]);
126 static void pop_lock()
128 (*lockstack).pop_back();
131 void EnterCritical(const char* pszName, const char* pszFile, int nLine, void* cs, bool fTry)
133 push_lock(cs, CLockLocation(pszName, pszFile, nLine, fTry));
136 void LeaveCritical()
138 pop_lock();
141 std::string LocksHeld()
143 std::string result;
144 for (const std::pair<void*, CLockLocation> & i : *lockstack)
145 result += i.second.ToString() + std::string("\n");
146 return result;
149 void AssertLockHeldInternal(const char* pszName, const char* pszFile, int nLine, void* cs)
151 for (const std::pair<void*, CLockLocation> & i : *lockstack)
152 if (i.first == cs)
153 return;
154 fprintf(stderr, "Assertion failed: lock %s not held in %s:%i; locks held:\n%s", pszName, pszFile, nLine, LocksHeld().c_str());
155 abort();
158 void DeleteLock(void* cs)
160 if (!lockdata.available) {
161 // We're already shutting down.
162 return;
164 boost::unique_lock<boost::mutex> lock(lockdata.dd_mutex);
165 std::pair<void*, void*> item = std::make_pair(cs, nullptr);
166 LockOrders::iterator it = lockdata.lockorders.lower_bound(item);
167 while (it != lockdata.lockorders.end() && it->first.first == cs) {
168 std::pair<void*, void*> invitem = std::make_pair(it->first.second, it->first.first);
169 lockdata.invlockorders.erase(invitem);
170 lockdata.lockorders.erase(it++);
172 InvLockOrders::iterator invit = lockdata.invlockorders.lower_bound(item);
173 while (invit != lockdata.invlockorders.end() && invit->first == cs) {
174 std::pair<void*, void*> invinvitem = std::make_pair(invit->second, invit->first);
175 lockdata.lockorders.erase(invinvitem);
176 lockdata.invlockorders.erase(invit++);
180 #endif /* DEBUG_LOCKORDER */