Skip to content

Commit

Permalink
locking: teach Clang's -Wthread-safety to cope with our scoped lock m…
Browse files Browse the repository at this point in the history
…acros

This allows us to use function/variable/class attributes to specify locking
requisites, allowing problems to be detected during static analysis.

This works perfectly with newer Clang versions (tested with 3.3-3.7). For older
versions (tested 3.2), it compiles fine but spews lots of false-positives.
  • Loading branch information
theuni authored and str4d committed Mar 3, 2017
1 parent 8e4bc69 commit 5093299
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/sync.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ void PrintLockContention(const char* pszName, const char* pszFile, int nLine);

/** Wrapper around boost::unique_lock<Mutex> */
template <typename Mutex>
class CMutexLock
class SCOPED_LOCKABLE CMutexLock
{
private:
boost::unique_lock<Mutex> lock;
Expand Down Expand Up @@ -129,15 +129,15 @@ class CMutexLock
}

public:
CMutexLock(Mutex& mutexIn, const char* pszName, const char* pszFile, int nLine, bool fTry = false) : lock(mutexIn, boost::defer_lock)
CMutexLock(Mutex& mutexIn, const char* pszName, const char* pszFile, int nLine, bool fTry = false) EXCLUSIVE_LOCK_FUNCTION(mutexIn) : lock(mutexIn, boost::defer_lock)
{
if (fTry)
TryEnter(pszName, pszFile, nLine);
else
Enter(pszName, pszFile, nLine);
}

CMutexLock(Mutex* pmutexIn, const char* pszName, const char* pszFile, int nLine, bool fTry = false)
CMutexLock(Mutex* pmutexIn, const char* pszName, const char* pszFile, int nLine, bool fTry = false) EXCLUSIVE_LOCK_FUNCTION(pmutexIn)
{
if (!pmutexIn) return;

Expand All @@ -148,7 +148,7 @@ class CMutexLock
Enter(pszName, pszFile, nLine);
}

~CMutexLock()
~CMutexLock() UNLOCK_FUNCTION()
{
if (lock.owns_lock())
LeaveCritical();
Expand Down

0 comments on commit 5093299

Please sign in to comment.