aboutsummaryrefslogtreecommitdiff
path: root/src/threadsafety.h
diff options
context:
space:
mode:
authorAnthony Towns <aj@erisian.com.au>2020-02-11 16:50:22 +1000
committerAnthony Towns <aj@erisian.com.au>2020-05-27 01:31:51 +1000
commit5478d6c099e76fe070703cc5383cba7b91468b0f (patch)
treeec02170ef4e628dad3bbec5d7c1199671c5c4c23 /src/threadsafety.h
parente685ca19928eec4e687c66f5edfcfff085a42c27 (diff)
downloadbitcoin-5478d6c099e76fe070703cc5383cba7b91468b0f.tar.xz
logging: thread safety annotations
Adds LockGuard helper in threadsafety.h to replace lock_guard<mutex> when LOCK(Mutex) isn't available for use.
Diffstat (limited to 'src/threadsafety.h')
-rw-r--r--src/threadsafety.h11
1 files changed, 11 insertions, 0 deletions
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 <mutex>
+
#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<std::mutex>
+{
+public:
+ explicit LockGuard(std::mutex& cs) EXCLUSIVE_LOCK_FUNCTION(cs) : std::lock_guard<std::mutex>(cs) { }
+ ~LockGuard() UNLOCK_FUNCTION() {};
+};
+
#endif // BITCOIN_THREADSAFETY_H