tac0S
Template Affectional Command Operating System
thread.h
Go to the documentation of this file.
1 
39 #ifndef THREAD_H
40 #define THREAD_H
41 
42 #include "copyright.h"
43 #include "utility.h"
44 
45 #ifdef USER_PROGRAM
46 #include "machine.h"
47 #include "addrspace.h"
48 #endif
49 
53 #define MachineStateSize 18
54 
55 
58 #define StackSize (4 * 1024) // in words
59 
60 
63 { JUST_CREATED, RUNNING, READY, BLOCKED };
64 
66 extern void ThreadPrint (int arg);
67 
78 
79 class Thread
80 {
81  private:
82  // NOTE: DO NOT CHANGE the order of these first two members.
83  // THEY MUST be in this position for SWITCH to work.
84 
86  int *stackTop;
87  int machineState[MachineStateSize]; // all registers except for stackTop
88 
89  public:
90  Thread (const char *debugName); // initialize a Thread
91  ~Thread (); // deallocate a Thread
92  // NOTE -- thread being deleted
93  // must not be running when delete
94  // is called
95 
96  // basic thread operations
97 
98  void Fork (VoidFunctionPtr func, int arg); // Make thread run (*func)(arg)
99  void Yield (); // Relinquish the CPU if any
100  // other thread is runnable
101  void Sleep (); // Put the thread to sleep and
102  // relinquish the processor
103  void Finish (); // The thread is done executing
104 
105  void CheckOverflow (); // Check if thread has
106  // overflowed its stack
107  void setStatus (ThreadStatus st);
108  const char *getName ()
109  {
110  return (name);
111  }
112  void Print ()
113  {
114  printf ("%s, ", name);
115  }
116 
117  void setUserThread(void * userThreadAdress);
118  void * getUserThreadAdress();
119  ThreadStatus getStatus();
120 
121  void enterCritique();
122  void enterCritiqueExt();
123  void exitCritique();
124 
125  bool stopped = false;
126  bool inAMutex =false;
127  private:
128  // some of the private data for this class is listed above
129  void * critique;
130 
131  int *stack; // Bottom of the stack
132  // NULL if this is the main thread
133  // (If NULL, don't deallocate stack)
134  ThreadStatus status; // ready, running or blocked
135  const char *name;
136 
137  void StackAllocate (VoidFunctionPtr func, int arg);
138  // Allocate a stack for thread.
139  // Used internally by Fork()
140  void * userthread;
141 
142 #ifdef USER_PROGRAM
143 // A thread running a user program actually has *two* sets of CPU registers --
144 // one for its state while executing user code, one for its state
145 // while executing kernel code.
146 
147  int userRegisters[NumTotalRegs]; // user-level CPU register state
148 
149  public:
150  void SaveUserState (); // save user-level register state
151  void RestoreUserState (); // restore user-level register state
152 
153  AddrSpace *space; // User code this thread is running.
154 #endif
155 };
156 
157 // Magical machine-dependent routines, defined in switch.s
158 
159 extern "C"
160 {
161 // First frame on thread execution stack;
162 // enable interrupts
163 // call "func"
164 // (when func returns, if ever) call ThreadFinish()
165  void ThreadRoot ();
166 
167 // Stop running oldThread and start running newThread
168  void SWITCH (Thread * oldThread, Thread * newThread);
169 }
170 
171 #endif // THREAD_H
172 
173 #include "synch.h"
void Yield()
Thread::Yield Relinquish the CPU if any other thread is ready to run. If so, put the thread on the en...
Definition: thread.cc:203
Data structures to keep track of executing user programs.
void ThreadPrint(int arg)
external function, dummy routine whose sole job is to call Thread::Print
Definition: thread.cc:336
void Sleep()
Thread::Sleep Relinquish the CPU, because the current thread is blocked waiting on a synchronization ...
Definition: thread.cc:246
The following class defines a "thread control block" – which represents a single thread of execution...
Definition: thread.h:79
Definition: addrspace.h:24
void CheckOverflow()
Thread::CheckOverflow Check a thread's stack to see if it has overrun the space that has been allocat...
Definition: thread.cc:140
void enterCritiqueExt()
Thread::enterCritiqueExt From exterior of a thread Get a semaphore for "critic area" code with still ...
Definition: thread.cc:503
#define MachineStateSize
CPU register state to be saved on context switch. The SPARC and MIPS only need 10 registers...
Definition: thread.h:53
ThreadStatus
Thread state.
Definition: thread.h:62
void enterCritique()
Thread::enterCritique Get a semaphore for "critic area" code with still having interrupt.
Definition: thread.cc:492
void setStatus(ThreadStatus st)
Thread::setStatus Set the status as in parameter with debug info.
Definition: thread.cc:479
void exitCritique()
Thread::exitCritique Drop the semaphore for the critique zone of code, see Thread::enterCritique.
Definition: thread.cc:512
Miscellaneous useful definitions, including debugging routines.
~Thread()
Thread::~Thread De-allocate a thread.
Definition: thread.cc:67
void Fork(VoidFunctionPtr func, int arg)
Thread::Fork Invoke (*func)(arg), allowing caller and callee to execute concurrently.
Definition: thread.cc:99
Data structures for synchronizing threads.
Data structures for simulating the execution of user programs running on top of Nachos.
void Finish()
Thread::Finish Called by ThreadRoot when a thread is done executing the forked procedure.
Definition: thread.cc:167
Thread(const char *debugName)
Thread::Thread Initialize a thread control block, so that we can then call Thread::Fork.
Definition: thread.cc:37