diff options
| author | Felix Morgner <felix.morgner@ost.ch> | 2025-12-29 11:37:13 +0100 |
|---|---|---|
| committer | Felix Morgner <felix.morgner@ost.ch> | 2025-12-29 11:37:13 +0100 |
| commit | c0ef12b6a449aef43d3a6a700a2c4041642bf88c (patch) | |
| tree | 5911e441c3d249782e6a1266e3f9cd573bf24dfa /libs/kstd/src/mutex.cpp | |
| parent | 47209f32e5b68f2b1190afd9c409da7dcc369514 (diff) | |
| download | teachos-c0ef12b6a449aef43d3a6a700a2c4041642bf88c.tar.xz teachos-c0ef12b6a449aef43d3a6a700a2c4041642bf88c.zip | |
kstd: clean up mutex implementation
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 |
