Changeset 21043
- Timestamp:
- 02/23/08 08:24:22 (7 months ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
juggler/trunk/modules/vapor/vpr/Thread/ThreadPool.cpp
r21042 r21043 65 65 // --------------------------------------------------------------------------- 66 66 ThreadPool::ThreadPool(const int numToStartWith) 67 : readyThreads(0)67 : mReadyThreads(0) 68 68 { 69 69 //DebugLock.acquire(); … … 75 75 //DebugLock.release(); 76 76 77 listHead = NULL;78 workingCount = 0;79 listLock.release(); // release threadList80 finishedLock.release(); // Initialize if to threads being done77 mListHead = NULL; 78 mWorkingCount = 0; 79 mListLock.release(); // release threadList 80 mFinishedLock.release(); // Initialize if to threads being done 81 81 82 82 //-- Start the initial # of threads ---// … … 89 89 ThreadPool::~ThreadPool() 90 90 { 91 OneThread* cur_thread = listHead;91 OneThread* cur_thread = mListHead; 92 92 while ( cur_thread != NULL ) 93 93 { … … 115 115 << "[vpr::ThreadPool::threadLoop()] Entering." << std::endl 116 116 << vprDEBUG_FLUSH; 117 // vprDEBUG(vprDBG_ALL, vprDBG_HVERB_LVL) << Thread::self()118 // << " vpr::ThreadPool::threadLoop:theThreadAsVoid:"117 // vprDEBUG(vprDBG_ALL, vprDBG_HVERB_LVL) 118 // << "[vpr::ThreadPool::threadLoop()] theThreadAsVoid:" 119 119 // << theThreadAsVoid << endl << vprDEBUG_FLUSH; 120 120 // DebugLock.release(); 121 121 122 listLock.acquire();123 listLock.release(); // Do this to make sure addThread is done122 mListLock.acquire(); 123 mListLock.release(); // Do this to make sure addThread is done 124 124 125 125 for ( ;; ) … … 129 129 // ASSERT: We now have work to do... 130 130 // --- PROCESS ENTRY OVERHEAD --- // 131 workingCountLock.acquire(); // Get access to the working thread count132 if ( workingCount == 0 )131 mWorkingCountLock.acquire(); // Get access to the working thread count 132 if ( mWorkingCount == 0 ) 133 133 { 134 finishedLock.acquire(); // Now there are threads working134 mFinishedLock.acquire(); // Now there are threads working 135 135 } 136 ++ workingCount; // Update thread count137 workingCountLock.release();136 ++mWorkingCount; // Update thread count 137 mWorkingCountLock.release(); 138 138 139 139 // --- DO THE WORK --- // … … 141 141 142 142 // --- PROCESS EXIT OVERHEAD --- // 143 workingCountLock.acquire(); // Get access to the working count144 -- workingCount;145 if ( workingCount == 0 )143 mWorkingCountLock.acquire(); // Get access to the working count 144 --mWorkingCount; 145 if ( mWorkingCount == 0 ) 146 146 { 147 finishedLock.release(); // Now there are no threads working147 mFinishedLock.release(); // Now there are no threads working 148 148 } 149 workingCountLock.release();149 mWorkingCountLock.release(); 150 150 } 151 151 } … … 157 157 void ThreadPool::threadSleep(OneThread* theThread) 158 158 { 159 listLock.acquire();// acquire exclusive rights to threadList160 theThread->next = listHead;// put self on head of the list161 listHead = theThread;162 listLock.release();// release threadList163 164 readyThreads.release();// notify master, at least 1 on the list159 mListLock.acquire(); // acquire exclusive rights to threadList 160 theThread->next = mListHead; // put self on head of the list 161 mListHead = theThread; 162 mListLock.release(); // release threadList 163 164 mReadyThreads.release(); // notify master, at least 1 on the list 165 165 166 166 theThread->threadWait.acquire(); // sleep until master needs/releases me … … 174 174 OneThread* ThreadPool::getThread() 175 175 { 176 OneThread* the Thread;177 178 readyThreads.acquire();// wait until at least 1 thread is free179 180 listLock.acquire();// acquire exclusive rights to threadList181 the Thread = listHead; // get address of first free OneThread182 listHead = theThread->next; // make next in list, the head of list183 listLock.release();// release threadList184 185 return the Thread;176 OneThread* the_thread; 177 178 mReadyThreads.acquire(); // wait until at least 1 thread is free 179 180 mListLock.acquire(); // acquire exclusive rights to threadList 181 the_thread = mListHead; // get address of first free OneThread 182 mListHead = the_thread->next; // make next in list, the head of list 183 mListLock.release(); // release threadList 184 185 return the_thread; 186 186 } 187 187 … … 190 190 void ThreadPool::printList() const 191 191 { 192 OneThread* cur Thread = listHead;193 int counter = 0;192 OneThread* cur_thread(mListHead); 193 int counter(0); 194 194 195 195 std::cerr << "----- Thread List -----\n"; 196 196 197 while ( cur Thread != NULL )197 while ( cur_thread != NULL ) 198 198 { 199 199 std::cerr << "Thread: " << counter++ << std::endl; 200 std::cerr << "\tpid: " << *cur Thread << std::endl;201 cur Thread = curThread->next;200 std::cerr << "\tpid: " << *cur_thread << std::endl; 201 cur_thread = cur_thread->next; 202 202 } 203 203 } … … 214 214 // DebugLock.release(); 215 215 216 Guard<Mutex> guard( listLock); // Protect the head216 Guard<Mutex> guard(mListLock); // Protect the head 217 217 218 218 //OneThread* newThread = new (this->getMyMemPool()->allocate(sizeof(OneThread))) OneThread; // Used placement new … … 229 229 // DebugLock.release(); 230 230 231 return listHead;231 return mListHead; 232 232 } 233 233 juggler/trunk/modules/vapor/vpr/Thread/ThreadPool.h
r21042 r21043 133 133 void wait() 134 134 { 135 finishedLock.acquire(); // Get the lock that means threads done136 finishedLock.release(); // Reset it to done135 mFinishedLock.acquire(); // Get the lock that means threads done 136 mFinishedLock.release(); // Reset it to done 137 137 } 138 138 … … 140 140 141 141 private: 142 Semaphore readyThreads; /**< Count represents threads ready to work */143 Mutex listLock; /**< Mutex control of threadList head */144 Mutex workingCountLock; /**< Mutex on thread count */145 Mutex finishedLock; /**< Lock for wether thread are finished, lock -> doing work */146 OneThread* listHead; /**< First ready vpr::OneThread */147 volatile int workingCount; /**< Number of threads currently doing work */142 Semaphore mReadyThreads; /**< Count represents threads ready to work */ 143 Mutex mListLock; /**< Mutex control of threadList head */ 144 Mutex mWorkingCountLock; /**< Mutex on thread count */ 145 Mutex mFinishedLock; /**< Lock for wether thread are finished, lock -> doing work */ 146 OneThread* mListHead; /**< First ready vpr::OneThread */ 147 volatile int mWorkingCount; /**< Number of threads currently doing work */ 148 148 }; 149 149
