2023-03-17 20:07:49 +00:00
|
|
|
#include <socks/sched.h>
|
|
|
|
|
#include <socks/percpu.h>
|
2023-05-04 21:42:51 +01:00
|
|
|
#include <socks/cpu.h>
|
2023-03-17 20:07:49 +00:00
|
|
|
|
2023-04-30 14:27:57 +01:00
|
|
|
#define PRIO_MASK(p) (((uint32_t)1) << (p))
|
|
|
|
|
#define FIRST_PRIO(m) (m > 0 ? (PRIO_MAX - __builtin_clz(m) - 1) : -1)
|
|
|
|
|
|
|
|
|
|
void rq_init(struct runqueue *rq)
|
2023-03-18 19:35:23 +00:00
|
|
|
{
|
|
|
|
|
memset(rq, 0x00, sizeof *rq);
|
|
|
|
|
rq->rq_lock = SPIN_LOCK_INIT;
|
|
|
|
|
}
|
2023-04-30 14:27:57 +01:00
|
|
|
|
|
|
|
|
struct thread *rq_dequeue(struct runqueue *rq)
|
|
|
|
|
{
|
|
|
|
|
int prio = FIRST_PRIO(rq->rq_readybits);
|
|
|
|
|
if (prio == -1) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct queue *q = &rq->rq_queues[prio];
|
|
|
|
|
struct queue_entry *qe = queue_pop_front(q);
|
|
|
|
|
if (!qe) {
|
|
|
|
|
rq->rq_readybits &= ~PRIO_MASK(prio);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct thread *thr = QUEUE_CONTAINER(struct thread, tr_rqentry, qe);
|
|
|
|
|
|
2023-05-04 21:43:18 +01:00
|
|
|
if (rq->rq_nthreads > 0) {
|
|
|
|
|
rq->rq_nthreads--;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-30 14:27:57 +01:00
|
|
|
if (queue_empty(q)) {
|
|
|
|
|
rq->rq_readybits &= ~PRIO_MASK(prio);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return thr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void rq_enqueue(struct runqueue *rq, struct thread *thr)
|
|
|
|
|
{
|
|
|
|
|
int prio = thread_priority(thr);
|
|
|
|
|
if (prio < 0 || prio > PRIO_MAX) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct queue *q = &rq->rq_queues[prio];
|
|
|
|
|
queue_push_back(q, &thr->tr_rqentry);
|
2023-05-04 21:42:51 +01:00
|
|
|
rq->rq_nthreads++;
|
2023-04-30 14:27:57 +01:00
|
|
|
|
|
|
|
|
rq->rq_readybits |= PRIO_MASK(thread_priority(thr));
|
2023-05-10 20:29:57 +01:00
|
|
|
thr->tr_rq = rq;
|
2023-04-30 14:27:57 +01:00
|
|
|
}
|
2023-05-04 21:42:51 +01:00
|
|
|
|
|
|
|
|
struct runqueue *cpu_rq(unsigned int cpu)
|
|
|
|
|
{
|
|
|
|
|
struct cpu_data *cpu_data = get_cpu(cpu);
|
|
|
|
|
struct runqueue *rq = &cpu_data->c_rq;
|
|
|
|
|
put_cpu(cpu_data);
|
|
|
|
|
|
|
|
|
|
return rq;
|
|
|
|
|
}
|