summaryrefslogtreecommitdiff
path: root/kernel/src/generic/lock/spinlock.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/src/generic/lock/spinlock.hpp')
-rw-r--r--kernel/src/generic/lock/spinlock.hpp35
1 files changed, 35 insertions, 0 deletions
diff --git a/kernel/src/generic/lock/spinlock.hpp b/kernel/src/generic/lock/spinlock.hpp
index 782e682..2c2b2a6 100644
--- a/kernel/src/generic/lock/spinlock.hpp
+++ b/kernel/src/generic/lock/spinlock.hpp
@@ -6,6 +6,41 @@
namespace locks {
inline bool is_disabled = 0;
+
+ class preempt_spinlock {
+ private:
+ std::atomic_flag flag = ATOMIC_FLAG_INIT;
+ public:
+ bool lock() {
+ if(is_disabled)
+ return 0;
+
+ bool state = arch::test_interrupts();
+
+ arch::disable_interrupts();
+ while (flag.test_and_set(std::memory_order_acquire)) {
+ arch::pause();
+ }
+
+ return state;
+ }
+
+ void unlock(bool state) {
+ flag.clear(std::memory_order_release);
+
+ if(state)
+ arch::enable_interrupts();
+ }
+
+ bool test() {
+ return flag.test();
+ }
+
+ bool try_lock() {
+ return !flag.test_and_set(std::memory_order_acquire);
+ }
+ };
+
class spinlock {
private:
std::atomic_flag flag = ATOMIC_FLAG_INIT;