sync.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // ECOin - Copyright (c) - 2014/2022 - GPLv3 - epsylon@riseup.net (https://03c8.net)
  2. #include "sync.h"
  3. #include "util.h"
  4. #include <boost/foreach.hpp>
  5. #ifdef DEBUG_LOCKCONTENTION
  6. void PrintLockContention(const char* pszName, const char* pszFile, int nLine)
  7. {
  8. printf("LOCKCONTENTION: %s\n", pszName);
  9. printf("Locker: %s:%d\n", pszFile, nLine);
  10. }
  11. #endif /* DEBUG_LOCKCONTENTION */
  12. #ifdef DEBUG_LOCKORDER
  13. struct CLockLocation
  14. {
  15. CLockLocation(const char* pszName, const char* pszFile, int nLine)
  16. {
  17. mutexName = pszName;
  18. sourceFile = pszFile;
  19. sourceLine = nLine;
  20. }
  21. std::string ToString() const
  22. {
  23. return mutexName+" "+sourceFile+":"+itostr(sourceLine);
  24. }
  25. private:
  26. std::string mutexName;
  27. std::string sourceFile;
  28. int sourceLine;
  29. };
  30. typedef std::vector< std::pair<void*, CLockLocation> > LockStack;
  31. static boost::mutex dd_mutex;
  32. static std::map<std::pair<void*, void*>, LockStack> lockorders;
  33. static boost::thread_specific_ptr<LockStack> lockstack;
  34. static void potential_deadlock_detected(const std::pair<void*, void*>& mismatch, const LockStack& s1, const LockStack& s2)
  35. {
  36. printf("POTENTIAL DEADLOCK DETECTED\n");
  37. printf("Previous lock order was:\n");
  38. BOOST_FOREACH(const PAIRTYPE(void*, CLockLocation)& i, s2)
  39. {
  40. if (i.first == mismatch.first) printf(" (1)");
  41. if (i.first == mismatch.second) printf(" (2)");
  42. printf(" %s\n", i.second.ToString().c_str());
  43. }
  44. printf("Current lock order is:\n");
  45. BOOST_FOREACH(const PAIRTYPE(void*, CLockLocation)& i, s1)
  46. {
  47. if (i.first == mismatch.first) printf(" (1)");
  48. if (i.first == mismatch.second) printf(" (2)");
  49. printf(" %s\n", i.second.ToString().c_str());
  50. }
  51. }
  52. static void push_lock(void* c, const CLockLocation& locklocation, bool fTry)
  53. {
  54. if (lockstack.get() == NULL)
  55. lockstack.reset(new LockStack);
  56. if (fDebug) printf("Locking: %s\n", locklocation.ToString().c_str());
  57. dd_mutex.lock();
  58. (*lockstack).push_back(std::make_pair(c, locklocation));
  59. if (!fTry) {
  60. BOOST_FOREACH(const PAIRTYPE(void*, CLockLocation)& i, (*lockstack)) {
  61. if (i.first == c) break;
  62. std::pair<void*, void*> p1 = std::make_pair(i.first, c);
  63. if (lockorders.count(p1))
  64. continue;
  65. lockorders[p1] = (*lockstack);
  66. std::pair<void*, void*> p2 = std::make_pair(c, i.first);
  67. if (lockorders.count(p2))
  68. {
  69. potential_deadlock_detected(p1, lockorders[p2], lockorders[p1]);
  70. break;
  71. }
  72. }
  73. }
  74. dd_mutex.unlock();
  75. }
  76. static void pop_lock()
  77. {
  78. if (fDebug)
  79. {
  80. const CLockLocation& locklocation = (*lockstack).rbegin()->second;
  81. printf("Unlocked: %s\n", locklocation.ToString().c_str());
  82. }
  83. dd_mutex.lock();
  84. (*lockstack).pop_back();
  85. dd_mutex.unlock();
  86. }
  87. void EnterCritical(const char* pszName, const char* pszFile, int nLine, void* cs, bool fTry)
  88. {
  89. push_lock(cs, CLockLocation(pszName, pszFile, nLine), fTry);
  90. }
  91. void LeaveCritical()
  92. {
  93. pop_lock();
  94. }
  95. #endif /* DEBUG_LOCKORDER */