#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}; } } } }