vbos/amd64/kernel/spinlock.c
Michael Shaw 9c50a1df92
kernel: add thread_wait, thread_block and mtx
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
2026-08-12 14:59:34 -04:00

38 lines
658 B
C

/* SPDX-License-Identifier: GPL-2.0-only */
#include <kernel/common.h>
#include <kernel/spinlock.h>
#include <kernel/atomic.h>
#include <arch/proc.h>
static uint64_t spinlock_attempt_acquire(spinlock_t *lock)
{
return atomic_cmpxchg(lock, 0, 1);
}
void spinlock_acquire(spinlock_t *lock)
{
while (spinlock_attempt_acquire(lock))
;
}
void spinlock_release(spinlock_t *lock)
{
atomic_cmpxchg(lock, 1, 0);
}
void cleanup_spinlock(spinlock_t **lock_ptr)
{
if (lock_ptr && *lock_ptr)
spinlock_release(*lock_ptr);
}
void mtx_lock(mtx_t *l)
{
while (spinlock_attempt_acquire(l))
thread_wait();
}
void mtx_unlock(mtx_t *l)
{
spinlock_release(l);
}