Main Page   Compound List   File List   Compound Members   File Members  

Thread Class Reference

#include <thread.h>

List of all members.

Public Methods

 Thread (char *debugName)
 ~Thread ()
void Fork (VoidFunctionPtr func, int arg)
void Yield ()
void Sleep ()
void Finish ()
void CheckOverflow ()
void setStatus (ThreadStatus st)
char * getName ()
void Print ()

Private Methods

void StackAllocate (VoidFunctionPtr func, int arg)

Private Attributes

int * stackTop
int machineState [MachineStateSize]
int * stack
ThreadStatus status
char * name


Constructor & Destructor Documentation

Thread::Thread char *    debugName
 

Definition at line 35 of file thread.cc.

References JUST_CREATED, name, NULL, stack, stackTop, and status.

00036 {
00037     name = threadName;
00038     stackTop = NULL;
00039     stack = NULL;
00040     status = JUST_CREATED;
00041 #ifdef USER_PROGRAM
00042     space = NULL;
00043 #endif
00044 }

Thread::~Thread  
 

Definition at line 58 of file thread.cc.

References ASSERT, DeallocBoundedArray, DEBUG, name, NULL, and stack.

00059 {
00060     DEBUG('t', "Deleting thread \"%s\"\n", name);
00061 
00062     ASSERT(this != currentThread);
00063     if (stack != NULL)
00064         DeallocBoundedArray((char *) stack, StackSize * sizeof(int));
00065 }


Member Function Documentation

void Thread::CheckOverflow  
 

Definition at line 117 of file thread.cc.

References ASSERT, NULL, stack, and STACK_FENCEPOST.

Referenced by Scheduler::Run.

00118 {
00119     if (stack != NULL)
00120 #ifdef HOST_SNAKE                       // Stacks grow upward on the Snakes
00121         ASSERT(stack[StackSize - 1] == STACK_FENCEPOST);
00122 #else
00123         ASSERT(*stack == (int) STACK_FENCEPOST);
00124 #endif
00125 }

void Thread::Finish  
 

Definition at line 144 of file thread.cc.

References ASSERT, DEBUG, getName, IntOff, and Sleep.

00145 {
00146     (void) interrupt->SetLevel(IntOff);         
00147     ASSERT(this == currentThread);
00148     
00149     DEBUG('t', "Finishing thread \"%s\"\n", getName());
00150     
00151     threadToBeDestroyed = currentThread;
00152     Sleep();                                    // invokes SWITCH
00153     // not reached
00154 }

void Thread::Fork VoidFunctionPtr    func,
int    arg
 

Definition at line 88 of file thread.cc.

References DEBUG, IntOff, IntStatus, name, StackAllocate, and VoidFunctionPtr.

Referenced by PostOffice::PostOffice, and ThreadTest.

00089 {
00090     DEBUG('t', "Forking thread \"%s\" with func = 0x%x, arg = %d\n",
00091           name, (int) func, arg);
00092     
00093     StackAllocate(func, arg);
00094 
00095     IntStatus oldLevel = interrupt->SetLevel(IntOff);
00096     scheduler->ReadyToRun(this);        // ReadyToRun assumes that interrupts 
00097                                         // are disabled!
00098     (void) interrupt->SetLevel(oldLevel);
00099 }    

char* Thread::getName   [inline]
 

Definition at line 102 of file thread.h.

Referenced by Finish, Scheduler::ReadyToRun, Scheduler::Run, Sleep, and Yield.

00102 { return (name); }

void Thread::Print   [inline]
 

Definition at line 103 of file thread.h.

References name.

Referenced by ThreadPrint.

00103 { printf("%s, ", name); }

void Thread::setStatus ThreadStatus    st [inline]
 

Definition at line 101 of file thread.h.

References status, and ThreadStatus.

Referenced by Initialize, Scheduler::ReadyToRun, and Scheduler::Run.

00101 { status = st; }

void Thread::Sleep  
 

Definition at line 212 of file thread.cc.

References ASSERT, BLOCKED, DEBUG, getName, IntOff, NULL, and status.

Referenced by Finish.

