user-level mutex implementation for use by other parts of libc
This commit is contained in:
parent
2fdcc4b557
commit
2d3d325066
15
include/core/mutex.h
Normal file
15
include/core/mutex.h
Normal 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
35
lib/libc/core/mutex.c
Normal 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);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user