blob: 6608169c4088ca4a4e8b8615dab4ad5226d5c78c (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
#include <kapi/tracked_mutex.hpp>
#include <kapi/cpu.hpp>
#include <kapi/system.hpp>
#include <atomic>
namespace kapi
{
auto tracked_mutex::lock() -> void
{
auto self = kapi::cpu::current_id();
auto expected = kapi::cpu::invalid_id;
while (!m_owner.compare_exchange_weak(expected, self, std::memory_order::acquire, std::memory_order::relaxed))
{
if (expected == self)
{
system::panic("[OS] CPU {} tried to reacquire a mutex it already holds!", self);
}
expected = kapi::cpu::invalid_id;
}
}
auto tracked_mutex::try_lock() -> bool
{
auto self = kapi::cpu::current_id();
auto expected = kapi::cpu::invalid_id;
if (m_owner.compare_exchange_strong(expected, self, std::memory_order::acquire, std::memory_order::relaxed))
{
return true;
}
if (expected == self)
{
system::panic("[OS] CPU {} tried to reacquire a mutex it already holds!", self);
}
return false;
}
auto tracked_mutex::unlock() -> void
{
auto self = kapi::cpu::current_id();
auto expected = self;
if (!m_owner.compare_exchange_strong(expected, kapi::cpu::invalid_id, std::memory_order::release))
{
system::panic("[OS] CPU {} released a mutex it did not hold!", self);
}
}
} // namespace kapi
|