nachos.kernel.threads
Class Scheduler

java.lang.Object
  extended by nachos.kernel.threads.Scheduler

public class Scheduler
extends java.lang.Object

The scheduler is responsible for maintaining a list of threads that are ready to run and for choosing the next thread to run. These routines assume that interrupts are already disabled. If interrupts are disabled, we can assume mutual exclusion (since we are on a uniprocessor). NOTE: We can't use Locks to provide mutual exclusion here, since if we needed to wait for a lock, and the lock was busy, we would end up calling findNextToRun(), and that would put us in an infinite loop. Very simple implementation -- no priorities, straight FIFO. Might need to be improved in later assignments.


Nested Class Summary
private static class Scheduler.TimerInterruptHandler
          Interrupt handler for the timer device.
 
Field Summary
private static nachos.machine.NachosThread currentThread
          The currently running thread, or null, if none.
private static List readyList
          Queue of threads that are ready to run, but not running.
private static nachos.machine.NachosThread threadToBeDestroyed
          Terminated thread awaiting reclamation of its stack.
private static nachos.machine.Timer timer
          The timer used to implement time slicing.
 
Constructor Summary
Scheduler()
           
 
Method Summary
static nachos.machine.NachosThread currentThread()
          Accessor method for accessing the current thread.
private static nachos.machine.NachosThread findNextToRun()
          Return the next thread to be scheduled onto the CPU.
static void finish()
          Called by a thread to terminate itself.
static void init(java.lang.String[] args)
          Initialize the scheduler.
static void readyToRun(nachos.machine.NachosThread thread)
          Mark a thread as ready, but not running, and put it on the ready list for later scheduling onto the CPU.
private static void run(nachos.machine.NachosThread nextThread)
          Dispatch the CPU to nextThread.
static void setRandomYield(boolean on)
          Called to turn on or off a timer that forces a context switch at random intervals.
static void sleep()
          Relinquish the CPU, because the current thread is blocked waiting on a synchronization variable (Semaphore, Lock, or Condition).
static void start()
          Called by a Java thread (usually the initial thread that calls Nachos.main) to start the first Nachos thread.
static void yield()
          Relinquish the CPU if any other thread is ready to run.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

readyList

private static List readyList
Queue of threads that are ready to run, but not running.


currentThread

private static nachos.machine.NachosThread currentThread
The currently running thread, or null, if none.


threadToBeDestroyed

private static nachos.machine.NachosThread threadToBeDestroyed
Terminated thread awaiting reclamation of its stack.


timer

private static nachos.machine.Timer timer
The timer used to implement time slicing.

Constructor Detail

Scheduler

public Scheduler()
Method Detail

currentThread

public static nachos.machine.NachosThread currentThread()
Accessor method for accessing the current thread.


init

public static void init(java.lang.String[] args)
Initialize the scheduler. Set the list of ready but not running threads to empty. Process command-line arguments.


start

public static void start()
Called by a Java thread (usually the initial thread that calls Nachos.main) to start the first Nachos thread. Interrupts are assumed to be off when this method is called.


readyToRun

public static void readyToRun(nachos.machine.NachosThread thread)
Mark a thread as ready, but not running, and put it on the ready list for later scheduling onto the CPU.

Parameters:
thread - The thread to be put on the ready list.

findNextToRun

private static nachos.machine.NachosThread findNextToRun()
Return the next thread to be scheduled onto the CPU. If there are no ready threads, return null. Side effect: Thread is removed from the ready list.

Returns:
the thread to be scheduled onto the CPU.

run

private static void run(nachos.machine.NachosThread nextThread)
Dispatch the CPU to nextThread. Save the state of the old thread, and load the state of the new thread, by calling the machine dependent context switch routine, NachosThread.switchTo(). Note: we assume the state of the previously running thread has already been changed from running to blocked or ready (depending). Side effect: The global variable currentThread becomes nextThread.

Parameters:
nextThread - The thread to be given the CPU.

yield

public static void yield()
Relinquish the CPU if any other thread is ready to run. If so, put the thread on the end of the ready list, so that it will eventually be re-scheduled. NOTE: returns immediately if no other thread on the ready queue. Otherwise returns when the thread eventually works its way to the front of the ready list and gets re-scheduled. NOTE: we disable interrupts, so that looking at the thread on the front of the ready list, and switching to it, can be done atomically. On return, we re-set the interrupt level to its original state, in case we are called with interrupts disabled. Similar to sleep(), but a little different.


sleep

public static void sleep()
Relinquish the CPU, because the current thread is blocked waiting on a synchronization variable (Semaphore, Lock, or Condition). Eventually, some thread will wake this thread up, and put it back on the ready queue, so that it can be re-scheduled. NOTE: if there are no threads on the ready queue, that means we have no thread to run. "Interrupt.idle" is called to signify that we should idle the CPU until the next I/O interrupt occurs (the only thing that could cause a thread to become ready to run). NOTE: we assume interrupts are already disabled, because it is called from the synchronization routines which must disable interrupts for atomicity. We need interrupts off so that there can't be a time slice between pulling the first thread off the ready list, and switching to it.


finish

public static void finish()
Called by a thread to terminate itself. A thread can't completely destroy itself, because it needs some resources (e.g. a stack) as long as it is running. So it is the responsibility of the next thread to run to finish the job.


setRandomYield

public static void setRandomYield(boolean on)
Called to turn on or off a timer that forces a context switch at random intervals.

Parameters:
on - True if the timer is to be turned on.