42 lines
736 B
C++
42 lines
736 B
C++
|
#pragma once
|
||
|
|
||
|
#include <twist/stdlike/atomic.hpp>
|
||
|
#include <twist/util/spin_wait.hpp>
|
||
|
#include <wheels/logging/logging.hpp>
|
||
|
#include <twist/util/spin_wait.hpp>
|
||
|
|
||
|
namespace exe::support {
|
||
|
|
||
|
// Test-and-TAS spinlock
|
||
|
|
||
|
class SpinLock {
|
||
|
public:
|
||
|
void Lock() {
|
||
|
while (is_locked_.exchange(true, std::memory_order::acquire)) {
|
||
|
twist::util::SpinWait s;
|
||
|
while (is_locked_.load(std::memory_order::relaxed)) {
|
||
|
s.Spin();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void Unlock() {
|
||
|
is_locked_.store(false, std::memory_order::release);
|
||
|
}
|
||
|
|
||
|
// BasicLockable
|
||
|
|
||
|
void lock() { // NOLINT
|
||
|
Lock();
|
||
|
}
|
||
|
|
||
|
void unlock() { // NOLINT
|
||
|
Unlock();
|
||
|
}
|
||
|
|
||
|
private:
|
||
|
twist::stdlike::atomic<bool> is_locked_{false};
|
||
|
};
|
||
|
|
||
|
} // namespace exe::support
|