SpinLock: add RAII Guard class

This commit is contained in:
2025-11-05 22:38:04 -04:00
parent d29ebafea0
commit 1c7277d141
+28
View File
@@ -72,6 +72,34 @@ public:
locked.store(false); 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: private:
std::atomic<bool> locked; std::atomic<bool> locked;
}; };