diff options
author | Alexis211 <alexis211@gmail.com> | 2009-10-20 19:23:33 +0200 |
---|---|---|
committer | Alexis211 <alexis211@gmail.com> | 2009-10-20 19:23:33 +0200 |
commit | 768ada13917aeda373e6ff5fee21faf90c963746 (patch) | |
tree | 9e26d7d65e1693d1a7f9fd93c9fd33b41d175464 /Source/Library/Common/Mutex.class.cpp | |
parent | 6ec4b3d31080f90393e72989d559cfb76eff6f9d (diff) | |
parent | 9836acd720988af30250c2c1ec18d618664dea4e (diff) | |
download | Melon-768ada13917aeda373e6ff5fee21faf90c963746.tar.gz Melon-768ada13917aeda373e6ff5fee21faf90c963746.zip |
Merge branch 'usermode_syscalls'
Conflicts:
Source/Kernel/Makefile
Diffstat (limited to 'Source/Library/Common/Mutex.class.cpp')
-rw-r--r-- | Source/Library/Common/Mutex.class.cpp | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/Source/Library/Common/Mutex.class.cpp b/Source/Library/Common/Mutex.class.cpp new file mode 100644 index 0000000..2e9a63c --- /dev/null +++ b/Source/Library/Common/Mutex.class.cpp @@ -0,0 +1,45 @@ +#include "Mutex.class.h" + +#ifdef THIS_IS_MELON_KERNEL +#include <TaskManager/Task.ns.h> +#endif + +#ifdef THIS_IS_MELON_USERLAND +#include <Binding/Thread.class.h> +#endif + +u32int atomic_exchange(u32int* ptr, u32int newval) { + u32int r; + asm volatile("xchg (%%ecx), %%eax" : "=a"(r) : "c"(ptr), "a"(newval)); + return r; +} + +Mutex::Mutex(u32int locked) { + m_locked = locked; +} + +bool Mutex::lock() { + if (atomic_exchange(&m_locked, MUTEX_TRUE) == MUTEX_TRUE) return false; //The lock was already locked + return true; +} + +void Mutex::waitLock() { + while (atomic_exchange(&m_locked, MUTEX_TRUE) == MUTEX_TRUE) { +#ifdef THIS_IS_MELON_KERNEL + if (Task::currThread() != 0) Task::currThread()->sleep(10); //Wait 10ms + else return; +#endif + +#ifdef THIS_IS_MELON_USERLAND + Thread::get().sleep(10); +#endif + } +} + +void Mutex::unlock() { + m_locked = MUTEX_FALSE; +} + +bool Mutex::locked() { + return m_locked; +} |