Main Page | Class Hierarchy | Class List | File List | Class Members | File Members

process.cpp

Go to the documentation of this file.
00001 00002 #include "process.h" 00003 00004 00005 00006 00007 00008 process::process(program& prog, processId pid) 00009 : m_pProgram(&prog) 00010 , m_id(pid) 00011 , m_threadCnt(1) 00012 { 00013 push_back(new thread(0)); 00014 } 00015 00016 00017 process::~process() 00018 { 00019 clean(); 00020 } 00021 00022 00023 void process::clean() 00024 { 00025 // delete all threads 00026 iterator tit = begin(); 00027 for( ; tit!=end();++tit) 00028 { 00029 if(*tit) delete (*tit); 00030 } 00031 00032 resize(0); 00033 00034 // clear all data 00035 m_threadCnt = m_id = 0; 00036 m_pProgram = NULL; 00037 } 00038 00039 00040 program& process::getProgram() 00041 { 00042 ASSERT(m_pProgram, "cannot manipulate terminated process"); 00043 return *m_pProgram; 00044 } 00045 00046 00047 processId process::getId() 00048 { 00049 return m_id; 00050 } 00051 00052 00053 threadId process::spawnThread(address addr) 00054 { 00055 // check if there is an unused thread 00056 if(m_threadCnt<size()) 00057 { 00058 // find the unused thread 00059 iterator it = begin(); 00060 for(uInt tid=0; it!=end(); ++tid, ++it) 00061 { 00062 if((*it)->isTerminated()) 00063 { 00064 // respawn the thread 00065 (*it)->spawn(addr); 00066 00067 m_threadCnt++; 00068 return tid+1; 00069 } 00070 } 00071 } 00072 00073 // create a new thread 00074 push_back(new thread(addr)); 00075 m_threadCnt++; 00076 00077 return static_cast<uInt>(size()); 00078 } 00079 00080 00081 void process::killThread(threadId tid) 00082 { 00083 // verify that the thread exists 00084 if(tid>0 && tid<=size() && !(*this)[tid-1]->m_terminated) 00085 { 00086 // terminate the thread 00087 (*this)[tid-1]->m_terminated=true; 00088 m_threadCnt--; 00089 } 00090 } 00091 00092 00093 uInt process::getNumThreads() 00094 { 00095 return m_threadCnt; 00096 } 00097 00098 00099 address process::getThreadAddress(threadId tid) 00100 { 00101 // verify that the thread exists 00102 if(tid>0 && tid<=size() && !(*this)[tid-1]->m_terminated) 00103 { 00104 // return its address 00105 thread &t = *((*this)[tid-1]); 00106 if(!t.isTerminated()) return t; 00107 } 00108 00109 return g_max_uInt; 00110 } 00111 00112 00113 00114 bool process::isTerminated() 00115 { 00116 return m_threadCnt==0 00117 || m_pProgram==NULL 00118 || m_pProgram->size()==0; 00119 } 00120 00121 00122 process::process() 00123 : m_pProgram(NULL) 00124 , m_id(0) 00125 { 00126 } 00127 00128 00129 00130 00131

Generated on Sun Mar 6 22:12:28 2005 for virtualMachine03 by doxygen 1.3.7