From 5478d6c099e76fe070703cc5383cba7b91468b0f Mon Sep 17 00:00:00 2001 From: Anthony Towns Date: Tue, 11 Feb 2020 16:50:22 +1000 Subject: logging: thread safety annotations Adds LockGuard helper in threadsafety.h to replace lock_guard when LOCK(Mutex) isn't available for use. --- src/threadsafety.h | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'src/threadsafety.h') diff --git a/src/threadsafety.h b/src/threadsafety.h index bb988dfdfd..81f86eac3a 100644 --- a/src/threadsafety.h +++ b/src/threadsafety.h @@ -6,6 +6,8 @@ #ifndef BITCOIN_THREADSAFETY_H #define BITCOIN_THREADSAFETY_H +#include + #ifdef __clang__ // TL;DR Add GUARDED_BY(mutex) to member variables. The others are // rarely necessary. Ex: int nFoo GUARDED_BY(cs_foo); @@ -54,4 +56,13 @@ #define ASSERT_EXCLUSIVE_LOCK(...) #endif // __GNUC__ +// LockGuard provides an annotated version of lock_guard for us +// should only be used when sync.h Mutex/LOCK/etc aren't usable +class SCOPED_LOCKABLE LockGuard : public std::lock_guard +{ +public: + explicit LockGuard(std::mutex& cs) EXCLUSIVE_LOCK_FUNCTION(cs) : std::lock_guard(cs) { } + ~LockGuard() UNLOCK_FUNCTION() {}; +}; + #endif // BITCOIN_THREADSAFETY_H -- cgit v1.2.3