SpinLock: add RAII Guard class
This commit is contained in:
@@ -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<bool> locked;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user