The Pedigree Project 0.1
SpinlockWord.h
1#ifndef KERNEL_SPINLOCKWORD_H
2#define KERNEL_SPINLOCKWORD_H
3
4#include "pedigree/kernel/compiler.h"
5#include "pedigree/kernel/processor/types.h"
6
9 public:
10 explicit constexpr SpinlockWord(bool locked = false) : m_Value(locked ? 0 : 1) {}
11
12 ALWAYS_INLINE bool tryAcquire() {
13 processor_register_t expected = 1;
14 return __atomic_compare_exchange_n(&m_Value, &expected, 0, false, __ATOMIC_ACQUIRE,
15 __ATOMIC_RELAXED);
16 }
17
18 ALWAYS_INLINE bool acquired() const {
19 return __atomic_load_n(&m_Value, __ATOMIC_RELAXED) == 0;
20 }
21
22 ALWAYS_INLINE void release() {
23 __atomic_store_n(&m_Value, 1, __ATOMIC_RELEASE);
24 }
25
26 ALWAYS_INLINE bool releaseChecked() {
27 processor_register_t expected = 0;
28 return __atomic_compare_exchange_n(&m_Value, &expected, 1, false, __ATOMIC_RELEASE,
29 __ATOMIC_RELAXED);
30 }
31
32 private:
33 friend class Spinlock;
34 NOT_COPYABLE_OR_ASSIGNABLE(SpinlockWord);
35
36 // Context-switch assembly publishes 1 through this machine-word pointer.
37 volatile processor_register_t m_Value;
38};
39
40#endif