diff options
author | Alexis211 <alexis211@gmail.com> | 2010-03-31 17:26:21 +0200 |
---|---|---|
committer | Alexis211 <alexis211@gmail.com> | 2010-03-31 17:26:21 +0200 |
commit | b945eafa126d6a17aa8826a405df7d5d4d999008 (patch) | |
tree | 34b5ba6f0c13b1b35770375f51183a047271a4d4 /src/library/mutex.c | |
parent | ad1ec29070e1ffba7461687cd268e64be06aa78b (diff) | |
download | TCE-b945eafa126d6a17aa8826a405df7d5d4d999008.tar.gz TCE-b945eafa126d6a17aa8826a405df7d5d4d999008.zip |
Shared memory segment manager in userland
Diffstat (limited to 'src/library/mutex.c')
-rw-r--r-- | src/library/mutex.c | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/src/library/mutex.c b/src/library/mutex.c new file mode 100644 index 0000000..ac0ee8f --- /dev/null +++ b/src/library/mutex.c @@ -0,0 +1,22 @@ +#include <mutex.h> + +static uint32_t atomic_exchange(uint32_t* ptr, uint32_t newval) { + uint32_t r; + asm volatile("xchg (%%ecx), %%eax" : "=a"(r) : "c"(ptr), "a"(newval)); + return r; +} + +void mutex_lock(uint32_t* mutex) { + while (atomic_exchange(mutex, MUTEX_LOCKED) == MUTEX_LOCKED) { + thread_sleep(1); + } +} + +int mutex_lockE(uint32_t* mutex) { + if (atomic_exchange(mutex, MUTEX_LOCKED) == MUTEX_LOCKED) return 0; + return 1; +} + +void mutex_unlock(uint32_t* mutex) { + *mutex = MUTEX_UNLOCKED; +} |