Implement Mutexes and supporting syscalls, cleanup/optimize scheduler
All checks were successful
Build documentation / build-and-deploy (push) Successful in 39s
All checks were successful
Build documentation / build-and-deploy (push) Successful in 39s
This commit is contained in:
42
kernel/proc/mutex.c
Normal file
42
kernel/proc/mutex.c
Normal file
@@ -0,0 +1,42 @@
|
||||
#include <libk/assert.h>
|
||||
#include <libk/rbtree.h>
|
||||
#include <libk/std.h>
|
||||
#include <proc/mutex.h>
|
||||
#include <proc/proc.h>
|
||||
#include <sync/spin_lock.h>
|
||||
#include <sys/debug.h>
|
||||
|
||||
void proc_mutex_lock (struct proc* proc, struct proc_mutex* mutex) {
|
||||
while (atomic_flag_test_and_set_explicit (&mutex->flag, memory_order_acquire))
|
||||
proc_suspend (proc, &mutex->suspension_q);
|
||||
|
||||
/* taken */
|
||||
|
||||
mutex->owner = proc;
|
||||
}
|
||||
|
||||
bool proc_mutex_unlock (struct proc* proc, struct proc_mutex* mutex) {
|
||||
if (mutex->owner != proc)
|
||||
return false;
|
||||
|
||||
atomic_flag_clear_explicit (&mutex->flag, memory_order_release);
|
||||
|
||||
struct proc* resumed_proc;
|
||||
struct rb_node_link* node;
|
||||
rbtree_first (&mutex->suspension_q.proc_tree, node);
|
||||
|
||||
while (node) {
|
||||
struct rb_node_link* next;
|
||||
rbtree_next (node, next);
|
||||
|
||||
resumed_proc = rbtree_entry (node, struct proc, suspension_link);
|
||||
|
||||
proc_resume (resumed_proc);
|
||||
|
||||
node = next;
|
||||
}
|
||||
|
||||
assert (mutex->suspension_q.proc_tree == NULL);
|
||||
|
||||
return true;
|
||||
}
|
||||
Reference in New Issue
Block a user