blob: cabf83346345b4fde5993d4599e5184309c6d443 (
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
|
#include "kstd/mutex"
#include "kstd/os/error.hpp"
#include <atomic>
namespace kstd
{
[[gnu::section(".stl_text")]]
mutex::mutex() = default;
[[gnu::section(".stl_text")]]
mutex::~mutex()
{
if (m_locked.test(std::memory_order_relaxed))
{
os::panic("[KSTD] Tried to destroy a locked mutex.");
}
}
[[gnu::section(".stl_text")]]
auto mutex::lock() -> void
{
while (!try_lock())
{
asm volatile("nop");
}
}
[[gnu::section(".stl_text")]]
auto mutex::try_lock() -> bool
{
return !m_locked.test_and_set(std::memory_order_acquire);
}
[[gnu::section(".stl_text")]]
auto mutex::unlock() -> void
{
m_locked.clear(std::memory_order_release);
}
} // namespace kstd
|