Permalink
Browse files

Add a warning if we upgrade directly from shared->exclusive lock.

  • Loading branch information...
1 parent b3226ff commit d3d8bf6f924678961c5f8200757f1deaa8630981 @TheBlueMatt committed Jul 3, 2012
Showing with 22 additions and 0 deletions.
  1. +16 −0 src/sync.cpp
  2. +6 −0 src/sync.h
View
@@ -128,6 +128,14 @@ void LeaveCritical()
#endif /* DEBUG_LOCKORDER */
+void CheckLockUpgrade(const char* pszFile, int nLine, CCriticalSection& cs)
+{
+ if (cs.has_shared())
+ printf("WARNING: Upgrading directly from shared to exclusive mutex, something is wrong here: %s:%d\n", pszFile, nLine);
+
+}
+
+
static void do_nothing (int* pBool) {}
CCriticalSection::CCriticalSection() : nHasExclusive(&do_nothing),
@@ -357,6 +365,11 @@ void CCriticalSection::unlock_shared()
nHasShared.reset((int*) 0);
}
+bool CCriticalSection::has_shared()
+{
+ return nHasShared.get() > (int*)0;
+}
+
@@ -370,6 +383,7 @@ void CCriticalBlock::Enter(const char* pszName, const char* pszFile, int nLine)
switch (lockType)
{
case UNIQUE:
+ CheckLockUpgrade(pszFile, nLine, *pmutex);
fLocked = pmutex->try_lock();
break;
case UPGRADE:
@@ -387,6 +401,7 @@ void CCriticalBlock::Enter(const char* pszName, const char* pszFile, int nLine)
switch (lockType)
{
case UNIQUE:
+ CheckLockUpgrade(pszFile, nLine, *pmutex);
pmutex->lock();
break;
case UPGRADE:
@@ -431,6 +446,7 @@ bool CCriticalBlock::TryEnter(const char* pszName, const char* pszFile, int nLin
switch (lockType)
{
case UNIQUE:
+ CheckLockUpgrade(pszFile, nLine, *pmutex);
fOwnsLock = pmutex->try_lock();
break;
case UPGRADE:
View
@@ -36,6 +36,9 @@ class CCriticalSection
void lock_shared();
bool try_lock_shared();
void unlock_shared();
+
+ // Temporary function to allow useful warnings when upgrading shared->exclusive
+ bool has_shared();
};
/** RAII wrapper around CCriticalSection */
@@ -90,9 +93,12 @@ void static inline EnterCritical(const char* pszName, const char* pszFile, int n
void static inline LeaveCritical() {}
#endif
+void CheckLockUpgrade(const char* pszfile, int nLine, CCriticalSection& cs);
+
#define ENTER_CRITICAL_SECTION(cs) \
{ \
EnterCritical(#cs, __FILE__, __LINE__, (void*)(&cs)); \
+ CheckLockUpgrade(__FILE__, __LINE__, cs); \
(cs).lock(); \
}

0 comments on commit d3d8bf6

Please sign in to comment.