diff options
author | Alexis211 <alexis211@gmail.com> | 2009-10-18 18:39:52 +0200 |
---|---|---|
committer | Alexis211 <alexis211@gmail.com> | 2009-10-18 18:39:52 +0200 |
commit | ccf807eb4ff541bb849c4f370d34123cb23d7d76 (patch) | |
tree | 7f97fcf3f83ef1efcdc0ae72e11aab1f8332a667 /Source/Library/Common/Mutex.class.cpp | |
parent | eb7b832d47bcbd74181028c62e871d407ba63a23 (diff) | |
download | Melon-ccf807eb4ff541bb849c4f370d34123cb23d7d76.tar.gz Melon-ccf807eb4ff541bb849c4f370d34123cb23d7d76.zip |
Heap included as well in userland library
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; +} |