nachos.threads
Class KThread

java.lang.Object
  extended by nachos.threads.KThread
Direct Known Subclasses:
UThread

public class KThread
extends Object

A KThread is a thread that can be used to execute Nachos kernel code. Nachos allows multiple threads to run concurrently. To create a new thread of execution, first declare a class that implements the Runnable interface. That class then implements the run method. An instance of the class can then be allocated, passed as an argument when creating KThread, and forked. For example, a thread that computes pi could be written as follows:

 class PiRun implements Runnable {
     public void run() {
         // compute pi
         ...
     }
 }
 

The following code would then create a thread and start it running:

 PiRun p = new PiRun();
 new KThread(p).fork();
 


Field Summary
 Object schedulingState
          Additional state used by schedulers.
 
Constructor Summary
KThread()
          Allocate a new KThread.
KThread(Runnable target)
          Allocate a new KThread.
 
Method Summary
 int compareTo(Object o)
          Deterministically and consistently compare this thread to another thread.
static KThread currentThread()
          Get the current thread.
static void finish()
          Finish the current thread and schedule it to be destroyed when it is safe to do so.
 void fork()
          Causes this thread to begin execution.
 String getName()
          Get the name of this thread.
 void join()
          Waits for this thread to finish.
 void ready()
          Moves this thread to the ready state and adds this to the scheduler's ready queue.
protected  void restoreState()
          Prepare this thread to be run.
protected  void saveState()
          Prepare this thread to give up the processor.
static void selfTest()
          Tests whether this module is working.
 KThread setName(String name)
          Set the name of this thread.
 KThread setTarget(Runnable target)
          Set the target of this thread.
static void sleep()
          Relinquish the CPU, because the current thread has either finished or it is blocked.
 String toString()
          Get the full name of this 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, wait, wait, wait
 

Field Detail

schedulingState

public Object schedulingState
Additional state used by schedulers.

See Also:
PriorityScheduler.ThreadState
Constructor Detail

KThread

public KThread()
Allocate a new KThread. If this is the first KThread, create an idle thread as well.


KThread

public KThread(Runnable target)
Allocate a new KThread.

Parameters:
target - the object whose run method is called.
Method Detail

currentThread

public static KThread currentThread()
Get the current thread.

Returns:
the current thread.

setTarget

public KThread setTarget(Runnable target)
Set the target of this thread.

Parameters:
target - the object whose run method is called.
Returns:
this thread.

setName

public KThread setName(String name)
Set the name of this thread. This name is used for debugging purposes only.

Parameters:
name - the name to give to this thread.
Returns:
this thread.

getName

public String getName()
Get the name of this thread. This name is used for debugging purposes only.

Returns:
the name given to this thread.

toString

public String toString()
Get the full name of this thread. This includes its name along with its numerical ID. This name is used for debugging purposes only.

Overrides:
toString in class Object
Returns:
the full name given to this thread.

compareTo

public int compareTo(Object o)
Deterministically and consistently compare this thread to another thread.


fork

public void fork()
Causes this thread to begin execution. The result is that two threads are running concurrently: the current thread (which returns from the call to the fork method) and the other thread (which executes its target's run method).


finish

public static void finish()
Finish the current thread and schedule it to be destroyed when it is safe to do so. This method is automatically called when a thread's run method returns, but it may also be called directly. The current thread cannot be immediately destroyed because its stack and other execution state are still in use. Instead, this thread will be destroyed automatically by the next thread to run, when it is safe to delete this thread.


yield

public static void yield()
Relinquish the CPU if any other thread is ready to run. If so, put the current thread on the ready queue, so that it will eventually be rescheuled.

Returns immediately if no other thread is ready to run. Otherwise returns when the current thread is chosen to run again by readyQueue.nextThread().

Interrupts are disabled, so that the current thread can atomically add itself to the ready queue and switch to the next thread. On return, restores interrupts to the previous state, in case yield() was called with interrupts disabled.


sleep

public static void sleep()
Relinquish the CPU, because the current thread has either finished or it is blocked. This thread must be the current thread.

If the current thread is blocked (on a synchronization primitive, i.e. a Semaphore, Lock, or Condition), eventually some thread will wake this thread up, putting it back on the ready queue so that it can be rescheduled. Otherwise, finish() should have scheduled this thread to be destroyed by the next thread to run.


ready

public void ready()
Moves this thread to the ready state and adds this to the scheduler's ready queue.


join

public void join()
Waits for this thread to finish. If this thread is already finished, return immediately. This method must only be called once; the second call is not guaranteed to return. This thread must not be the current thread.


restoreState

protected void restoreState()
Prepare this thread to be run. Set status to statusRunning and check toBeDestroyed.


saveState

protected void saveState()
Prepare this thread to give up the processor. Kernel threads do not need to do anything here.


selfTest

public static void selfTest()
Tests whether this module is working.