sched: add kernel thread creation and SMP-aware thread scheduling

This commit is contained in:
2023-05-04 21:43:18 +01:00
parent bb524c1576
commit 7d321fb7f0
5 changed files with 86 additions and 2 deletions

View File

@@ -130,6 +130,50 @@ void schedule(enum sched_mode mode)
} while (need_resched());
}
struct runqueue *select_rq_for_thread(struct thread *thr)
{
struct runqueue *best_rq = NULL;
unsigned int best_nthreads = 0;
unsigned long flags;
unsigned int nr_cpu = cpu_get_highest_available() + 1;
for (unsigned int i = 0; i < nr_cpu; i++) {
if (!cpu_is_available(i) || !cpu_is_online(i)) {
continue;
}
struct runqueue *rq = cpu_rq(i);
if (!rq) {
continue;
}
rq_lock(rq, &flags);
unsigned int nthreads = rq->rq_nthreads;
if (rq->rq_cur && rq->rq_cur != rq->rq_idle) {
nthreads++;
}
rq_unlock(rq, flags);
if (!best_rq || nthreads < best_nthreads) {
best_rq = rq;
best_nthreads = nthreads;
}
}
return best_rq;
}
void schedule_thread_on_cpu(struct thread *thr)
{
struct runqueue *rq = select_rq_for_thread(thr);
if (rq) {
unsigned long flags;
rq_lock(rq, &flags);
rq_enqueue(rq, thr);
rq_unlock(rq, flags);
}
}
void start_charge_period(void)
{
struct thread *self = current_thread();