152 lines
3.7 KiB
C
152 lines
3.7 KiB
C
#include <libk/assert.h>
|
|
#include <libk/rbtree.h>
|
|
#include <libk/std.h>
|
|
#include <libk/string.h>
|
|
#include <mm/liballoc.h>
|
|
#include <proc/mutex.h>
|
|
#include <proc/proc.h>
|
|
#include <sync/spin_lock.h>
|
|
#include <sys/debug.h>
|
|
#include <sys/smp.h>
|
|
#include <sys/spin_lock.h>
|
|
|
|
static void proc_mutex_suspend (struct proc* proc, struct proc_suspension_q* sq,
|
|
spin_lock_t* resource_lock, spin_lock_ctx_t* ctxrl) {
|
|
spin_lock_ctx_t ctxpr, ctxcpu, ctxsq;
|
|
struct cpu* cpu = proc->cpu;
|
|
|
|
struct proc_sq_entry* sq_entry = malloc (sizeof (*sq_entry));
|
|
if (!sq_entry) {
|
|
spin_unlock (resource_lock, ctxrl);
|
|
return;
|
|
}
|
|
|
|
sq_entry->proc = proc;
|
|
sq_entry->sq = sq;
|
|
|
|
spin_lock (&cpu->lock, &ctxcpu);
|
|
spin_lock (&proc->lock, &ctxpr);
|
|
spin_lock (&sq->lock, &ctxsq);
|
|
|
|
spin_unlock (resource_lock, ctxrl);
|
|
|
|
atomic_store (&proc->state, PROC_SUSPENDED);
|
|
|
|
/* append to sq's list */
|
|
list_append (sq->proc_list, &sq_entry->sq_link);
|
|
|
|
/* append to proc's list */
|
|
list_append (proc->sq_entries, &sq_entry->proc_link);
|
|
|
|
list_remove (cpu->proc_run_q, &proc->cpu_run_q_link);
|
|
atomic_fetch_sub (&cpu->proc_run_q_count, 1);
|
|
|
|
if (cpu->proc_current == proc)
|
|
cpu->proc_current = NULL;
|
|
|
|
proc->cpu = NULL;
|
|
|
|
spin_unlock (&sq->lock, &ctxsq);
|
|
spin_unlock (&proc->lock, &ctxpr);
|
|
spin_unlock (&cpu->lock, &ctxcpu);
|
|
|
|
cpu_request_sched (cpu);
|
|
}
|
|
|
|
static void proc_mutex_resume (struct proc* proc, struct proc_sq_entry* sq_entry) {
|
|
spin_lock_ctx_t ctxsq, ctxpr, ctxcpu;
|
|
struct cpu* cpu = cpu_find_lightest ();
|
|
struct proc_suspension_q* sq = sq_entry->sq;
|
|
|
|
spin_lock (&cpu->lock, &ctxcpu);
|
|
spin_lock (&proc->lock, &ctxpr);
|
|
spin_lock (&sq->lock, &ctxsq);
|
|
|
|
/* remove from sq's list */
|
|
list_remove (sq->proc_list, &sq_entry->sq_link);
|
|
|
|
/* remove from proc's list */
|
|
list_remove (proc->sq_entries, &sq_entry->proc_link);
|
|
|
|
proc->cpu = cpu;
|
|
|
|
if (proc->sq_entries == NULL)
|
|
atomic_store (&proc->state, PROC_READY);
|
|
|
|
list_append (cpu->proc_run_q, &proc->cpu_run_q_link);
|
|
atomic_fetch_add (&cpu->proc_run_q_count, 1);
|
|
|
|
spin_unlock (&sq->lock, &ctxsq);
|
|
spin_unlock (&proc->lock, &ctxpr);
|
|
spin_unlock (&cpu->lock, &ctxcpu);
|
|
|
|
free (sq_entry);
|
|
|
|
cpu_request_sched (cpu);
|
|
}
|
|
|
|
bool proc_create_resource_mutex (struct proc_mutex* mutex) {
|
|
memset (mutex, 0, sizeof (*mutex));
|
|
|
|
return true;
|
|
}
|
|
|
|
void proc_cleanup_resource_mutex (struct proc* proc, struct proc_resource* resource) {
|
|
struct proc_mutex* mutex = &resource->u.mutex;
|
|
}
|
|
|
|
void proc_mutex_lock (struct proc* proc, struct proc_mutex* mutex) {
|
|
spin_lock_ctx_t ctxmt;
|
|
|
|
for (;;) {
|
|
spin_lock (&mutex->resource->lock, &ctxmt);
|
|
|
|
if (!mutex->locked || mutex->owner == proc) {
|
|
mutex->locked = true;
|
|
mutex->owner = proc;
|
|
spin_unlock (&mutex->resource->lock, &ctxmt);
|
|
return;
|
|
}
|
|
|
|
proc_mutex_suspend (proc, &mutex->suspension_q, &mutex->resource->lock, &ctxmt);
|
|
}
|
|
}
|
|
|
|
bool proc_mutex_unlock (struct proc* proc, struct proc_mutex* mutex) {
|
|
spin_lock_ctx_t ctxmt, ctxsq;
|
|
|
|
spin_lock (&mutex->resource->lock, &ctxmt);
|
|
|
|
if (mutex->owner != proc) {
|
|
spin_unlock (&mutex->resource->lock, &ctxmt);
|
|
return false;
|
|
}
|
|
|
|
spin_lock (&mutex->suspension_q.lock, &ctxsq);
|
|
|
|
struct list_node_link* node = mutex->suspension_q.proc_list;
|
|
|
|
if (node) {
|
|
struct proc_sq_entry* sq_entry = list_entry (node, struct proc_sq_entry, sq_link);
|
|
struct proc* resumed_proc = sq_entry->proc;
|
|
|
|
mutex->owner = resumed_proc;
|
|
mutex->locked = true;
|
|
|
|
spin_unlock (&mutex->suspension_q.lock, &ctxsq);
|
|
spin_unlock (&mutex->resource->lock, &ctxmt);
|
|
|
|
proc_mutex_resume (resumed_proc, sq_entry);
|
|
|
|
return true;
|
|
}
|
|
|
|
mutex->locked = false;
|
|
mutex->owner = NULL;
|
|
|
|
spin_unlock (&mutex->suspension_q.lock, &ctxsq);
|
|
spin_unlock (&mutex->resource->lock, &ctxmt);
|
|
|
|
return true;
|
|
}
|