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