Skip to content

Commit

Permalink
sched: Improve mutex
Browse files Browse the repository at this point in the history
  • Loading branch information
Qwinci committed Nov 28, 2023
1 parent 1ea6678 commit 38c017c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
20 changes: 11 additions & 9 deletions kernel/src/sched/mutex.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,14 @@ bool mutex_try_lock(Mutex* self) {
}

void mutex_lock(Mutex* self) {
bool result = atomic_exchange_explicit(&self->inner, true, memory_order_acquire);
if (result) {
Task* task = arch_get_cur_task();
Ipl old = arch_ipl_set(IPL_CRITICAL);
Ipl old = arch_ipl_set(IPL_CRITICAL);
spinlock_lock(&self->task_protector);

if (self->inner) {
Task* task = arch_get_cur_task();
task->status = TASK_STATUS_WAITING;
task->next = NULL;

spinlock_lock(&self->task_protector);

if (self->waiting_tasks) {
self->waiting_tasks_end->next = task;
self->waiting_tasks_end = task;
Expand All @@ -27,24 +25,28 @@ void mutex_lock(Mutex* self) {
}

spinlock_unlock(&self->task_protector);

arch_ipl_set(old);
sched();
}
else {
self->inner = true;
spinlock_unlock(&self->task_protector);
arch_ipl_set(old);
}
}

void mutex_unlock(Mutex* self) {
atomic_store_explicit(&self->inner, false, memory_order_release);
Ipl old = arch_ipl_set(IPL_CRITICAL);
spinlock_lock(&self->task_protector);
if (self->waiting_tasks) {
Task* task = self->waiting_tasks;
self->waiting_tasks = task->next;
spinlock_unlock(&self->task_protector);
arch_ipl_set(old);
sched_unblock(task);
arch_ipl_set(old);
}
else {
self->inner = false;
spinlock_unlock(&self->task_protector);
arch_ipl_set(old);
}
Expand Down
2 changes: 1 addition & 1 deletion kernel/src/sched/mutex.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ typedef struct Task Task;
typedef struct {
Task* waiting_tasks;
Task* waiting_tasks_end;
atomic_bool inner;
Spinlock task_protector;
bool inner;
} Mutex;

bool mutex_try_lock(Mutex* self);
Expand Down

0 comments on commit 38c017c

Please sign in to comment.