blob: 782e6820bfdf54cfa0c1ecc07f84e6cde294f47f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
#pragma once
#include <atomic>
#include <cstdint>
#include <generic/arch.hpp>
namespace locks {
inline bool is_disabled = 0;
class spinlock {
private:
std::atomic_flag flag = ATOMIC_FLAG_INIT;
public:
void lock() {
if(is_disabled)
return;
while (flag.test_and_set(std::memory_order_acquire)) {
arch::pause();
}
}
void unlock() {
flag.clear(std::memory_order_release);
}
bool test() {
return flag.test();
}
bool try_lock() {
return !flag.test_and_set(std::memory_order_acquire);
}
};
};
|