Locking: add SpinLock class
Nothing much to add: add a spinlock which has a tryAcquire method. This will be used as a primitive for building our spinQing locking system.
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
#ifndef SPIN_LOCK_H
|
||||
#define SPIN_LOCK_H
|
||||
|
||||
#include <atomic>
|
||||
|
||||
namespace smo {
|
||||
|
||||
/**
|
||||
* @brief Simple spinlock using std::atomic
|
||||
*/
|
||||
class SpinLock
|
||||
{
|
||||
public:
|
||||
SpinLock()
|
||||
: locked(false)
|
||||
{}
|
||||
|
||||
bool tryAcquire()
|
||||
{
|
||||
bool expected = false;
|
||||
return locked.compare_exchange_strong(expected, true);
|
||||
}
|
||||
|
||||
void release()
|
||||
{
|
||||
locked.store(false);
|
||||
}
|
||||
|
||||
private:
|
||||
std::atomic<bool> locked;
|
||||
};
|
||||
|
||||
} // namespace smo
|
||||
|
||||
#endif // SPIN_LOCK_H
|
||||
Reference in New Issue
Block a user