From 1c7277d1415cf9b80d6fe8ca4b9ff019223d7ee5 Mon Sep 17 00:00:00 2001 From: Hayodea Hekol Date: Wed, 5 Nov 2025 22:38:04 -0400 Subject: [PATCH] SpinLock: add RAII Guard class --- include/spinLock.h | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/include/spinLock.h b/include/spinLock.h index f3436d8..0d22055 100644 --- a/include/spinLock.h +++ b/include/spinLock.h @@ -72,6 +72,34 @@ public: locked.store(false); } + /** + * @brief RAII guard for SpinLock + * Locks the spinlock on construction and unlocks on destruction + */ + class Guard + { + public: + explicit Guard(SpinLock& lock) + : lock_(lock) + { + lock_.acquire(); + } + + ~Guard() + { + lock_.release(); + } + + // Non-copyable, non-movable + Guard(const Guard&) = delete; + Guard& operator=(const Guard&) = delete; + Guard(Guard&&) = delete; + Guard& operator=(Guard&&) = delete; + + private: + SpinLock& lock_; + }; + private: std::atomic locked; };