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,7 +1,6 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#include <kernel/common.h>
#include <kernel/slab.h>
#include <kernel/mtx.h>
#include <lib/hashchain.h>
#include <lib/math.h>
#include <lib/string.h>

View file

@ -1,5 +1,4 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#include <kernel/mtx.h>
#include <kernel/types.h>
#include <kernel/slab.h>
#include <lib/list.h>

View file

@ -40,7 +40,7 @@
#include <drivers/serial.h>
#include <kernel/tty.h>
#include <kernel/spinlock.h>
#include <kernel/lock.h>
// define this globally (e.g. gcc -DPRINTF_INCLUDE_CONFIG_H ...) to include the
// printf_config.h header file
@ -941,14 +941,14 @@ static int _vsnprintf(out_fct_type out, char *buffer, const size_t maxlen, const
int printf_(const char *format, ...)
{
static spinlock_t lock = 0;
spinlock_acquire(&lock);
static lock_t lock = 0;
spin_lock(&lock);
va_list va;
va_start(va, format);
char buffer[1];
const int ret = _vsnprintf(_out_char, buffer, (size_t)-1, format, va);
va_end(va);
spinlock_release(&lock);
spin_unlock(&lock);
return ret;
}

View file

@ -4,41 +4,41 @@
void read_lock(rwlock_t *lock)
{
while (1) {
spinlock_acquire(&lock->lock);
spin_lock(&lock->lock);
if (!lock->writer) {
lock->readers++;
spinlock_release(&lock->lock);
spin_unlock(&lock->lock);
return;
}
spinlock_release(&lock->lock);
spin_unlock(&lock->lock);
}
}
void read_unlock(rwlock_t *lock)
{
spinlock_acquire(&lock->lock);
spin_lock(&lock->lock);
lock->readers--;
spinlock_release(&lock->lock);
spin_unlock(&lock->lock);
}
void write_lock(rwlock_t *lock)
{
while (1) {
spinlock_acquire(&lock->lock);
spin_lock(&lock->lock);
if (!lock->writer && lock->readers == 0) {
lock->writer = true;
spinlock_release(&lock->lock);
spin_unlock(&lock->lock);
return;
}
spinlock_release(&lock->lock);
spin_unlock(&lock->lock);
}
}
void write_unlock(rwlock_t *lock)
{
spinlock_acquire(&lock->lock);
spin_lock(&lock->lock);
lock->writer = false;
spinlock_release(&lock->lock);
spin_unlock(&lock->lock);
}
void rwlock_init(rwlock_t *lock)

View file

@ -5,12 +5,12 @@ bool sem_trywait(sem_t *sem)
{
bool ret = false;
spinlock_acquire(&sem->lock);
spin_lock(&sem->lock);
if (sem->sem > 0) {
sem->sem -= 1;
ret = true;
}
spinlock_release(&sem->lock);
spin_unlock(&sem->lock);
return ret;
}
@ -23,9 +23,9 @@ void sem_wait(sem_t *sem)
void sem_post(sem_t *sem)
{
spinlock_acquire(&sem->lock);
spin_lock(&sem->lock);
sem->sem += 1;
spinlock_release(&sem->lock);
spin_unlock(&sem->lock);
}
void sem_init(sem_t *sem, size_t n_res)