Unverified Commit 1575e160 authored by PhoeniX's avatar PhoeniX

Mutex refactoring

parent 25b35cd6
......@@ -9,6 +9,23 @@ class Mutex {
bool state;
public:
Mutex(): state(0) {}
void lock();
void release();
inline void lock() {
asm volatile(
"1:"
"testw $1, %0; jnz 1b;"
"lock btsw $0, %0; jc 1b;"
::"m"(state));
}
inline bool try_lock() {
bool ret = 0;
asm volatile(
"lock btsw $0, %1; jc 1f;"
"mov $1, %0;"
"1:"
:"=r"(ret):"m"(state));
return ret;
}
inline void release() {
asm volatile("movw $0, %0"::"m"(state));
}
};
// PhoeniX OS Kernel library mutex functions
// Copyright © 2017 Yury Popov a.k.a. PhoeniX
#include "kernlib.hpp"
void Mutex::lock() {
asm volatile(
"1:"
"mov $1, %%dl;"
"xor %%al, %%al;"
"lock cmpxchgb %%dl, %0;"
"jnz 1b"
::"m"(state)
:"rax","rdx");
}
void Mutex::release() {
asm volatile(
"xor %%al, %%al;"
"lock xchgb %%al, %0"
::"m"(state)
:"rax");
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment