nachos.machine
Class NachosThread

java.lang.Object
  extended by nachos.machine.NachosThread

public class NachosThread
extends java.lang.Object

Nachos threads class. Threads are the units of concurrent activity in an operating system, and support for threads would usually be found in one of the lowest layers of the kernel. In a real operating system, a thread is represented as an object that has space to hold a "snapshot" of the state of the thread when it is not running. This snapshot includes the contents of the CPU registers (including the program counter and stack pointer), and a reference to a region of memory private to the thread that it uses as its stack. Switching the CPU between threads requires direct access to the CPU registers. Since this is not usually possible in a higher-level programming language, the context switch routine is generally programmed in assembly language. In Java, there isn't any way for us to manipulate registers and stacks so that we can implement our own threads support from scratch. That's too bad, because it is very instructive to see how this is actually accomplished. But since we can't do our own threads implementation, we just implement Nachos threads on top of the thread support that already exists in Java.


Field Summary
static int BLOCKED
          Status of a thread that is waiting for some event to occur.
static int JUST_CREATED
          Status of a thread that has just been created and is not yet runnable.
static int READY
          Status of a thread that is ready to use the CPU but not now running.
static int RUNNING
          Status of the thread that is currently using the CPU.
static int TERMINATED
          Setting the status of a thread to this causes it to terminate.
 
Constructor Summary
NachosThread(java.lang.String threadName)
          Create a new Nachos thread object, without specifying an entry point.
NachosThread(java.lang.String threadName, java.lang.Runnable runObj)
          Create a new Nachos thread object.
 
Method Summary
 java.lang.String getName()
          Get the name of this thread.
 int getStatus()
          Method used to get the current status of a thread.
 void restoreState()
          Restore the state of a thread on a context switch.
 void saveState()
          Save the state of a thread on a context switch.
 void setRunnable(java.lang.Runnable runObj)
          This method is used to set the entry point for a NachosThread.
 void setStatus(int st)
          Method used to set the current status (ready, running, blocked, etc.) of a thread.
 void switchTo(NachosThread newThread)
          This method is used by the Scheduler to switch execution to a new thread.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

JUST_CREATED

public static final int JUST_CREATED
Status of a thread that has just been created and is not yet runnable.

See Also:
Constant Field Values

RUNNING

public static final int RUNNING
Status of the thread that is currently using the CPU.

See Also:
Constant Field Values

READY

public static final int READY
Status of a thread that is ready to use the CPU but not now running.

See Also:
Constant Field Values

BLOCKED

public static final int BLOCKED
Status of a thread that is waiting for some event to occur.

See Also:
Constant Field Values

TERMINATED

public static final int TERMINATED
Setting the status of a thread to this causes it to terminate.

See Also:
Constant Field Values
Constructor Detail

NachosThread

public NachosThread(java.lang.String threadName,
                    java.lang.Runnable runObj)
Create a new Nachos thread object. The thread does not actually begin running until it is given control via switchTo().

Parameters:
threadName - An arbitrary name, useful for debugging.
runObj - Execution of the thread will begin with the run() method of this object.

NachosThread

public NachosThread(java.lang.String threadName)
Create a new Nachos thread object, without specifying an entry point. The thread does not actually begin running until it is given control via switchTo(). Before then, it is necessary to set the entry point for the thread by calling setRunnable(). The reason this method has to be provided is that if one tries to create a subclass of NachosThread, restrictions imposed by Java on the use of "this" before super() has been called make it impossible for the subclass to set the entry point within the constructor of the subclass.

Parameters:
threadName - An arbitrary name, useful for debugging.
Method Detail

setStatus

public void setStatus(int st)
Method used to set the current status (ready, running, blocked, etc.) of a thread. NOTE: This method should not be used to set the state of a thread to RUNNING. Rather, the switchTo() method should be used for this purpose.

Parameters:
st - The new status of the thread.

getStatus

public int getStatus()
Method used to get the current status of a thread.

Returns:
The current status of the thread.

saveState

public void saveState()
Save the state of a thread on a context switch. This is called by the scheduler as part of a context switch. In a real operating system this method would be responsible for saving the current contents of the CPU registers, in preparation for switching the CPU to a new thread. Here there really isn't anything to do (it's done by Java), but we include this method anyway to make the simulation appear more realistic.


restoreState

public void restoreState()
Restore the state of a thread on a context switch. This undoes what saveState() does.


setRunnable

public void setRunnable(java.lang.Runnable runObj)
This method is used to set the entry point for a NachosThread. If a "runObj" was not previously provided as a parameter to the constructor, then this method must be called before attempting to switchTo() the thread for the first time.

Parameters:
runObj - Execution of the thread will begin with the run() method of this object.

switchTo

public void switchTo(NachosThread newThread)
This method is used by the Scheduler to switch execution to a new thread. Normally in an OS this switching of threads is done by an assembly language routine that saves the program counter and stack pointer of the old thread, then restores the program counter and stack pointer of the new thread. Since we are emulating Nachos threads using Java threads, we accomplish the switch by using the Java synchronization primitives to cause the old thread to block and the new thread to wake up.

Parameters:
newThread - The thread to switch to.

getName

public java.lang.String getName()
Get the name of this thread.

Returns:
The name of the thread.