From e7cb0a5dab291d453fa34c5d1250d85e82478ecf Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Sun, 26 Jul 2026 20:45:55 +0200 Subject: kapi: add tracked mutex tests --- kernel/kapi/tracked_mutex.cpp | 5 --- kernel/kapi/tracked_mutex.tests.cpp | 79 +++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 5 deletions(-) create mode 100644 kernel/kapi/tracked_mutex.tests.cpp (limited to 'kernel') 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 + +#include + +#include + +#include + +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 -- cgit v1.2.3