CoreMutex cleanup and add to build

This commit is contained in:
Ali Mashtizadeh 2015-01-30 20:44:01 -08:00
parent 8b45f56bf5
commit bdb6a08704
3 changed files with 15 additions and 11 deletions

View File

@ -2,14 +2,14 @@
#ifndef __CORE_MUTEX_H__ #ifndef __CORE_MUTEX_H__
#define __CORE_MUTEX_H__ #define __CORE_MUTEX_H__
typedef struct Mutex { typedef struct CoreMutex {
uint64_t lock; uint64_t lock;
} Mutex; } CoreMutex;
int CoreMutex_Init(Mutex *mtx); void CoreMutex_Init(CoreMutex *mtx);
void CoreMutex_Lock(Mutex *mtx); void CoreMutex_Lock(CoreMutex *mtx);
bool CoreMutex_TryLock(Mutex *mtx); bool CoreMutex_TryLock(CoreMutex *mtx);
void CoreMutex_Unlock(Mutex *mtx); void CoreMutex_Unlock(CoreMutex *mtx);
#endif /* __CORE_MUTEX_H__ */ #endif /* __CORE_MUTEX_H__ */

View File

@ -9,6 +9,7 @@ src = [ ]
src_common = [ src_common = [
"abort.c", "abort.c",
"assert.c", "assert.c",
"core/mutex.c",
"dir.c", "dir.c",
"exit.c", "exit.c",
"file.c", "file.c",

View File

@ -2,23 +2,26 @@
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
#include <syscall.h>
#include <core/mutex.h> #include <core/mutex.h>
int void
CoreMutex_Init(Mutex *mtx) CoreMutex_Init(CoreMutex *mtx)
{ {
mtx->lock = 0;
} }
void void
CoreMutex_Lock(Mutex *mtx) CoreMutex_Lock(CoreMutex *mtx)
{ {
while (__sync_lock_test_and_set(&mtx->lock, 1) == 1) { while (__sync_lock_test_and_set(&mtx->lock, 1) == 1) {
OSThreadSleep(0);
} }
} }
bool bool
CoreMutex_TryLock(Mutex *mtx) CoreMutex_TryLock(CoreMutex *mtx)
{ {
if (__sync_lock_test_and_set(&mtx->lock, 1) == 1) { if (__sync_lock_test_and_set(&mtx->lock, 1) == 1) {
return false; return false;
@ -28,7 +31,7 @@ CoreMutex_TryLock(Mutex *mtx)
} }
void void
CoreMutex_Unlock(Mutex *mtx) CoreMutex_Unlock(CoreMutex *mtx)
{ {
__sync_lock_release(&mtx->lock); __sync_lock_release(&mtx->lock);
} }