39 lines
664 B
C
39 lines
664 B
C
/* 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);
|
|
}
|