mirror of
https://github.com/Mathewnd/Astral.git
synced 2026-11-01 15:08:21 +00:00
file: use mutex for locking rather than spinlock
This commit is contained in:
+11
-11
@@ -73,13 +73,13 @@ static int growtable(int newcount) {
|
|||||||
|
|
||||||
file_t *fd_get(int fd) {
|
file_t *fd_get(int fd) {
|
||||||
proc_t *proc = _cpu()->thread->proc;
|
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;
|
file_t *file = fd < proc->fdcount ? proc->fd[fd].file : NULL;
|
||||||
if (file)
|
if (file)
|
||||||
FILE_HOLD(file);
|
FILE_HOLD(file);
|
||||||
|
|
||||||
spinlock_release(&proc->fdlock);
|
MUTEX_RELEASE(&proc->fdmutex);
|
||||||
|
|
||||||
return file;
|
return file;
|
||||||
}
|
}
|
||||||
@@ -94,7 +94,7 @@ int fd_new(int flags, file_t **rfile, int *rfd) {
|
|||||||
return ENOMEM;
|
return ENOMEM;
|
||||||
|
|
||||||
proc_t *proc = _cpu()->thread->proc;
|
proc_t *proc = _cpu()->thread->proc;
|
||||||
spinlock_acquire(&proc->fdlock);
|
MUTEX_ACQUIRE(&proc->fdmutex, false);
|
||||||
|
|
||||||
// find first free fd
|
// find first free fd
|
||||||
int fd = getfree(proc->fdfirst);
|
int fd = getfree(proc->fdfirst);
|
||||||
@@ -104,7 +104,7 @@ int fd_new(int flags, file_t **rfile, int *rfd) {
|
|||||||
fd = proc->fdcount;
|
fd = proc->fdcount;
|
||||||
int error = growtable(fd + 1);
|
int error = growtable(fd + 1);
|
||||||
if (error) {
|
if (error) {
|
||||||
spinlock_release(&proc->fdlock);
|
MUTEX_RELEASE(&proc->fdmutex);
|
||||||
cleanfile(file);
|
cleanfile(file);
|
||||||
return error;
|
return error;
|
||||||
}
|
}
|
||||||
@@ -114,7 +114,7 @@ int fd_new(int flags, file_t **rfile, int *rfd) {
|
|||||||
proc->fd[fd].file = file;
|
proc->fd[fd].file = file;
|
||||||
proc->fd[fd].flags = flags;
|
proc->fd[fd].flags = flags;
|
||||||
|
|
||||||
spinlock_release(&proc->fdlock);
|
MUTEX_RELEASE(&proc->fdmutex);
|
||||||
|
|
||||||
*rfile = file;
|
*rfile = file;
|
||||||
*rfd = fd;
|
*rfd = fd;
|
||||||
@@ -123,7 +123,7 @@ int fd_new(int flags, file_t **rfile, int *rfd) {
|
|||||||
|
|
||||||
int fd_close(int fd) {
|
int fd_close(int fd) {
|
||||||
proc_t *proc = _cpu()->thread->proc;
|
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;
|
file_t *file = fd < proc->fdcount ? proc->fd[fd].file : NULL;
|
||||||
if (file) {
|
if (file) {
|
||||||
@@ -132,7 +132,7 @@ int fd_close(int fd) {
|
|||||||
proc->fdfirst = fd;
|
proc->fdfirst = fd;
|
||||||
}
|
}
|
||||||
|
|
||||||
spinlock_release(&proc->fdlock);
|
MUTEX_RELEASE(&proc->fdmutex);
|
||||||
|
|
||||||
if (file) {
|
if (file) {
|
||||||
FILE_RELEASE(file);
|
FILE_RELEASE(file);
|
||||||
@@ -147,7 +147,7 @@ int fd_close(int fd) {
|
|||||||
int fd_clone(proc_t *targproc) {
|
int fd_clone(proc_t *targproc) {
|
||||||
proc_t *proc = _cpu()->thread->proc;
|
proc_t *proc = _cpu()->thread->proc;
|
||||||
int error = 0;
|
int error = 0;
|
||||||
spinlock_acquire(&proc->fdlock);
|
MUTEX_ACQUIRE(&proc->fdmutex, false);
|
||||||
|
|
||||||
targproc->fdcount = proc->fdcount;
|
targproc->fdcount = proc->fdcount;
|
||||||
targproc->fdfirst = proc->fdfirst;
|
targproc->fdfirst = proc->fdfirst;
|
||||||
@@ -166,14 +166,14 @@ int fd_clone(proc_t *targproc) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
cleanup:
|
cleanup:
|
||||||
spinlock_release(&proc->fdlock);
|
MUTEX_RELEASE(&proc->fdmutex);
|
||||||
return error;
|
return error;
|
||||||
}
|
}
|
||||||
|
|
||||||
int fd_dup(int oldfd, int newfd, bool exact, int fdflags, int *retfd) {
|
int fd_dup(int oldfd, int newfd, bool exact, int fdflags, int *retfd) {
|
||||||
proc_t *proc = _cpu()->thread->proc;
|
proc_t *proc = _cpu()->thread->proc;
|
||||||
int err = 0;
|
int err = 0;
|
||||||
spinlock_acquire(&proc->fdlock);
|
MUTEX_ACQUIRE(&proc->fdmutex, false);
|
||||||
|
|
||||||
file_t *file = oldfd < proc->fdcount ? proc->fd[oldfd].file : NULL;
|
file_t *file = oldfd < proc->fdcount ? proc->fd[oldfd].file : NULL;
|
||||||
if (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);
|
FILE_HOLD(file);
|
||||||
|
|
||||||
cleanup:
|
cleanup:
|
||||||
spinlock_release(&proc->fdlock);
|
MUTEX_RELEASE(&proc->fdmutex);
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@
|
|||||||
#include <kernel/abi.h>
|
#include <kernel/abi.h>
|
||||||
#include <kernel/vfs.h>
|
#include <kernel/vfs.h>
|
||||||
#include <semaphore.h>
|
#include <semaphore.h>
|
||||||
|
#include <mutex.h>
|
||||||
|
|
||||||
#define SCHED_THREAD_FLAGS_QUEUED 1
|
#define SCHED_THREAD_FLAGS_QUEUED 1
|
||||||
#define SCHED_THREAD_FLAGS_RUNNING 2
|
#define SCHED_THREAD_FLAGS_RUNNING 2
|
||||||
@@ -64,7 +65,7 @@ typedef struct proc_t {
|
|||||||
size_t runningthreadcount;
|
size_t runningthreadcount;
|
||||||
size_t fdcount;
|
size_t fdcount;
|
||||||
uintmax_t fdfirst;
|
uintmax_t fdfirst;
|
||||||
spinlock_t fdlock;
|
mutex_t fdmutex;
|
||||||
struct fd_t *fd;
|
struct fd_t *fd;
|
||||||
mode_t umask;
|
mode_t umask;
|
||||||
int flags;
|
int flags;
|
||||||
|
|||||||
@@ -54,7 +54,7 @@ proc_t *sched_newproc() {
|
|||||||
proc->fdcount = 3;
|
proc->fdcount = 3;
|
||||||
proc->refcount = 1;
|
proc->refcount = 1;
|
||||||
proc->fdfirst = 3;
|
proc->fdfirst = 3;
|
||||||
SPINLOCK_INIT(proc->fdlock);
|
MUTEX_INIT(&proc->fdmutex);
|
||||||
SEMAPHORE_INIT(&proc->waitsem, 0);
|
SEMAPHORE_INIT(&proc->waitsem, 0);
|
||||||
SPINLOCK_INIT(proc->jobctllock);
|
SPINLOCK_INIT(proc->jobctllock);
|
||||||
SPINLOCK_INIT(proc->pgrp.lock);
|
SPINLOCK_INIT(proc->pgrp.lock);
|
||||||
|
|||||||
Reference in New Issue
Block a user