diff options
| author | Felix Morgner <felix.morgner@ost.ch> | 2026-07-26 20:45:55 +0200 |
|---|---|---|
| committer | Felix Morgner <felix.morgner@ost.ch> | 2026-07-26 20:45:55 +0200 |
| commit | e7cb0a5dab291d453fa34c5d1250d85e82478ecf (patch) | |
| tree | d6c2c3bbb4bd87a3e82aa4b4478d43201c4ea49a | |
| parent | cf500b301ee3f518ea391ea21e39853b7e7a37c8 (diff) | |
| download | kernel-e7cb0a5dab291d453fa34c5d1250d85e82478ecf.tar.xz kernel-e7cb0a5dab291d453fa34c5d1250d85e82478ecf.zip | |
kapi: add tracked mutex tests
| -rw-r--r-- | kernel/kapi/tracked_mutex.cpp | 5 | ||||
| -rw-r--r-- | kernel/kapi/tracked_mutex.tests.cpp | 79 |
2 files changed, 79 insertions, 5 deletions
diff --git a/kernel/kapi/tracked_mutex.cpp b/kernel/kapi/tracked_mutex.cpp index 6608169c..b1796adc 100644 --- a/kernel/kapi/tracked_mutex.cpp +++ b/kernel/kapi/tracked_mutex.cpp @@ -33,11 +33,6 @@ namespace kapi return true; } - if (expected == self) - { - system::panic("[OS] CPU {} tried to reacquire a mutex it already holds!", self); - } - return false; } diff --git a/kernel/kapi/tracked_mutex.tests.cpp b/kernel/kapi/tracked_mutex.tests.cpp new file mode 100644 index 00000000..37493a1b --- /dev/null +++ b/kernel/kapi/tracked_mutex.tests.cpp @@ -0,0 +1,79 @@ +#include <kapi/tracked_mutex.hpp> + +#include <kernel/test_support/cpu.hpp> + +#include <kstd/mutex.hpp> + +#include <catch2/catch_test_macros.hpp> + +SCENARIO("Tracked mutex single threaded") +{ + GIVEN("a tracked mutex") + { + auto mutex = kapi::tracked_mutex{}; + + WHEN("the current thread hasn't locked the mutex") + { + THEN("unlocking the mutex panics") + { + REQUIRE_THROWS_AS(mutex.unlock(), kernel::tests::cpu::halt); + } + + THEN("locking the mutex succeeds") + { + mutex.lock(); + } + + THEN("locking the mutex using try_lock succeeds") + { + REQUIRE(mutex.try_lock()); + } + } + + WHEN("the current thread has locked the mutex") + { + mutex.lock(); + + THEN("unlocking the mutex succeeds") + { + mutex.unlock(); + } + + THEN("locking the mutex panics") + { + REQUIRE_THROWS_AS(mutex.lock(), kernel::tests::cpu::halt); + } + + THEN("locking the mutex using try_lock fails") + { + REQUIRE_FALSE(mutex.try_lock()); + } + } + } +} + +SCENARIO("Tracked mutex as a lockable for lock_guard") +{ + GIVEN("a tracked mutex") + { + auto mutex = kapi::tracked_mutex{}; + + WHEN("the current thread hasn't locked the mutex") + { + THEN("it can be locked using a lock_guard") + { + auto guard = kstd::lock_guard{mutex}; + } + } + + WHEN("the current thread has locked the mutex") + { + mutex.lock(); + + THEN("it can be adopted by a lock_guard") + { + auto guard = kstd::lock_guard{mutex, kstd::adopt_lock}; + } + } + } +}
\ No newline at end of file |
