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; };