Fabcoin Core  0.16.2
P2P Digital Currency
sync.cpp
Go to the documentation of this file.
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.
4 
5 #include <sync.h>
6 
7 #include <util.h>
8 #include <utilstrencodings.h>
9 
10 #include <stdio.h>
11 
12 #include <boost/thread.hpp>
13 
14 #ifdef DEBUG_LOCKCONTENTION
15 void PrintLockContention(const char* pszName, const char* pszFile, int nLine)
16 {
17  LogPrintf("LOCKCONTENTION: %s\n", pszName);
18  LogPrintf("Locker: %s:%d\n", pszFile, nLine);
19 }
20 #endif /* DEBUG_LOCKCONTENTION */
21 
22 #ifdef DEBUG_LOCKORDER
23 //
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.
32 //
33 
34 struct CLockLocation {
35  CLockLocation(const char* pszName, const char* pszFile, int nLine, bool fTryIn)
36  {
37  mutexName = pszName;
38  sourceFile = pszFile;
39  sourceLine = nLine;
40  fTry = fTryIn;
41  }
42 
43  std::string ToString() const
44  {
45  return mutexName + " " + sourceFile + ":" + itostr(sourceLine) + (fTry ? " (TRY)" : "");
46  }
47 
48  bool fTry;
49 private:
50  std::string mutexName;
51  std::string sourceFile;
52  int sourceLine;
53 };
54 
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;
58 
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; }
67 
68  LockOrders lockorders;
69  InvLockOrders invlockorders;
70  boost::mutex dd_mutex;
71 } static lockdata;
72 
73 boost::thread_specific_ptr<LockStack> lockstack;
74 
75 static void potential_deadlock_detected(const std::pair<void*, void*>& mismatch, const LockStack& s1, const LockStack& s2)
76 {
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)");
82  }
83  if (i.first == mismatch.second) {
84  LogPrintf(" (2)");
85  }
86  LogPrintf(" %s\n", i.second.ToString());
87  }
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)");
92  }
93  if (i.first == mismatch.second) {
94  LogPrintf(" (2)");
95  }
96  LogPrintf(" %s\n", i.second.ToString());
97  }
98  assert(false);
99 }
100 
101 static void push_lock(void* c, const CLockLocation& locklocation, bool fTry)
102 {
103  if (lockstack.get() == nullptr)
104  lockstack.reset(new LockStack);
105 
106  boost::unique_lock<boost::mutex> lock(lockdata.dd_mutex);
107 
108  (*lockstack).push_back(std::make_pair(c, locklocation));
109 
110  for (const std::pair<void*, CLockLocation> & i : (*lockstack)) {
111  if (i.first == c)
112  break;
113 
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);
118 
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]);
123  }
124 }
125 
126 static void pop_lock()
127 {
128  (*lockstack).pop_back();
129 }
130 
131 void EnterCritical(const char* pszName, const char* pszFile, int nLine, void* cs, bool fTry)
132 {
133  push_lock(cs, CLockLocation(pszName, pszFile, nLine, fTry), fTry);
134 }
135 
136 void LeaveCritical()
137 {
138  pop_lock();
139 }
140 
141 std::string LocksHeld()
142 {
143  std::string result;
144  for (const std::pair<void*, CLockLocation> & i : *lockstack)
145  result += i.second.ToString() + std::string("\n");
146  return result;
147 }
148 
149 void AssertLockHeldInternal(const char* pszName, const char* pszFile, int nLine, void* cs)
150 {
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();
156 }
157 
158 void DeleteLock(void* cs)
159 {
160  if (!lockdata.available) {
161  // We're already shutting down.
162  return;
163  }
164  boost::unique_lock<boost::mutex> lock(lockdata.dd_mutex);
165  std::pair<void*, void*> item = std::make_pair(cs, (void*)0);
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++);
171  }
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++);
177  }
178 }
179 
180 #endif /* DEBUG_LOCKORDER */
static const std::string s2("AAD")
#define c(i)
assert(len-trim+(2 *lenIndices)<=WIDTH)
#define LogPrintf(...)
Definition: util.h:153
#define s1(x)
Definition: sha256.c:70
std::string itostr(int n)