extern void switch_runqueue(runqueue_t *from_runq, gt_spinlock_t *from_runqlock, runqueue_t *to_runq, gt_spinlock_t *to_runqlock, uthread_struct_t *u_elem) { rem_from_runqueue(from_runq, from_runqlock, u_elem); add_to_runqueue(to_runq, to_runqlock, u_elem); return; }
extern uthread_struct_t *sched_find_best_uthread_group(kthread_runqueue_t *kthread_runq) { #if 0 /* [1] Tries to find a RUNNABLE uthread in active-runq from u_gid. * [2] Found - Jump to [FOUND] * [3] Tries to find a thread from a group with least threads in runq (XXX: NOT DONE) * - [Tries to find the highest priority RUNNABLE thread (XXX: DONE)] * [4] Found - Jump to [FOUND] * [5] Switches runqueues (active/expires) * [6] Repeat [1] through [4] * [NOT FOUND] Return NULL(no more jobs) * [FOUND] Remove uthread from pq and return it. */ runqueue_t *runq; prio_struct_t *prioq; uthread_head_t *u_head; uthread_struct_t *u_obj; unsigned int uprio, ugroup, mask; uthread_group_t u_gid; #ifndef COSCHED return sched_find_best_uthread(kthread_runq); #endif /* XXX: Read u_gid from global uthread-select-criterion */ u_gid = 0; runq = kthread_runq->active_runq; if(!runq->uthread_mask) { /* No jobs in active. switch runqueue */ assert(!runq->uthread_tot); kthread_runq->active_runq = kthread_runq->expires_runq; kthread_runq->expires_runq = runq; runq = kthread_runq->expires_runq; if(!runq->uthread_mask) { assert(!runq->uthread_tot); return NULL; } } if(!(mask = runq->uthread_group_mask[u_gid])) { /* No uthreads in the desired group */ assert(!runq->uthread_group_tot[u_gid]); return (sched_find_best_uthread(kthread_runq)); } /* Find the highest priority bucket for u_gid */ uprio = LOWEST_BIT_SET(mask); /* Take out a uthread from the bucket. Return it. */ u_head = &(runq->prio_array[uprio].group[u_gid]); u_obj = TAILQ_FIRST(u_head); rem_from_runqueue(runq, &(kthread_runq->kthread_runqlock), u_obj); return(u_obj); #endif return (uthread_struct_t * )NULL; }
/* [1] Tries to find the highest priority RUNNABLE uthread in active-runq. * [2] Found - Jump to [FOUND] * [3] Switches runqueues (active/expires) * [4] Repeat [1] through [2] * [NOT FOUND] Return NULL(no more jobs) * [FOUND] Remove uthread from pq and return it. */ uthread_t *pcs_pick_next_uthread(kthread_t *k_ctx) { checkpoint("k%d: PCS: Picking next uthread", k_ctx->cpuid); pcs_kthread_t *pcs_kthread = pcs_get_kthread(k_ctx); kthread_runqueue_t *kthread_runq = &pcs_kthread->k_runqueue; gt_spin_lock(&(kthread_runq->kthread_runqlock)); kthread_runq->kthread_runqlock.holder = 0x04; runqueue_t *runq = kthread_runq->active_runq; if (!(runq->uthread_mask)) { /* No jobs in active. switch runqueue */ checkpoint("k%d: PCS: Switching runqueues", k_ctx->cpuid); assert(!runq->uthread_tot); kthread_runq->active_runq = kthread_runq->expires_runq; kthread_runq->expires_runq = runq; runq = kthread_runq->active_runq; if (!runq->uthread_mask) { assert(!runq->uthread_tot); gt_spin_unlock(&(kthread_runq->kthread_runqlock)); return NULL; } } /* Find the highest priority bucket */ unsigned int uprio, ugroup; uprio = LOWEST_BIT_SET(runq->uthread_mask); prio_struct_t *prioq = &(runq->prio_array[uprio]); assert(prioq->group_mask); ugroup = LOWEST_BIT_SET(prioq->group_mask); uthread_head_t *u_head = &(prioq->group[ugroup]); pcs_uthread_t *next_uthread = TAILQ_FIRST(u_head); rem_from_runqueue(runq, NULL, next_uthread); gt_spin_unlock(&(kthread_runq->kthread_runqlock)); return next_uthread->uthread; }