[3226] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
| 3 | * > www.orxonox.net < |
---|
| 4 | * |
---|
| 5 | * |
---|
| 6 | * License notice: |
---|
| 7 | * |
---|
| 8 | * This program is free software; you can redistribute it and/or |
---|
| 9 | * modify it under the terms of the GNU General Public License |
---|
| 10 | * as published by the Free Software Foundation; either version 2 |
---|
| 11 | * of the License, or (at your option) any later version. |
---|
| 12 | * |
---|
| 13 | * This program is distributed in the hope that it will be useful, |
---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 16 | * GNU General Public License for more details. |
---|
| 17 | * |
---|
| 18 | * You should have received a copy of the GNU General Public License |
---|
| 19 | * along with this program; if not, write to the Free Software |
---|
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
| 21 | * |
---|
| 22 | * Author: |
---|
| 23 | * Oliver Scheuss |
---|
| 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | #include "ThreadPool.h" |
---|
[8373] | 30 | |
---|
| 31 | #include "util/OrxAssert.h" |
---|
[3240] | 32 | #include "Thread.h" |
---|
[3226] | 33 | |
---|
| 34 | namespace orxonox |
---|
| 35 | { |
---|
[3231] | 36 | |
---|
[3226] | 37 | ThreadPool::ThreadPool() |
---|
| 38 | { |
---|
| 39 | } |
---|
[6417] | 40 | |
---|
[3226] | 41 | ThreadPool::~ThreadPool() |
---|
| 42 | { |
---|
[8394] | 43 | OrxVerify(this->setNrOfThreads(0) == 0, "ERROR: could not join remaining threads in ThreadPool" ); |
---|
[3226] | 44 | } |
---|
[6417] | 45 | |
---|
[3226] | 46 | void ThreadPool::addThreads( unsigned int nr ) |
---|
| 47 | { |
---|
| 48 | for( unsigned int i=0; i<nr; i++ ) |
---|
[3240] | 49 | this->threadPool_.push_back(new Thread()); |
---|
[3226] | 50 | } |
---|
| 51 | unsigned int ThreadPool::removeThreads( unsigned int nr ) |
---|
| 52 | { |
---|
| 53 | unsigned int i=0; |
---|
[3240] | 54 | std::vector<Thread*>::iterator it; |
---|
| 55 | for( it = this->threadPool_.begin(); it != threadPool_.end() && i<nr; ) |
---|
[3226] | 56 | { |
---|
[3240] | 57 | if( ! (*it)->isWorking() ) |
---|
[3226] | 58 | { |
---|
[3240] | 59 | Thread* temp = *it; |
---|
| 60 | it=this->threadPool_.erase( it ); |
---|
| 61 | delete temp; |
---|
[3226] | 62 | ++i; |
---|
| 63 | } |
---|
[3240] | 64 | else |
---|
| 65 | ++it; |
---|
[3226] | 66 | } |
---|
[3231] | 67 | return i; |
---|
[3226] | 68 | } |
---|
| 69 | unsigned int ThreadPool::setNrOfThreads( unsigned int nr ) |
---|
| 70 | { |
---|
| 71 | unsigned int currentsize = this->threadPool_.size(); |
---|
| 72 | if ( nr < currentsize ) |
---|
| 73 | return currentsize - removeThreads( currentsize - nr ); |
---|
| 74 | else if ( nr == currentsize ) |
---|
| 75 | return currentsize; |
---|
| 76 | else |
---|
| 77 | { |
---|
| 78 | addThreads( nr - currentsize ); |
---|
| 79 | return nr; |
---|
| 80 | } |
---|
| 81 | } |
---|
[6417] | 82 | |
---|
[7284] | 83 | bool ThreadPool::passFunction( const ExecutorPtr& executor, bool addThread ) |
---|
[3226] | 84 | { |
---|
[3240] | 85 | std::vector<Thread*>::iterator it; |
---|
[3226] | 86 | for ( it=this->threadPool_.begin(); it!=this->threadPool_.end(); ++it ) |
---|
| 87 | { |
---|
[3240] | 88 | if ( ! (*it)->isWorking() ) |
---|
[3226] | 89 | { |
---|
[8373] | 90 | // If that fails, then there is some code error |
---|
[8394] | 91 | OrxVerify( (*it)->evaluateExecutor( executor ), "ERROR: could not evaluate Executor" ); |
---|
[3226] | 92 | return true; |
---|
| 93 | } |
---|
| 94 | } |
---|
| 95 | if ( addThread ) |
---|
| 96 | { |
---|
| 97 | addThreads( 1 ); |
---|
[8394] | 98 | OrxVerify( this->threadPool_.back()->evaluateExecutor( executor ), "ERROR: could not evaluate Executor" ); // access the last element |
---|
[3226] | 99 | return true; |
---|
| 100 | } |
---|
| 101 | else |
---|
| 102 | return false; |
---|
| 103 | } |
---|
[6417] | 104 | |
---|
[3226] | 105 | void ThreadPool::synchronise() |
---|
| 106 | { |
---|
[3240] | 107 | std::vector<Thread*>::iterator it; |
---|
[3226] | 108 | for ( it=this->threadPool_.begin(); it!=this->threadPool_.end(); ++it ) |
---|
| 109 | { |
---|
[3240] | 110 | (*it)->waitUntilFinished(); |
---|
[3226] | 111 | } |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | } |
---|