aboutsummaryrefslogtreecommitdiff
path: root/kernel
diff options
context:
space:
mode:
Diffstat (limited to 'kernel')
-rw-r--r--kernel/kapi/tracked_mutex.cpp5
-rw-r--r--kernel/kapi/tracked_mutex.tests.cpp79
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