/* SPDX-License-Identifier: GPL-2.0-only */ #include #include #include #include /* Run queue */ static struct { struct thread *head; struct thread *tail; spinlock_t lock; } _run_queue; void sched_add_thread(struct thread *t) { spinlock_acquire(&_run_queue.lock); t->next = NULL; if (_run_queue.tail) _run_queue.tail->next = t; _run_queue.tail = t; if (!_run_queue.head) _run_queue.head = t; spinlock_release(&_run_queue.lock); } static struct thread *dequeue_thread() { spinlock_acquire(&_run_queue.lock); struct thread *t = _run_queue.head; if (t) { _run_queue.head = t->next; if (!_run_queue.head) _run_queue.tail = NULL; t->next = NULL; } spinlock_release(&_run_queue.lock); return t; } void schedule() { struct thread *cur = get_cur_thread(); struct thread *next = dequeue_thread(); if (!next) return; struct thread *prev = cur; if (cur) sched_add_thread(cur); 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); switch_to(prev, next); }