diff options
author | Vegard Storheil Eriksen <zyp@jvnv.net> | 2011-11-19 17:21:31 +0100 |
---|---|---|
committer | Vegard Storheil Eriksen <zyp@jvnv.net> | 2011-11-19 19:23:33 +0100 |
commit | 025a38a1f743fd9e89cbd477abe3f79a8d098097 (patch) | |
tree | a1f20c1b6a7f6c418bee641da923ab3d0d7208fe /os/mutex.h | |
parent | 9e5875f2908c1ce506e7c5712ccabc379f911360 (diff) |
Moved os and hal related files into subdirectories.
Diffstat (limited to 'os/mutex.h')
-rw-r--r-- | os/mutex.h | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/os/mutex.h b/os/mutex.h new file mode 100644 index 0000000..d12331d --- /dev/null +++ b/os/mutex.h @@ -0,0 +1,42 @@ +#ifndef MUTEX_H +#define MUTEX_H + +class Mutex { + private: + uint8_t locked; + public: + Mutex() : locked(0) {} + Mutex(uint8_t l) : locked(l) {} + + bool trylock() { + uint8_t val; + + // Check if mutex is locked. + asm volatile ("ldrexb %0, [%1]" : "=r" (val) : "r" (&locked)); + if(val) { + return false; + } + + // Try taking the lock. + asm volatile ("strexb %0, %1, [%2]" : "=r" (val) : "r" (1), "r" (&locked)); + if(val) { + return false; + } + + asm volatile("dmb"); + return true; + } + + void lock() { + while(!trylock()) { + Thread::yield(); + } + } + + void unlock() { + asm volatile("dmb"); + locked = 0; + } +}; + +#endif |