user-level mutex implementation for use by other parts of libc

This commit is contained in:
Ali Mashtizadeh 2015-01-30 18:41:46 -08:00
parent 2fdcc4b557
commit 2d3d325066
2 changed files with 50 additions and 0 deletions

15
include/core/mutex.h Normal file
View File

@ -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__ */

35
lib/libc/core/mutex.c Normal file
View File

@ -0,0 +1,35 @@
#include <stdbool.h>
#include <stdint.h>
#include <core/mutex.h>
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);
}