From 0733bb9a6809e33db8259ffaeb5a2839fd6cbe50 Mon Sep 17 00:00:00 2001 From: Hayodea Hakol Date: Wed, 17 Sep 2025 16:28:07 -0400 Subject: [PATCH] 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. --- include/spinLock.h | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 include/spinLock.h diff --git a/include/spinLock.h b/include/spinLock.h new file mode 100644 index 0000000..afda9a5 --- /dev/null +++ b/include/spinLock.h @@ -0,0 +1,35 @@ +#ifndef SPIN_LOCK_H +#define SPIN_LOCK_H + +#include + +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 locked; +}; + +} // namespace smo + +#endif // SPIN_LOCK_H