blob: d5ae02f125fe7f793e673745868eff0b0b77d3d0 (
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
|
#include <types.h>
#include <atomic.h>
#include <libk/stdio.h>
#include <heap.h>
void init_mutex(mutex_t *mutex)
{
mutex->addr = (uint64_t *)kalloc(sizeof(uint64_t));
*(mutex->addr) = 0;
}
bool test_and_set(mutex_t mutex, bool value)
{
bool rax;
__asm__ __volatile__("lock xchg %%rax, (%%rbx);"
: "=a"(rax)
: "b"(mutex.addr), "a"(value));
return rax;
}
void lock(mutex_t mutex)
{
while (test_and_set(mutex, 1))
;
}
void unlock(mutex_t mutex)
{
test_and_set(mutex, 0);
}
|