/* SPDX-License-Identifier: GPL-2.0-only */ #include #include #include #include static struct { struct thread *head; struct thread *tail; lock_t lock; } sched_queue; void sched_add_thread(struct thread *t) { spin_lock(&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; spin_unlock(&sched_queue.lock); } void sched_remove_thread(struct thread *del) { spin_lock(&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; } spin_unlock(&sched_queue.lock); } static struct thread *dequeue_thread() { spin_lock(&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; } else { t = tls_get()->idle_thread; } spin_unlock(&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); }