lock: merge spinlock and mtx locks into one

Signed-off-by: Michael Shaw <m@michaelsshaw.com>
This commit is contained in:
Michael Shaw 2026-08-13 14:19:10 -04:00
commit 2076f1dae1
Signed by: michaelsshaw
GPG key ID: 5A5E0FB20708A9E7
32 changed files with 158 additions and 169 deletions

View file

@ -1,10 +0,0 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#ifndef _MTX_H_
#define _MTX_H_
#include <kernel/spinlock.h>
void mtx_lock(mtx_t *l);
void mtx_unlock(mtx_t *l);
#endif /* _MTX_H_ */

View file

@ -1,17 +0,0 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#ifndef _SPINLOCK_H_
#define _SPINLOCK_H_
#include <kernel/types.h>
#include <kernel/common.h>
#define CLEANUP_SLOCK CLEANUP(cleanup_spinlock)
typedef u32 spinlock_t;
typedef spinlock_t mtx_t;
void spinlock_acquire(spinlock_t *lock);
void spinlock_release(spinlock_t *lock);
void cleanup_spinlock(spinlock_t **lock_ptr);
#endif /* _SPINLOCK_H_ */

View file

@ -1,6 +1,6 @@
# SPDX-License-Identifier: GPL-2.0-only
obj-y += atomic.o
obj-y += spinlock.o
obj-y += lock.o
obj-y += thread.o
obj-y += time.o
obj-y += tsc.o

View file

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