lock: merge spinlock and mtx locks into one
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
This commit is contained in:
parent
3e5abada9b
commit
2076f1dae1
32 changed files with 158 additions and 169 deletions
39
amd64/kernel/lock.c
Normal file
39
amd64/kernel/lock.c
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#include <kernel/common.h>
|
||||
#include <kernel/lock.h>
|
||||
#include <kernel/atomic.h>
|
||||
#include <kernel/thread.h>
|
||||
#include <arch/proc.h>
|
||||
|
||||
static uint64_t spinlock_attempt_acquire(lock_t *lock)
|
||||
{
|
||||
return atomic_cmpxchg(lock, 0, 1);
|
||||
}
|
||||
|
||||
void spin_lock(lock_t *lock)
|
||||
{
|
||||
while (spinlock_attempt_acquire(lock))
|
||||
;
|
||||
}
|
||||
|
||||
void spin_unlock(lock_t *lock)
|
||||
{
|
||||
atomic_cmpxchg(lock, 1, 0);
|
||||
}
|
||||
|
||||
void cleanup_spinlock(lock_t **lock_ptr)
|
||||
{
|
||||
if (lock_ptr && *lock_ptr)
|
||||
spin_unlock(*lock_ptr);
|
||||
}
|
||||
|
||||
void mtx_lock(lock_t *l)
|
||||
{
|
||||
while (spinlock_attempt_acquire(l))
|
||||
reschedule(THREAD_STATE_WAITING);
|
||||
}
|
||||
|
||||
void mtx_unlock(lock_t *l)
|
||||
{
|
||||
spin_unlock(l);
|
||||
}
|
||||
Loading…
Reference in a new issue