diff --git a/include/core/mutex.h b/include/core/mutex.h new file mode 100644 index 0000000..b507e0b --- /dev/null +++ b/include/core/mutex.h @@ -0,0 +1,15 @@ + +#ifndef __CORE_MUTEX_H__ +#define __CORE_MUTEX_H__ + +typedef struct Mutex { + uint64_t lock; +} Mutex; + +int CoreMutex_Init(Mutex *mtx); +void CoreMutex_Lock(Mutex *mtx); +bool CoreMutex_TryLock(Mutex *mtx); +void CoreMutex_Unlock(Mutex *mtx); + +#endif /* __CORE_MUTEX_H__ */ + diff --git a/lib/libc/core/mutex.c b/lib/libc/core/mutex.c new file mode 100644 index 0000000..227877f --- /dev/null +++ b/lib/libc/core/mutex.c @@ -0,0 +1,35 @@ + +#include +#include + +#include + +int +CoreMutex_Init(Mutex *mtx) +{ +} + + +void +CoreMutex_Lock(Mutex *mtx) +{ + while (__sync_lock_test_and_set(&mtx->lock, 1) == 1) { + } +} + +bool +CoreMutex_TryLock(Mutex *mtx) +{ + if (__sync_lock_test_and_set(&mtx->lock, 1) == 1) { + return false; + } else { + return true; + } +} + +void +CoreMutex_Unlock(Mutex *mtx) +{ + __sync_lock_release(&mtx->lock); +} +