[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" |
---|
| 30 | #include <cassert> |
---|
| 31 | |
---|
| 32 | namespace orxonox |
---|
| 33 | { |
---|
[3231] | 34 | |
---|
[3226] | 35 | ThreadPool::ThreadPool() |
---|
| 36 | { |
---|
| 37 | } |
---|
| 38 | |
---|
| 39 | ThreadPool::~ThreadPool() |
---|
| 40 | { |
---|
| 41 | } |
---|
| 42 | |
---|
| 43 | void ThreadPool::addThreads( unsigned int nr ) |
---|
| 44 | { |
---|
| 45 | for( unsigned int i=0; i<nr; i++ ) |
---|
| 46 | this->threadPool_.push_back(Thread()); |
---|
| 47 | } |
---|
| 48 | unsigned int ThreadPool::removeThreads( unsigned int nr ) |
---|
| 49 | { |
---|
| 50 | unsigned int i=0; |
---|
| 51 | std::vector<Thread>::iterator it; |
---|
| 52 | for( it = this->threadPool_.begin(); it != threadPool_.end() && i<nr; ++it ) |
---|
| 53 | { |
---|
| 54 | if( ! it->isWorking() ) |
---|
| 55 | { |
---|
| 56 | this->threadPool_.erase( it++ ); |
---|
| 57 | ++i; |
---|
| 58 | } |
---|
| 59 | } |
---|
[3231] | 60 | return i; |
---|
[3226] | 61 | } |
---|
| 62 | unsigned int ThreadPool::setNrOfThreads( unsigned int nr ) |
---|
| 63 | { |
---|
| 64 | unsigned int currentsize = this->threadPool_.size(); |
---|
| 65 | if ( nr < currentsize ) |
---|
| 66 | return currentsize - removeThreads( currentsize - nr ); |
---|
| 67 | else if ( nr == currentsize ) |
---|
| 68 | return currentsize; |
---|
| 69 | else |
---|
| 70 | { |
---|
| 71 | addThreads( nr - currentsize ); |
---|
| 72 | return nr; |
---|
| 73 | } |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | bool ThreadPool::passFunction( Functor* functor, bool addThread ) |
---|
| 77 | { |
---|
| 78 | std::vector<Thread>::iterator it; |
---|
| 79 | for ( it=this->threadPool_.begin(); it!=this->threadPool_.end(); ++it ) |
---|
| 80 | { |
---|
| 81 | if ( ! it->isWorking() ) |
---|
| 82 | { |
---|
| 83 | bool b = it->evaluateFunctor( functor ); |
---|
| 84 | assert(b); // if b is false then there is some code error |
---|
| 85 | return true; |
---|
| 86 | } |
---|
| 87 | } |
---|
| 88 | if ( addThread ) |
---|
| 89 | { |
---|
| 90 | addThreads( 1 ); |
---|
| 91 | this->threadPool_.back().evaluateFunctor( functor ); // access the last element |
---|
| 92 | return true; |
---|
| 93 | } |
---|
| 94 | else |
---|
| 95 | return false; |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | void ThreadPool::synchronise() |
---|
| 99 | { |
---|
| 100 | std::vector<Thread>::iterator it; |
---|
| 101 | for ( it=this->threadPool_.begin(); it!=this->threadPool_.end(); ++it ) |
---|
| 102 | { |
---|
| 103 | it->waitUntilFinished(); |
---|
| 104 | } |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | } |
---|