]> Git Repo - VerusCoin.git/blob - src/sync.cpp
Merge pull request #5481
[VerusCoin.git] / src / sync.cpp
1 // Copyright (c) 2011-2012 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.
4
5 #include "sync.h"
6
7 #include "util.h"
8 #include "utilstrencodings.h"
9
10 #include <stdio.h>
11
12 #include <boost/foreach.hpp>
13 #include <boost/thread.hpp>
14
15 #ifdef DEBUG_LOCKCONTENTION
16 void PrintLockContention(const char* pszName, const char* pszFile, int nLine)
17 {
18     LogPrintf("LOCKCONTENTION: %s\n", pszName);
19     LogPrintf("Locker: %s:%d\n", pszFile, nLine);
20 }
21 #endif /* DEBUG_LOCKCONTENTION */
22
23 #ifdef DEBUG_LOCKORDER
24 //
25 // Early deadlock detection.
26 // Problem being solved:
27 //    Thread 1 locks  A, then B, then C
28 //    Thread 2 locks  D, then C, then A
29 //     --> may result in deadlock between the two threads, depending on when they run.
30 // Solution implemented here:
31 // Keep track of pairs of locks: (A before B), (A before C), etc.
32 // Complain if any thread tries to lock in a different order.
33 //
34
35 struct CLockLocation {
36     CLockLocation(const char* pszName, const char* pszFile, int nLine)
37     {
38         mutexName = pszName;
39         sourceFile = pszFile;
40         sourceLine = nLine;
41     }
42
43     std::string ToString() const
44     {
45         return mutexName + "  " + sourceFile + ":" + itostr(sourceLine);
46     }
47
48     std::string MutexName() const { return mutexName; }
49
50 private:
51     std::string mutexName;
52     std::string sourceFile;
53     int sourceLine;
54 };
55
56 typedef std::vector<std::pair<void*, CLockLocation> > LockStack;
57
58 static boost::mutex dd_mutex;
59 static std::map<std::pair<void*, void*>, LockStack> lockorders;
60 static boost::thread_specific_ptr<LockStack> lockstack;
61
62
63 static void potential_deadlock_detected(const std::pair<void*, void*>& mismatch, const LockStack& s1, const LockStack& s2)
64 {
65     LogPrintf("POTENTIAL DEADLOCK DETECTED\n");
66     LogPrintf("Previous lock order was:\n");
67     BOOST_FOREACH (const PAIRTYPE(void*, CLockLocation) & i, s2) {
68         if (i.first == mismatch.first)
69             LogPrintf(" (1)");
70         if (i.first == mismatch.second)
71             LogPrintf(" (2)");
72         LogPrintf(" %s\n", i.second.ToString());
73     }
74     LogPrintf("Current lock order is:\n");
75     BOOST_FOREACH (const PAIRTYPE(void*, CLockLocation) & i, s1) {
76         if (i.first == mismatch.first)
77             LogPrintf(" (1)");
78         if (i.first == mismatch.second)
79             LogPrintf(" (2)");
80         LogPrintf(" %s\n", i.second.ToString());
81     }
82 }
83
84 static void push_lock(void* c, const CLockLocation& locklocation, bool fTry)
85 {
86     if (lockstack.get() == NULL)
87         lockstack.reset(new LockStack);
88
89     LogPrint("lock", "Locking: %s\n", locklocation.ToString());
90     dd_mutex.lock();
91
92     (*lockstack).push_back(std::make_pair(c, locklocation));
93
94     if (!fTry) {
95         BOOST_FOREACH (const PAIRTYPE(void*, CLockLocation) & i, (*lockstack)) {
96             if (i.first == c)
97                 break;
98
99             std::pair<void*, void*> p1 = std::make_pair(i.first, c);
100             if (lockorders.count(p1))
101                 continue;
102             lockorders[p1] = (*lockstack);
103
104             std::pair<void*, void*> p2 = std::make_pair(c, i.first);
105             if (lockorders.count(p2)) {
106                 potential_deadlock_detected(p1, lockorders[p2], lockorders[p1]);
107                 break;
108             }
109         }
110     }
111     dd_mutex.unlock();
112 }
113
114 static void pop_lock()
115 {
116     if (fDebug) {
117         const CLockLocation& locklocation = (*lockstack).rbegin()->second;
118         LogPrint("lock", "Unlocked: %s\n", locklocation.ToString());
119     }
120     dd_mutex.lock();
121     (*lockstack).pop_back();
122     dd_mutex.unlock();
123 }
124
125 void EnterCritical(const char* pszName, const char* pszFile, int nLine, void* cs, bool fTry)
126 {
127     push_lock(cs, CLockLocation(pszName, pszFile, nLine), fTry);
128 }
129
130 void LeaveCritical()
131 {
132     pop_lock();
133 }
134
135 std::string LocksHeld()
136 {
137     std::string result;
138     BOOST_FOREACH (const PAIRTYPE(void*, CLockLocation) & i, *lockstack)
139         result += i.second.ToString() + std::string("\n");
140     return result;
141 }
142
143 void AssertLockHeldInternal(const char* pszName, const char* pszFile, int nLine, void* cs)
144 {
145     BOOST_FOREACH (const PAIRTYPE(void*, CLockLocation) & i, *lockstack)
146         if (i.first == cs)
147             return;
148     fprintf(stderr, "Assertion failed: lock %s not held in %s:%i; locks held:\n%s", pszName, pszFile, nLine, LocksHeld().c_str());
149     abort();
150 }
151
152 #endif /* DEBUG_LOCKORDER */
This page took 0.030961 seconds and 4 git commands to generate.