Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members  

Thread Class Reference

The following class defines a "thread control block" -- which represents a single thread of execution. Every thread has: an execution stack for activation records ("stackTop" and "stack") space to save CPU registers while not running ("machineState") a "status" (running/ready/blocked) Some threads also belong to a user address space; threads that only run in the kernel have a NULL address space. More...

#include <thread.h>

List of all members.

Public Methods

 Thread (char *debugName)
 initialize a Thread. More...

 ~Thread ()
 deallocate a Thread NOTE -- thread being deleted must not be running when delete is called. More...

void Fork (VoidFunctionPtr func, void *arg)
 Make thread run (*func)(arg). More...

void Yield ()
 Relinquish the CPU if any other thread is runnable. More...

void Sleep (bool finishing)
 Put the thread to sleep and relinquish the processor. More...

void Begin ()
 Startup code for the thread. More...

void Finish ()
 The thread is done executing. More...

void CheckOverflow ()
 Check if thread stack has overflowed. More...

void setStatus (ThreadStatus st)
char* getName ()
void Print ()
void SelfTest ()
 test whether thread impl is working. More...


Private Methods

void StackAllocate (VoidFunctionPtr func, void *arg)
 Allocate a stack for thread. Used internally by Fork(). More...


Private Attributes

int* stackTop
 the current stack pointer. More...

void* machineState [MachineStateSize]
 all registers except for stackTop. More...

int* stack
 Bottom of the stack NULL if this is the main thread (If NULL, don't deallocate stack). More...

ThreadStatus status
 ready, running or blocked. More...

char* name


Detailed Description

The following class defines a "thread control block" -- which represents a single thread of execution. Every thread has: an execution stack for activation records ("stackTop" and "stack") space to save CPU registers while not running ("machineState") a "status" (running/ready/blocked) Some threads also belong to a user address space; threads that only run in the kernel have a NULL address space.

Definition at line 79 of file thread.h.


Constructor & Destructor Documentation

Thread::Thread ( char * debugName )
 

initialize a Thread.

Thread::Thread Initialize a thread control block, so that we can then call Thread::Fork.

"threadName" is an arbitrary string, useful for debugging.

Definition at line 36 of file thread.cc.

Referenced by SelfTest().

Thread::~Thread ( )
 

deallocate a Thread NOTE -- thread being deleted must not be running when delete is called.

Thread::~Thread De-allocate a thread.

NOTE: the current thread *cannot* delete itself directly, since it is still running on the stack that we need to delete.

NOTE: if this is the main thread, we can't delete the stack because we didn't allocate it -- we got it automatically as part of starting up Nachos.

Definition at line 64 of file thread.cc.


Member Function Documentation

void Thread::Begin ( )
 

Startup code for the thread.

Thread::Begin Called by ThreadRoot when a thread is about to begin executing the forked procedure.

It's main responsibilities are:

1. deallocate the previously running thread if it finished (see Thread::Finish())

2. enable interrupts (so we can get time-sliced)

Definition at line 154 of file thread.cc.

void Thread::CheckOverflow ( )
 

Check if thread stack has overflowed.

Thread::CheckOverflow Check a thread's stack to see if it has overrun the space that has been allocated for it. If we had a smarter compiler, we wouldn't need to worry about this, but we don't.

NOTE: Nachos will not catch all stack overflow conditions. In other words, your program may still crash because of an overflow.

If you get bizarre results (such as seg faults where there is no code) then you *may* need to increase the stack size. You can avoid stack overflows by not putting large data structures on the stack. Don't do this: void foo() { int bigArray[10000]; ... }

Definition at line 129 of file thread.cc.

void Thread::Finish ( )
 

The thread is done executing.

Thread::Finish Called by ThreadRoot when a thread is done executing the forked procedure.

NOTE: we can't immediately de-allocate the thread data structure or the execution stack, because we're still running in the thread and we're still on the stack! Instead, we tell the scheduler to call the destructor, once it is running in the context of a different thread.

NOTE: we disable interrupts, because Sleep() assumes interrupts are disabled.

Definition at line 178 of file thread.cc.

Referenced by ThreadedKernel::Run().

void Thread::Fork ( VoidFunctionPtr func,
void * arg )
 

Make thread run (*func)(arg).

Thread::Fork Invoke (*func)(arg), allowing caller and callee to execute concurrently.

NOTE: although our definition allows only a single argument to be passed to the procedure, it is possible to pass multiple arguments by making them fields of a structure, and passing a pointer to the structure as "arg".

Implemented as the following steps: 1. Allocate a stack

2. Initialize the stack so that a call to SWITCH will cause it to run the procedure

3. Put the thread on the ready queue

"func" is the procedure to run concurrently.

"arg" is a single argument to be passed to the procedure.

Definition at line 97 of file thread.cc.

Referenced by ElevatorSelfTest(), PostOfficeInput::PostOfficeInput(), SelfTest(), SynchList::SelfTest(), and Semaphore::SelfTest().

void Thread::Print ( ) [inline]
 

Definition at line 107 of file thread.h.

Referenced by ThreadPrint().

void Thread::SelfTest ( )
 

test whether thread impl is working.

Thread::SelfTest Set up a ping-pong between two threads, by forking a thread to call SimpleThread, and then calling SimpleThread ourselves.

Definition at line 438 of file thread.cc.

Referenced by ThreadedKernel::SelfTest().

void Thread::Sleep ( bool finishing )
 

Put the thread to sleep and relinquish the processor.

Thread::Sleep Relinquish the CPU, because the current thread has either finished or is blocked waiting on a synchronization variable (Semaphore, Lock, or Condition). In the latter case, 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.

Definition at line 247 of file thread.cc.

Referenced by Finish(), and Semaphore::P().

void Thread::StackAllocate ( VoidFunctionPtr func,
void * arg ) [private]
 

Allocate a stack for thread. Used internally by Fork().

Thread::StackAllocate Allocate and initialize an execution stack. The stack is initialized with an initial stack frame for ThreadRoot, which:

enables interrupts

calls (*func)(arg)

calls Thread::Finish

"func" is the procedure to be forked

"arg" is the parameter to be passed to the procedure

Definition at line 316 of file thread.cc.

Referenced by Fork().

void Thread::Yield ( )
 

Relinquish the CPU if any other thread is runnable.

Thread::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 Thread::Sleep(), but a little different.

Definition at line 208 of file thread.cc.

char * Thread::getName ( ) [inline]
 

Definition at line 106 of file thread.h.

Referenced by Scheduler::ReadyToRun().

void Thread::setStatus ( ThreadStatus st ) [inline]
 

Definition at line 105 of file thread.h.

Referenced by ThreadedKernel::Initialize(), and Scheduler::ReadyToRun().


Member Data Documentation

void * Thread::machineState [private]
 

all registers except for stackTop.

Definition at line 84 of file thread.h.

char * Thread::name [private]
 

Definition at line 117 of file thread.h.

int * Thread::stack [private]
 

Bottom of the stack NULL if this is the main thread (If NULL, don't deallocate stack).

Definition at line 113 of file thread.h.

int * Thread::stackTop [private]
 

the current stack pointer.

Definition at line 83 of file thread.h.

ThreadStatus Thread::status [private]
 

ready, running or blocked.

Definition at line 116 of file thread.h.


The documentation for this class was generated from the following files:
Generated at Wed Jul 4 11:32:24 2001 for Nachos by doxygen1.2.8.1 written by Dimitri van Heesch, © 1997-2001