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/include/kstd/mutex | 58 ++++++++++++-------------------------------- libs/kstd/src/mutex.cpp | 23 ++++++++++++++++-- 2 files changed, 36 insertions(+), 45 deletions(-) diff --git a/libs/kstd/include/kstd/mutex b/libs/kstd/include/kstd/mutex index 6ae3adf..ae91970 100644 --- a/libs/kstd/include/kstd/mutex +++ b/libs/kstd/include/kstd/mutex @@ -5,65 +5,37 @@ namespace kstd { - /** - * @brief Custom mutex implementation, that simply wraps an atomic boolean to keep track if the mutex is already in - * use by another thread or not. - */ + + //! A non-recursive mutex. struct mutex { - /** - * @brief Defaulted constructor. - */ - mutex() = default; - - /** - * @brief Defaulted destructor. - */ - ~mutex() = default; + mutex(); + ~mutex(); - /** - * @brief Deleted copy constructor. - */ mutex(mutex const &) = delete; - - /** - * @brief Deleted move constructor. - * - */ mutex(mutex &&) = delete; - /** - * @brief Deleted copy assignment operator. - */ auto operator=(mutex const &) -> mutex & = delete; - - /** - * @brief Deleted move assignment operator. - */ auto operator=(mutex &&) -> mutex & = delete; - /** - * @brief Lock the mutex (blocks for as long as it is not available). - */ - [[gnu::section(".stl_text")]] + //! Lock the mutex. + //! + //! @note This function blocks for as long as the mutex is not available. auto lock() -> void; - /** - * @brief Try to lock the mutex (non-blocking). - * - * @return True if lock has been acquired and false otherwise. - */ - [[gnu::section(".stl_text")]] + //! Try to lock the mutex. + //! + //! @note This function never blocks. + //! @return @p true iff. the mutex was successfully locked, @p false otherwise. auto try_lock() -> bool; - /** - * @brief Unlock the mutex. - */ - [[gnu::section(".stl_text")]] + //! Unlock the mutex. + //! + //! @note The behavior is undefined if the mutex is not currently held by the thread unlocking it. auto unlock() -> void; private: - std::atomic locked = {false}; // Atomic boolean to track if mutex is locked or not. + std::atomic_flag m_locked{}; }; } // namespace kstd 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