From f2f7a629cccbab6a86f5f0dae2ad690ba02dd517 Mon Sep 17 00:00:00 2001 From: Matheus Date: Tue, 7 May 2024 18:52:05 -0300 Subject: [PATCH] file: use mutex for locking rather than spinlock --- kernel-src/fs/file.c | 22 +++++++++++----------- kernel-src/include/kernel/scheduler.h | 3 ++- kernel-src/sys/scheduler.c | 2 +- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/kernel-src/fs/file.c b/kernel-src/fs/file.c index ef2c89a6..6e6f1582 100644 --- a/kernel-src/fs/file.c +++ b/kernel-src/fs/file.c @@ -73,13 +73,13 @@ static int growtable(int newcount) { file_t *fd_get(int fd) { proc_t *proc = _cpu()->thread->proc; - spinlock_acquire(&proc->fdlock); + MUTEX_ACQUIRE(&proc->fdmutex, false); file_t *file = fd < proc->fdcount ? proc->fd[fd].file : NULL; if (file) FILE_HOLD(file); - spinlock_release(&proc->fdlock); + MUTEX_RELEASE(&proc->fdmutex); return file; } @@ -94,7 +94,7 @@ int fd_new(int flags, file_t **rfile, int *rfd) { return ENOMEM; proc_t *proc = _cpu()->thread->proc; - spinlock_acquire(&proc->fdlock); + MUTEX_ACQUIRE(&proc->fdmutex, false); // find first free fd int fd = getfree(proc->fdfirst); @@ -104,7 +104,7 @@ int fd_new(int flags, file_t **rfile, int *rfd) { fd = proc->fdcount; int error = growtable(fd + 1); if (error) { - spinlock_release(&proc->fdlock); + MUTEX_RELEASE(&proc->fdmutex); cleanfile(file); return error; } @@ -114,7 +114,7 @@ int fd_new(int flags, file_t **rfile, int *rfd) { proc->fd[fd].file = file; proc->fd[fd].flags = flags; - spinlock_release(&proc->fdlock); + MUTEX_RELEASE(&proc->fdmutex); *rfile = file; *rfd = fd; @@ -123,7 +123,7 @@ int fd_new(int flags, file_t **rfile, int *rfd) { int fd_close(int fd) { proc_t *proc = _cpu()->thread->proc; - spinlock_acquire(&proc->fdlock); + MUTEX_ACQUIRE(&proc->fdmutex, false); file_t *file = fd < proc->fdcount ? proc->fd[fd].file : NULL; if (file) { @@ -132,7 +132,7 @@ int fd_close(int fd) { proc->fdfirst = fd; } - spinlock_release(&proc->fdlock); + MUTEX_RELEASE(&proc->fdmutex); if (file) { FILE_RELEASE(file); @@ -147,7 +147,7 @@ int fd_close(int fd) { int fd_clone(proc_t *targproc) { proc_t *proc = _cpu()->thread->proc; int error = 0; - spinlock_acquire(&proc->fdlock); + MUTEX_ACQUIRE(&proc->fdmutex, false); targproc->fdcount = proc->fdcount; targproc->fdfirst = proc->fdfirst; @@ -166,14 +166,14 @@ int fd_clone(proc_t *targproc) { } cleanup: - spinlock_release(&proc->fdlock); + MUTEX_RELEASE(&proc->fdmutex); return error; } int fd_dup(int oldfd, int newfd, bool exact, int fdflags, int *retfd) { proc_t *proc = _cpu()->thread->proc; int err = 0; - spinlock_acquire(&proc->fdlock); + MUTEX_ACQUIRE(&proc->fdmutex, false); file_t *file = oldfd < proc->fdcount ? proc->fd[oldfd].file : NULL; if (file == NULL) { @@ -218,6 +218,6 @@ int fd_dup(int oldfd, int newfd, bool exact, int fdflags, int *retfd) { FILE_HOLD(file); cleanup: - spinlock_release(&proc->fdlock); + MUTEX_RELEASE(&proc->fdmutex); return err; } diff --git a/kernel-src/include/kernel/scheduler.h b/kernel-src/include/kernel/scheduler.h index 521dcaf8..284f808f 100644 --- a/kernel-src/include/kernel/scheduler.h +++ b/kernel-src/include/kernel/scheduler.h @@ -6,6 +6,7 @@ #include #include #include +#include #define SCHED_THREAD_FLAGS_QUEUED 1 #define SCHED_THREAD_FLAGS_RUNNING 2 @@ -64,7 +65,7 @@ typedef struct proc_t { size_t runningthreadcount; size_t fdcount; uintmax_t fdfirst; - spinlock_t fdlock; + mutex_t fdmutex; struct fd_t *fd; mode_t umask; int flags; diff --git a/kernel-src/sys/scheduler.c b/kernel-src/sys/scheduler.c index f4f3ffd7..c72b40db 100644 --- a/kernel-src/sys/scheduler.c +++ b/kernel-src/sys/scheduler.c @@ -54,7 +54,7 @@ proc_t *sched_newproc() { proc->fdcount = 3; proc->refcount = 1; proc->fdfirst = 3; - SPINLOCK_INIT(proc->fdlock); + MUTEX_INIT(&proc->fdmutex); SEMAPHORE_INIT(&proc->waitsem, 0); SPINLOCK_INIT(proc->jobctllock); SPINLOCK_INIT(proc->pgrp.lock);