/* SPDX-License-Identifier: GPL-2.0-only */ #include #include #include #include /* Run queue */ 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_purge_proc(struct proc *proc) { spinlock_acquire(&sched_queue.lock); struct thread *t = sched_queue.head; struct thread *p = NULL; while (t) { if (t->proc == proc) { if (p) p->next = t->next; else sched_queue.head = t->next; if (!t->next) sched_queue.tail = p; t->next = NULL; } else { 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->proc->pgd); sched_restore_state(next); }