36 lines
847 B
C++
36 lines
847 B
C++
/*
|
|
* Spinlock.cpp
|
|
* C++ Spinlock
|
|
* Copyright (c) 2025 Daniel Hammer
|
|
*/
|
|
|
|
#include "Spinlock.hpp"
|
|
|
|
namespace kcp {
|
|
uint64_t Spinlock::AcquireIrqSave() {
|
|
uint64_t flags;
|
|
asm volatile("pushfq; pop %0; cli" : "=r"(flags) :: "memory");
|
|
while (atomic_flag.test_and_set(std::memory_order_acquire)) {
|
|
asm volatile("pause");
|
|
}
|
|
return flags;
|
|
}
|
|
|
|
void Spinlock::ReleaseIrqRestore(uint64_t flags) {
|
|
atomic_flag.clear(std::memory_order_release);
|
|
asm volatile("push %0; popfq" :: "r"(flags) : "memory");
|
|
}
|
|
|
|
void Spinlock::ReleaseIrqNoRestore() {
|
|
atomic_flag.clear(std::memory_order_release);
|
|
}
|
|
|
|
void Spinlock::Acquire() {
|
|
savedFlags = AcquireIrqSave();
|
|
}
|
|
|
|
void Spinlock::Release() {
|
|
ReleaseIrqRestore(savedFlags);
|
|
}
|
|
};
|