Add DEBUG_LOCKCONTENTION, to warn each time a thread waits to lock.
authorMatt Corallo <matt@bluematt.me>
Sat, 7 Jan 2012 17:12:39 +0000 (12:12 -0500)
committerMatt Corallo <matt@bluematt.me>
Sat, 21 Jan 2012 21:16:28 +0000 (16:16 -0500)
If compiled with -DDEBUG_LOCKCONTENTION, Bitcoin will print to
debug.log each time a thread has to wait for a lock to continue.

src/util.cpp

index 67e1bf8..6a4c2a2 100644 (file)
@@ -1153,7 +1153,18 @@ static void pop_lock()
 void CCriticalSection::Enter(const char* pszName, const char* pszFile, int nLine)
 {
     push_lock(this, CLockLocation(pszName, pszFile, nLine));
+#ifdef DEBUG_LOCKCONTENTION
+    bool result = mutex.try_lock();
+    if (!result)
+    {
+        printf("LOCKCONTENTION: %s\n", pszName);
+        printf("Locker: %s:%d\n", pszFile, nLine);
+        mutex.lock();
+        printf("Locked\n");
+    }
+#else
     mutex.lock();
+#endif
 }
 void CCriticalSection::Leave()
 {
@@ -1170,9 +1181,19 @@ bool CCriticalSection::TryEnter(const char* pszName, const char* pszFile, int nL
 
 #else
 
-void CCriticalSection::Enter(const char*, const char*, int)
+void CCriticalSection::Enter(const char* pszName, const char* pszFile, int nLine)
 {
+#ifdef DEBUG_LOCKCONTENTION
+    bool result = mutex.try_lock();
+    if (!result)
+    {
+        printf("LOCKCONTENTION: %s\n", pszName);
+        printf("Locker: %s:%d\n", pszFile, nLine);
+        mutex.lock();
+    }
+#else
     mutex.lock();
+#endif
 }
 
 void CCriticalSection::Leave()