thread: remove struct proc abstraction
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
This commit is contained in:
parent
20905ec95f
commit
fa408751e1
16 changed files with 646 additions and 120 deletions
76
kernel/thread/sched.c
Normal file
76
kernel/thread/sched.c
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#include <arch/proc.h>
|
||||
#include <arch/page.h>
|
||||
#include <kernel/thread.h>
|
||||
#include <kernel/sched.h>
|
||||
|
||||
static struct {
|
||||
struct thread *head;
|
||||
struct thread *tail;
|
||||
spinlock_t lock;
|
||||
} sched_queue;
|
||||
|
||||
void sched_add_thread(struct thread *t)
|
||||
{
|
||||
spinlock_acquire(&sched_queue.lock);
|
||||
t->next = NULL;
|
||||
if (sched_queue.tail)
|
||||
sched_queue.tail->next = t;
|
||||
sched_queue.tail = t;
|
||||
if (!sched_queue.head)
|
||||
sched_queue.head = t;
|
||||
spinlock_release(&sched_queue.lock);
|
||||
}
|
||||
|
||||
void sched_remove_thread(struct thread *del)
|
||||
{
|
||||
spinlock_acquire(&sched_queue.lock);
|
||||
struct thread *t = sched_queue.head;
|
||||
struct thread *p = NULL;
|
||||
while (t) {
|
||||
if (t == del) {
|
||||
if (p)
|
||||
p->next = t->next;
|
||||
else
|
||||
sched_queue.head = t->next;
|
||||
|
||||
if (!t->next)
|
||||
sched_queue.tail = p;
|
||||
|
||||
t->next = NULL;
|
||||
break;
|
||||
}
|
||||
p = t;
|
||||
t = t->next;
|
||||
}
|
||||
spinlock_release(&sched_queue.lock);
|
||||
}
|
||||
|
||||
static struct thread *dequeue_thread()
|
||||
{
|
||||
spinlock_acquire(&sched_queue.lock);
|
||||
struct thread *t = sched_queue.head;
|
||||
if (t) {
|
||||
sched_queue.head = t->next;
|
||||
if (!sched_queue.head)
|
||||
sched_queue.tail = NULL;
|
||||
t->next = NULL;
|
||||
}
|
||||
spinlock_release(&sched_queue.lock);
|
||||
return t;
|
||||
}
|
||||
|
||||
void schedule()
|
||||
{
|
||||
struct thread *next = dequeue_thread();
|
||||
if (!next)
|
||||
return;
|
||||
|
||||
struct cpu_local *tls = tls_get();
|
||||
tls->cur_thread = next;
|
||||
tls->kernel_rsp = next->kstack_top;
|
||||
tls->tss.rsp0 = next->kstack_top;
|
||||
pgd_load(next->pgd);
|
||||
|
||||
sched_restore_state(next);
|
||||
}
|
||||
Loading…
Reference in a new issue