/* * Find the best (highest priority) thread in given * thread queue. Returns null if queue is empty. */ static __inline__ struct Kernel_Thread* Find_Best(struct Thread_Queue* queue) { /* Pick the highest priority thread */ struct Kernel_Thread *kthread = queue->head, *best = 0; while (kthread != 0) { if (best == 0 || kthread->priority > best->priority) best = kthread; kthread = Get_Next_In_Thread_Queue(kthread); } return best; }
void Switch_To_MLF(void) { struct Kernel_Thread *kthread; kthread = s_runQueue->head; // Transition to MLF MAX_QUEUE_LEVEL = 4; while (kthread != 0) { if (kthread->priority == PRIORITY_IDLE) { Remove_From_Thread_Queue(&s_runQueue ,kthread); Add_To_Front_Of_Thread_Queue(&s_runQueue[MAX_QUEUE_LEVEL-1], kthread); kthread->currentReadyQueue = MAX_QUEUE_LEVEL-1; } kthread = Get_Next_In_Thread_Queue(kthread); } }
/* * Wake up all threads waiting on given wait queue. * Must be called with interrupts disabled! * See Keyboard_Interrupt_Handler() function in keyboard.c * for an example. */ void Wake_Up(struct Thread_Queue* waitQueue) { struct Kernel_Thread *kthread = waitQueue->head, *next; KASSERT(!Interrupts_Enabled()); /* * Walk throught the list of threads in the wait queue, * transferring each one to the run queue. */ while (kthread != 0) { next = Get_Next_In_Thread_Queue(kthread); Make_Runnable(kthread); kthread = next; } /* The wait queue is now empty. */ Clear_Thread_Queue(waitQueue); }
/* * The reaper thread. Its job is to de-allocate memory * used by threads which have finished. */ static void Reaper(ulong_t arg) { struct Kernel_Thread *kthread; Disable_Interrupts(); while (true) { /* See if there are any threads needing disposal. */ if ((kthread = s_graveyardQueue.head) == 0) { /* Graveyard is empty, so wait for a thread to die. */ Wait(&s_reaperWaitQueue); } else { /* Make the graveyard queue empty. */ Clear_Thread_Queue(&s_graveyardQueue); /* * Now we can re-enable interrupts, since we * have removed all the threads needing disposal. */ Enable_Interrupts(); Yield(); /* allow other threads to run? */ /* Dispose of the dead threads. */ while (kthread != 0) { struct Kernel_Thread* next = Get_Next_In_Thread_Queue(kthread); #if 0 Print("Reaper: disposing of thread @ %x, stack @ %x\n", kthread, kthread->stackPage); #endif Destroy_Thread(kthread); kthread = next; } /* * Disable interrupts again, since we're going to * do another iteration. */ Disable_Interrupts(); } } }