aboutsummaryrefslogtreecommitdiff
path: root/src/sync.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/sync.h')
-rw-r--r--src/sync.h24
1 files changed, 24 insertions, 0 deletions
diff --git a/src/sync.h b/src/sync.h
index 3857eda56b..bdbdde1a2a 100644
--- a/src/sync.h
+++ b/src/sync.h
@@ -198,6 +198,16 @@ using DebugLock = UniqueLock<typename std::remove_reference<typename std::remove
LeaveCritical(); \
}
+//! Run code while locking a mutex.
+//!
+//! Examples:
+//!
+//! WITH_LOCK(cs, shared_val = shared_val + 1);
+//!
+//! int val = WITH_LOCK(cs, return shared_val);
+//!
+#define WITH_LOCK(cs, code) [&] { LOCK(cs); code; }()
+
class CSemaphore
{
private:
@@ -294,4 +304,18 @@ public:
}
};
+// Utility class for indicating to compiler thread analysis that a mutex is
+// locked (when it couldn't be determined otherwise).
+struct SCOPED_LOCKABLE LockAssertion
+{
+ template <typename Mutex>
+ explicit LockAssertion(Mutex& mutex) EXCLUSIVE_LOCK_FUNCTION(mutex)
+ {
+#ifdef DEBUG_LOCKORDER
+ AssertLockHeld(mutex);
+#endif
+ }
+ ~LockAssertion() UNLOCK_FUNCTION() {}
+};
+
#endif // BITCOIN_SYNC_H