00213 {
00214     Thread *nextThread;
00215     
00216     ASSERT(this == currentThread);
00217     ASSERT(interrupt->getLevel() == IntOff);
00218     
00219     DEBUG('t', "Sleeping thread \"%s\"\n", getName());
00220 
00221     status = BLOCKED;
00222     while ((nextThread = scheduler->FindNextToRun()) == NULL)
00223         interrupt->Idle();      // no one to run, wait for an interrupt
00224         
00225     scheduler->Run(nextThread); // returns when we've been signalled
00226 }

void Thread::StackAllocate VoidFunctionPtr    func,
int    arg
[private]
 

Definition at line 253 of file thread.cc.

References AllocBoundedArray, InterruptEnable, machineState, stack, STACK_FENCEPOST, stackTop, ThreadFinish, ThreadRoot, and VoidFunctionPtr.

Referenced by Fork.

00254 {
00255     stack = (int *) AllocBoundedArray(StackSize * sizeof(int));
00256 
00257 #ifdef HOST_SNAKE
00258     // HP stack works from low addresses to high addresses
00259     stackTop = stack + 16;      // HP requires 64-byte frame marker
00260     stack[StackSize - 1] = STACK_FENCEPOST;
00261 #else
00262     // i386 & MIPS & SPARC stack works from high addresses to low addresses
00263 #ifdef HOST_SPARC
00264     // SPARC stack must contains at least 1 activation record to start with.
00265     stackTop = stack + StackSize - 96;
00266 #else  // HOST_MIPS  || HOST_i386
00267     stackTop = stack + StackSize - 4;   // -4 to be on the safe side!
00268 #ifdef HOST_i386
00269     // the 80386 passes the return address on the stack.  In order for
00270     // SWITCH() to go to ThreadRoot when we switch to this thread, the
00271     // return addres used in SWITCH() must be the starting address of
00272     // ThreadRoot.
00273     *(--stackTop) = (int)ThreadRoot;
00274 #endif
00275 #endif  // HOST_SPARC
00276     *stack = STACK_FENCEPOST;
00277 #endif  // HOST_SNAKE
00278     
00279     machineState[PCState] = (int) ThreadRoot;
00280     machineState[StartupPCState] = (int) InterruptEnable;
00281     machineState[InitialPCState] = (int) func;
00282     machineState[InitialArgState] = arg;
00283     machineState[WhenDonePCState] = (int) ThreadFinish;
00284 }

void Thread::Yield  
 

Definition at line 175 of file thread.cc.

References ASSERT, DEBUG, getName, IntOff, IntStatus, and NULL.

00176 {
00177     Thread *nextThread;
00178     IntStatus oldLevel = interrupt->SetLevel(IntOff);
00179     
00180     ASSERT(this == currentThread);
00181     
00182     DEBUG('t', "Yielding thread \"%s\"\n", getName());
00183     
00184     nextThread = scheduler->FindNextToRun();
00185     if (nextThread != NULL) {
00186         scheduler->ReadyToRun(this);
00187         scheduler->Run(nextThread);
00188     }
00189     (void) interrupt->SetLevel(oldLevel);
00190 }


Member Data Documentation

int Thread::machineState[MachineStateSize] [private]
 

Definition at line 81 of file thread.h.

Referenced by StackAllocate.

char* Thread::name [private]
 

Definition at line 112 of file thread.h.

Referenced by Fork, Print, Thread, and ~Thread.

int* Thread::stack [private]
 

Definition at line 108 of file thread.h.

Referenced by CheckOverflow, StackAllocate, Thread, and ~Thread.

int* Thread::stackTop [private]
 

Definition at line 80 of file thread.h.

Referenced by StackAllocate, and Thread.

ThreadStatus Thread::status [private]
 

Definition at line 111 of file thread.h.

Referenced by setStatus, Sleep, and Thread.


The documentation for this class was generated from the following files:
Generated on Mon Feb 10 09:54:59 2003 for nachos by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002
The University of Southern California does not screen or control the content on this website and thus does not guarantee the accuracy, integrity, or quality of such content. All content on this website is provided by and is the sole responsibility of the person from which such content originated, and such content does not necessarily reflect the opinions of the University administration or the Board of Trustees