From c0ef12b6a449aef43d3a6a700a2c4041642bf88c Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Mon, 29 Dec 2025 11:37:13 +0100 Subject: kstd: clean up mutex implementation --- libs/kstd/src/mutex.cpp | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) (limited to 'libs/kstd/src/mutex.cpp') 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 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 -- cgit v1.2.3