Skip to content

Commit 79be487

Browse files
hebastoajtowns
andcommitted
Add thread safety annotated wrapper for std::mutex
Co-authored-by: Anthony Towns <[email protected]>
1 parent 55b4c65 commit 79be487

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

src/logging.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ namespace BCLog {
6262
class Logger
6363
{
6464
private:
65-
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
65+
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
6666

6767
FILE* m_fileout GUARDED_BY(m_cs) = nullptr;
6868
std::list<std::string> m_msgs_before_open GUARDED_BY(m_cs);

src/threadsafety.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,18 @@
5656
#define ASSERT_EXCLUSIVE_LOCK(...)
5757
#endif // __GNUC__
5858

59+
// StdMutex provides an annotated version of std::mutex for us,
60+
// and should only be used when sync.h Mutex/LOCK/etc are not usable.
61+
class LOCKABLE StdMutex : public std::mutex
62+
{
63+
};
64+
5965
// LockGuard provides an annotated version of lock_guard for us
6066
// should only be used when sync.h Mutex/LOCK/etc aren't usable
61-
class SCOPED_LOCKABLE LockGuard : public std::lock_guard<std::mutex>
67+
class SCOPED_LOCKABLE LockGuard : public std::lock_guard<StdMutex>
6268
{
6369
public:
64-
explicit LockGuard(std::mutex& cs) EXCLUSIVE_LOCK_FUNCTION(cs) : std::lock_guard<std::mutex>(cs) { }
70+
explicit LockGuard(StdMutex& cs) EXCLUSIVE_LOCK_FUNCTION(cs) : std::lock_guard<StdMutex>(cs) {}
6571
~LockGuard() UNLOCK_FUNCTION() {};
6672
};
6773

0 commit comments

Comments
 (0)