diff options
Diffstat (limited to 'libs/kstd/src/mutex.cpp')
| -rw-r--r-- | libs/kstd/src/mutex.cpp | 23 |
1 files changed, 21 insertions, 2 deletions
diff --git a/libs/kstd/src/mutex.cpp b/libs/kstd/src/mutex.cpp index 5a26154..cabf833 100644 --- a/libs/kstd/src/mutex.cpp +++ b/libs/kstd/src/mutex.cpp @@ -1,9 +1,25 @@ #include "kstd/mutex" +#include "kstd/os/error.hpp" + #include <atomic> 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()) @@ -12,13 +28,16 @@ namespace kstd } } + [[gnu::section(".stl_text")]] auto mutex::try_lock() -> bool { - return !locked.exchange(true, std::memory_order_acquire); + return !m_locked.test_and_set(std::memory_order_acquire); } + [[gnu::section(".stl_text")]] auto mutex::unlock() -> void { - locked.store(false, std::memory_order_release); + m_locked.clear(std::memory_order_release); } + } // namespace kstd |
