[SMP] SMP synchronization and scheduling

This commit is contained in:
2016-11-18 09:36:01 +01:00
parent 8961ae33eb
commit 474914ab1e
12 changed files with 69 additions and 18 deletions

View File

@@ -2,6 +2,7 @@
#include <thread.h>
#include <debug.h>
#include <scheduler.h>
#include <sync.h>
uint64_t pid = 1;
process_t *init_proc = 0;
@@ -20,7 +21,9 @@ process_t *process_spawn(process_t *parent)
if(parent)
{
spin_lock(&parent->lock);
LIST_APPEND(parent->children, proc, siblings);
spin_unlock(&parent->lock);
}
if(proc->pid == 1)
@@ -31,8 +34,10 @@ process_t *process_spawn(process_t *parent)
void process_attach(process_t *proc, thread_t *th)
{
spin_lock(&proc->lock);
LIST_APPEND(proc->threads, th, process_threads);
th->process = proc;
spin_unlock(&proc->lock);
}
void switch_process(process_t *proc)
@@ -60,6 +65,7 @@ void switch_process(process_t *proc)
void process_exit(process_t *proc, uint64_t status)
{
spin_lock(&proc->lock);
proc->status = status;
LIST_FOREACH(proc->children, process_t, c, siblings)
{
@@ -69,6 +75,7 @@ void process_exit(process_t *proc, uint64_t status)
LIST_APPEND(init_proc->children, c, siblings);
}
proc->state = PROC_STATE_ZOMBIE;
spin_unlock(&proc->lock);
}
void process_free(process_t *proc)

View File

@@ -3,6 +3,8 @@
#include <debug.h>
#include <sse.h>
#include <apic.h>
#include <sync.h>
#include <debug.h>
LIST(thread_t, ready_queue);
@@ -11,29 +13,37 @@ thread_t *scheduler_th;
#define set_last_thread(new) (get_cpu()->last_thread = (new))
int scheduler_started = 0;
lock_t scheduler_lock = 0;
void scheduler_insert(thread_t *th)
{
// Append thread to the ready queue and prepare it for running
spin_lock(&scheduler_lock);
LIST_APPEND(ready_queue, th, ready_queue);
th->state = THREAD_STATE_READY;
spin_unlock(&scheduler_lock);
}
void scheduler_remove(thread_t *th)
{
// Remove thread from the ready queue
spin_lock(&scheduler_lock);
LIST_REMOVE(ready_queue, th, ready_queue);
spin_unlock(&scheduler_lock);
}
thread_t *scheduler_next()
{
// Get the next thread from the ready queue
spin_lock(&scheduler_lock);
if(!LIST_EMPTY(ready_queue))
{
thread_t *th = LIST_FIRST(ready_queue);
scheduler_remove(th);
LIST_REMOVE(ready_queue, th, ready_queue);
spin_unlock(&scheduler_lock);
return th;
}
spin_unlock(&scheduler_lock);
return 0;
}

View File

@@ -8,7 +8,6 @@
uint64_t tid = 1;
thread_t *new_thread(void (*func)(void))
{
// Set up original stack of thread