lock: add mutex
Signed-off-by: Michael Shaw <spencershawiscool@gmail.com>
This commit is contained in:
parent
eeee9c1638
commit
15c0de6de8
2 changed files with 23 additions and 1 deletions
|
|
@ -3,9 +3,13 @@
|
|||
#define _LOCK_H_
|
||||
|
||||
typedef int spinlock_t;
|
||||
typedef spinlock_t mtx_t;
|
||||
|
||||
int atomic_cmpxchg(volatile int *ptr, int cmpval, int newval);
|
||||
void spinlock_acquire(spinlock_t *lock);
|
||||
void spinlock_release(spinlock_t *lock);
|
||||
|
||||
void mtx_acquire(mtx_t *mtx);
|
||||
void mtx_release(mtx_t *mtx);
|
||||
|
||||
#endif /* _LOCK_H_ */
|
||||
|
|
|
|||
20
lib/lock.c
20
lib/lock.c
|
|
@ -1,9 +1,16 @@
|
|||
#include <kernel/common.h>
|
||||
#include <kernel/lock.h>
|
||||
|
||||
#include <kernel/proc.h>
|
||||
|
||||
static uint64_t spinlock_attempt_acquire(spinlock_t *lock)
|
||||
{
|
||||
return atomic_cmpxchg(lock, 0, 1);
|
||||
}
|
||||
|
||||
void spinlock_acquire(spinlock_t *lock)
|
||||
{
|
||||
while (atomic_cmpxchg(lock, 0, 1))
|
||||
while (spinlock_attempt_acquire(lock))
|
||||
;
|
||||
}
|
||||
|
||||
|
|
@ -11,3 +18,14 @@ void spinlock_release(spinlock_t *lock)
|
|||
{
|
||||
atomic_cmpxchg(lock, 1, 0);
|
||||
}
|
||||
|
||||
void mtx_acquire(mtx_t *mtx)
|
||||
{
|
||||
while (spinlock_attempt_acquire(mtx))
|
||||
sswtch();
|
||||
}
|
||||
|
||||
void mtx_release(mtx_t *mtx)
|
||||
{
|
||||
spinlock_release(mtx);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue