diff --git a/include/core/mutex.h b/include/core/mutex.h index b507e0b..b1a1538 100644 --- a/include/core/mutex.h +++ b/include/core/mutex.h @@ -2,14 +2,14 @@ #ifndef __CORE_MUTEX_H__ #define __CORE_MUTEX_H__ -typedef struct Mutex { +typedef struct CoreMutex { uint64_t lock; -} Mutex; +} CoreMutex; -int CoreMutex_Init(Mutex *mtx); -void CoreMutex_Lock(Mutex *mtx); -bool CoreMutex_TryLock(Mutex *mtx); -void CoreMutex_Unlock(Mutex *mtx); +void CoreMutex_Init(CoreMutex *mtx); +void CoreMutex_Lock(CoreMutex *mtx); +bool CoreMutex_TryLock(CoreMutex *mtx); +void CoreMutex_Unlock(CoreMutex *mtx); #endif /* __CORE_MUTEX_H__ */ diff --git a/lib/libc/SConscript b/lib/libc/SConscript index 768eef0..9349fac 100644 --- a/lib/libc/SConscript +++ b/lib/libc/SConscript @@ -9,6 +9,7 @@ src = [ ] src_common = [ "abort.c", "assert.c", + "core/mutex.c", "dir.c", "exit.c", "file.c", diff --git a/lib/libc/core/mutex.c b/lib/libc/core/mutex.c index 227877f..35b423c 100644 --- a/lib/libc/core/mutex.c +++ b/lib/libc/core/mutex.c @@ -2,23 +2,26 @@ #include #include +#include #include -int -CoreMutex_Init(Mutex *mtx) +void +CoreMutex_Init(CoreMutex *mtx) { + mtx->lock = 0; } void -CoreMutex_Lock(Mutex *mtx) +CoreMutex_Lock(CoreMutex *mtx) { while (__sync_lock_test_and_set(&mtx->lock, 1) == 1) { + OSThreadSleep(0); } } bool -CoreMutex_TryLock(Mutex *mtx) +CoreMutex_TryLock(CoreMutex *mtx) { if (__sync_lock_test_and_set(&mtx->lock, 1) == 1) { return false; @@ -28,7 +31,7 @@ CoreMutex_TryLock(Mutex *mtx) } void -CoreMutex_Unlock(Mutex *mtx) +CoreMutex_Unlock(CoreMutex *mtx) { __sync_lock_release(&mtx->lock); }