Use RW spin locks
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:
@@ -12,6 +12,7 @@
|
||||
#include <proc/proc.h>
|
||||
#include <proc/resource.h>
|
||||
#include <rd/rd.h>
|
||||
#include <sync/rw_spin_lock.h>
|
||||
#include <sync/spin_lock.h>
|
||||
#include <sys/debug.h>
|
||||
#include <sys/mm.h>
|
||||
@@ -24,8 +25,16 @@
|
||||
#include <amd64/intr_defs.h>
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Lock hierachy:
|
||||
* - proc_tree_lock
|
||||
* - cpu->lock
|
||||
* - proc->lock
|
||||
* - suspension_q->lock
|
||||
*/
|
||||
|
||||
static struct rb_node_link* proc_tree = NULL;
|
||||
static spin_lock_t proc_tree_lock = SPIN_LOCK_INIT;
|
||||
static rw_spin_lock_t proc_tree_lock = RW_SPIN_LOCK_INIT;
|
||||
|
||||
static bool proc_check_elf (uint8_t* elf) {
|
||||
if (!((elf[0] == 0x7F) && (elf[1] == 'E') && (elf[2] == 'L') && (elf[3] == 'F')))
|
||||
@@ -185,8 +194,8 @@ static struct proc* proc_spawn_rd (char* name) {
|
||||
static void proc_register (struct proc* proc, struct cpu* cpu) {
|
||||
proc->cpu = cpu;
|
||||
|
||||
spin_lock (&proc_tree_lock);
|
||||
spin_lock (&cpu->lock);
|
||||
rw_spin_write_lock (&proc_tree_lock);
|
||||
rw_spin_write_lock (&cpu->lock);
|
||||
|
||||
rbtree_insert (struct proc, &cpu->proc_run_q, &proc->cpu_run_q_link, cpu_run_q_link, pid);
|
||||
rbtree_insert (struct proc, &proc_tree, &proc->proc_tree_link, proc_tree_link, pid);
|
||||
@@ -194,8 +203,8 @@ static void proc_register (struct proc* proc, struct cpu* cpu) {
|
||||
if (cpu->proc_current == NULL)
|
||||
cpu->proc_current = proc;
|
||||
|
||||
spin_unlock (&cpu->lock);
|
||||
spin_unlock (&proc_tree_lock);
|
||||
rw_spin_write_unlock (&cpu->lock);
|
||||
rw_spin_write_unlock (&proc_tree_lock);
|
||||
}
|
||||
|
||||
static struct proc* proc_find_sched (void) {
|
||||
@@ -235,19 +244,22 @@ static struct proc* proc_find_sched (void) {
|
||||
void proc_sched (void) {
|
||||
struct proc* next = NULL;
|
||||
|
||||
spin_lock (&thiscpu->lock);
|
||||
rw_spin_read_lock (&thiscpu->lock);
|
||||
|
||||
if (thiscpu->proc_run_q == NULL) {
|
||||
spin_unlock (&thiscpu->lock);
|
||||
rw_spin_read_unlock (&thiscpu->lock);
|
||||
goto idle;
|
||||
}
|
||||
|
||||
next = proc_find_sched ();
|
||||
|
||||
if (next != NULL)
|
||||
thiscpu->proc_current = next;
|
||||
rw_spin_read_unlock (&thiscpu->lock);
|
||||
|
||||
spin_unlock (&thiscpu->lock);
|
||||
if (next != NULL) {
|
||||
rw_spin_write_lock (&thiscpu->lock);
|
||||
thiscpu->proc_current = next;
|
||||
rw_spin_write_unlock (&thiscpu->lock);
|
||||
}
|
||||
|
||||
if (next != NULL && atomic_load (&next->state) == PROC_READY)
|
||||
do_sched (next);
|
||||
@@ -259,16 +271,16 @@ idle:
|
||||
void proc_kill (struct proc* proc) {
|
||||
atomic_store (&proc->state, PROC_DEAD);
|
||||
|
||||
spin_lock (&proc_tree_lock);
|
||||
rw_spin_write_lock (&proc_tree_lock);
|
||||
spin_lock (&proc->lock);
|
||||
rbtree_delete (&proc_tree, &proc->proc_tree_link);
|
||||
spin_unlock (&proc->lock);
|
||||
spin_unlock (&proc_tree_lock);
|
||||
rw_spin_write_unlock (&proc_tree_lock);
|
||||
|
||||
struct cpu* cpu = proc->cpu;
|
||||
spin_lock (&cpu->lock);
|
||||
rw_spin_write_lock (&cpu->lock);
|
||||
rbtree_delete (&cpu->proc_run_q, &proc->cpu_run_q_link);
|
||||
spin_unlock (&cpu->lock);
|
||||
rw_spin_write_unlock (&cpu->lock);
|
||||
|
||||
DEBUG ("killed PID %d\n", proc->pid);
|
||||
|
||||
@@ -280,6 +292,57 @@ void proc_kill (struct proc* proc) {
|
||||
cpu_request_sched (cpu);
|
||||
}
|
||||
|
||||
void proc_suspend (struct proc* proc, struct proc_suspension_q* sq) {
|
||||
struct cpu* cpu;
|
||||
|
||||
spin_lock (&proc->lock);
|
||||
|
||||
atomic_store (&proc->state, PROC_SUSPENDED);
|
||||
|
||||
cpu = proc->cpu;
|
||||
|
||||
/* remove from run q */
|
||||
rw_spin_write_lock (&cpu->lock);
|
||||
rbtree_delete (&cpu->proc_run_q, &proc->cpu_run_q_link);
|
||||
if (cpu->proc_current == proc)
|
||||
cpu->proc_current = NULL;
|
||||
rw_spin_write_unlock (&cpu->lock);
|
||||
|
||||
proc->suspension_q = sq;
|
||||
spin_lock (&proc->suspension_q->lock);
|
||||
rbtree_insert (struct proc, &proc->suspension_q->proc_tree, &proc->suspension_link,
|
||||
suspension_link, pid);
|
||||
spin_unlock (&proc->suspension_q->lock);
|
||||
|
||||
spin_unlock (&proc->lock);
|
||||
|
||||
cpu_request_sched (cpu);
|
||||
}
|
||||
|
||||
void proc_wakeup (struct proc* proc) {
|
||||
struct cpu* cpu;
|
||||
|
||||
spin_lock (&proc->lock);
|
||||
|
||||
cpu = proc->cpu;
|
||||
|
||||
spin_lock (&proc->suspension_q->lock);
|
||||
rbtree_delete (&proc->suspension_q->proc_tree, &proc->suspension_link);
|
||||
spin_unlock (&proc->suspension_q->lock);
|
||||
|
||||
proc->suspension_q = NULL;
|
||||
|
||||
rw_spin_write_lock (&cpu->lock);
|
||||
rbtree_insert (struct proc, &cpu->proc_run_q, &proc->cpu_run_q_link, cpu_run_q_link, pid);
|
||||
rw_spin_write_unlock (&cpu->lock);
|
||||
|
||||
atomic_store (&proc->state, PROC_READY);
|
||||
|
||||
spin_unlock (&proc->lock);
|
||||
|
||||
cpu_request_sched (cpu);
|
||||
}
|
||||
|
||||
static void proc_irq_sched (void* arg, void* regs) {
|
||||
(void)arg, (void)regs;
|
||||
proc_sched ();
|
||||
|
||||
@@ -15,10 +15,12 @@
|
||||
#include <amd64/proc.h> /* USTACK_SIZE */
|
||||
#endif
|
||||
|
||||
/// Process is ready to run
|
||||
/* Process is ready to run */
|
||||
#define PROC_READY 0
|
||||
/// Process marked garbage collection
|
||||
/* Process marked garbage collection */
|
||||
#define PROC_DEAD 1
|
||||
/* Process is suspended */
|
||||
#define PROC_SUSPENDED 2
|
||||
|
||||
#define PROC_RESOURCES_MAX 1024
|
||||
|
||||
@@ -36,6 +38,7 @@ struct proc {
|
||||
int pid;
|
||||
struct rb_node_link proc_tree_link;
|
||||
struct rb_node_link cpu_run_q_link;
|
||||
struct rb_node_link suspension_link;
|
||||
|
||||
struct list_node_link* mappings; /* pd.lock implicitly protects this field */
|
||||
struct proc_platformdata pdata;
|
||||
@@ -45,8 +48,15 @@ struct proc {
|
||||
atomic_int state;
|
||||
struct rb_node_link* resource_tree;
|
||||
atomic_int rids;
|
||||
struct proc_suspension_q* suspension_q;
|
||||
};
|
||||
|
||||
struct proc_suspension_q {
|
||||
struct rb_node_link* proc_tree;
|
||||
spin_lock_t lock;
|
||||
};
|
||||
|
||||
void proc_suspend (struct proc* proc, struct proc_suspension_q* sq);
|
||||
void proc_sched (void);
|
||||
void proc_kill (struct proc* proc);
|
||||
bool proc_map (struct proc* proc, uintptr_t start_paddr, uintptr_t start_vaddr, size_t pages,
|
||||
|
||||
Reference in New Issue
Block a user