From 79be4874209f71ba6428a80c40c9f028ac936c41 Mon Sep 17 00:00:00 2001 From: Hennadii Stepanov <32963518+hebasto@users.noreply.github.com> Date: Thu, 28 May 2020 09:32:21 +0300 Subject: Add thread safety annotated wrapper for std::mutex Co-authored-by: Anthony Towns --- src/logging.h | 2 +- src/threadsafety.h | 10 ++++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/logging.h b/src/logging.h index c55f581916..2bd8f2683c 100644 --- a/src/logging.h +++ b/src/logging.h @@ -62,7 +62,7 @@ namespace BCLog { class Logger { private: - mutable std::mutex m_cs; // Can not use Mutex from sync.h because in debug mode it would cause a deadlock when a potential deadlock was detected + mutable StdMutex m_cs; // Can not use Mutex from sync.h because in debug mode it would cause a deadlock when a potential deadlock was detected FILE* m_fileout GUARDED_BY(m_cs) = nullptr; std::list m_msgs_before_open GUARDED_BY(m_cs); diff --git a/src/threadsafety.h b/src/threadsafety.h index 81f86eac3a..404ecafebb 100644 --- a/src/threadsafety.h +++ b/src/threadsafety.h @@ -56,12 +56,18 @@ #define ASSERT_EXCLUSIVE_LOCK(...) #endif // __GNUC__ +// StdMutex provides an annotated version of std::mutex for us, +// and should only be used when sync.h Mutex/LOCK/etc are not usable. +class LOCKABLE StdMutex : public std::mutex +{ +}; + // 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 +class SCOPED_LOCKABLE LockGuard : public std::lock_guard { public: - explicit LockGuard(std::mutex& cs) EXCLUSIVE_LOCK_FUNCTION(cs) : std::lock_guard(cs) { } + explicit LockGuard(StdMutex& cs) EXCLUSIVE_LOCK_FUNCTION(cs) : std::lock_guard(cs) {} ~LockGuard() UNLOCK_FUNCTION() {}; }; -- cgit v1.2.3