#include "kstd/mutex" #include "kstd/os/error.hpp" #include namespace kstd { [[gnu::section(".stl_text")]] mutex::mutex() = default; [[gnu::section(".stl_text")]] mutex::~mutex() { if (m_locked.test(std::memory_order_relaxed)) { os::panic("[KSTD] Tried to destroy a locked mutex."); } } [[gnu::section(".stl_text")]] auto mutex::lock() -> void { while (!try_lock()) { asm volatile("nop"); } } [[gnu::section(".stl_text")]] auto mutex::try_lock() -> bool { return !m_locked.test_and_set(std::memory_order_acquire); } [[gnu::section(".stl_text")]] auto mutex::unlock() -> void { m_locked.clear(std::memory_order_release); } } // namespace kstd