Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 1 | #include "ThreadManager.h" |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame^] | 2 | #include "Exception.h" |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 3 | #include "Monitor.h" |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 4 | |
| 5 | #include <assert.h> |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 6 | #include <queue> |
| 7 | #include <set> |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 8 | |
| 9 | namespace facebook { namespace thrift { namespace concurrency { |
| 10 | |
| 11 | /** ThreadManager class |
| 12 | |
| 13 | This class manages a pool of threads. It uses a ThreadFactory to create threads. It never actually creates or destroys worker threads, rather |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame^] | 14 | it maintains statistics on number of idle threads, number of active threads, task backlog, and average wait and service times. |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 15 | |
| 16 | @author marc |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 17 | @version $Id:$ */ |
| 18 | |
| 19 | class ThreadManager::Impl : public ThreadManager { |
| 20 | |
| 21 | public: |
| 22 | |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame^] | 23 | Impl() : _stopped(false) {} |
| 24 | |
| 25 | |
| 26 | |
| 27 | ~Impl() { |
| 28 | |
| 29 | if(!_stopped) { |
| 30 | stop(); |
| 31 | } |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 32 | } |
| 33 | |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame^] | 34 | void stop(); |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 35 | |
| 36 | const ThreadFactory* threadFactory() const { |
| 37 | |
| 38 | Synchronized s(_monitor); |
| 39 | |
| 40 | return _threadFactory; |
| 41 | } |
| 42 | |
| 43 | void threadFactory(const ThreadFactory* value) { |
| 44 | |
| 45 | Synchronized s(_monitor); |
| 46 | |
| 47 | _threadFactory = value; |
| 48 | } |
| 49 | |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame^] | 50 | void addWorker(size_t value); |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 51 | |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame^] | 52 | void removeWorker(size_t value); |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 53 | |
| 54 | size_t idleWorkerCount() const {return _idleCount;} |
| 55 | |
| 56 | size_t workerCount() const { |
| 57 | |
| 58 | Synchronized s(_monitor); |
| 59 | |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame^] | 60 | return _workerCount; |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | size_t pendingTaskCount() const { |
| 64 | |
| 65 | Synchronized s(_monitor); |
| 66 | |
| 67 | return _tasks.size(); |
| 68 | } |
| 69 | |
| 70 | size_t totalTaskCount() const { |
| 71 | |
| 72 | Synchronized s(_monitor); |
| 73 | |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame^] | 74 | return _tasks.size() + _workerCount - _idleCount; |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | void add(Runnable* value); |
| 78 | |
| 79 | void remove(Runnable* task); |
| 80 | |
| 81 | private: |
| 82 | |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame^] | 83 | size_t _workerCount; |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 84 | |
| 85 | size_t _idleCount; |
| 86 | |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame^] | 87 | bool _stopped; |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 88 | |
| 89 | const ThreadFactory* _threadFactory; |
| 90 | |
| 91 | friend class ThreadManager::Task; |
| 92 | |
| 93 | std::queue<Task*> _tasks; |
| 94 | |
| 95 | Monitor _monitor; |
| 96 | |
| 97 | friend class ThreadManager::Worker; |
| 98 | |
| 99 | std::set<Thread*> _workers; |
| 100 | }; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 101 | |
| 102 | class ThreadManager::Task : public Runnable { |
| 103 | |
| 104 | public: |
| 105 | enum STATE { |
| 106 | WAITING, |
| 107 | EXECUTING, |
| 108 | CANCELLED, |
| 109 | COMPLETE |
| 110 | }; |
| 111 | |
| 112 | Task(Runnable* runnable) : |
| 113 | _runnable(runnable), |
| 114 | _state(WAITING) |
| 115 | {} |
| 116 | |
| 117 | ~Task() {}; |
| 118 | |
| 119 | void run() { |
| 120 | if(_state == EXECUTING) { |
| 121 | _runnable->run(); |
| 122 | _state = COMPLETE; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | private: |
| 127 | |
| 128 | Runnable* _runnable; |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame^] | 129 | |
| 130 | friend class ThreadManager::Worker; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 131 | |
| 132 | STATE _state; |
| 133 | }; |
| 134 | |
| 135 | class ThreadManager::Worker: public Runnable { |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 136 | enum STATE { |
| 137 | UNINITIALIZED, |
| 138 | STARTING, |
| 139 | STARTED, |
| 140 | STOPPING, |
| 141 | STOPPED |
| 142 | }; |
| 143 | |
| 144 | public: |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 145 | Worker(ThreadManager::Impl* manager) : |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 146 | _manager(manager), |
| 147 | _state(UNINITIALIZED), |
| 148 | _idle(false) |
| 149 | {} |
| 150 | |
| 151 | ~Worker() {} |
| 152 | |
| 153 | /** Worker entry point |
| 154 | |
| 155 | As long as worker thread is running, pull tasks off the task queue and execute. */ |
| 156 | |
| 157 | void run() { |
| 158 | |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 159 | {Synchronized s(_manager->_monitor); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 160 | |
| 161 | if(_state == STARTING) { |
| 162 | _state = STARTED; |
| 163 | } |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame^] | 164 | |
| 165 | _manager->_workerCount++; |
| 166 | |
| 167 | _manager->_monitor.notify(); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | do { |
| 171 | |
| 172 | ThreadManager::Task* task = NULL; |
| 173 | |
| 174 | /* While holding manager monitor block for non-empty task queue (Also check that the thread hasn't been requested to stop). |
| 175 | |
| 176 | Once the queue is non-empty, dequeue a task, release monitor, and execute. */ |
| 177 | |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 178 | {Synchronized s(_manager->_monitor); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 179 | |
| 180 | while(_state == STARTED && _manager->_tasks.empty()) { |
| 181 | |
| 182 | _manager->_idleCount++; |
| 183 | |
| 184 | _idle = true; |
| 185 | |
| 186 | _manager->_monitor.wait(); |
| 187 | |
| 188 | _idle = false; |
| 189 | |
| 190 | _manager->_idleCount--; |
| 191 | } |
| 192 | |
| 193 | if(_state == STARTED) { |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame^] | 194 | |
| 195 | if(!_manager->_tasks.empty()) { |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 196 | |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame^] | 197 | task = _manager->_tasks.front(); |
| 198 | |
| 199 | _manager->_tasks.pop(); |
| 200 | |
| 201 | if(task->_state == ThreadManager::Task::WAITING) { |
| 202 | |
| 203 | task->_state = ThreadManager::Task::EXECUTING; |
| 204 | } |
| 205 | } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | |
| 209 | if(task != NULL) { |
| 210 | |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame^] | 211 | if(task->_state == ThreadManager::Task::EXECUTING) { |
| 212 | try { |
| 213 | |
| 214 | task->run(); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 215 | |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame^] | 216 | } catch(...) { |
| 217 | |
| 218 | // XXX need to log this |
| 219 | } |
| 220 | |
| 221 | delete task; |
| 222 | } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 223 | } |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame^] | 224 | |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 225 | } while(_state == STARTED); |
| 226 | |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 227 | {Synchronized s(_manager->_monitor); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 228 | |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame^] | 229 | _manager->_workerCount--; |
| 230 | |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 231 | if(_state == STOPPING) { |
| 232 | |
| 233 | _state = STOPPED; |
| 234 | |
| 235 | _manager->_monitor.notify(); |
| 236 | |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | return; |
| 241 | } |
| 242 | |
| 243 | private: |
| 244 | |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 245 | ThreadManager::Impl* _manager; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 246 | |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 247 | friend class ThreadManager::Impl; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 248 | |
| 249 | STATE _state; |
| 250 | |
| 251 | bool _idle; |
| 252 | }; |
| 253 | |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame^] | 254 | void ThreadManager::Impl::addWorker(size_t value) { |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 255 | |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame^] | 256 | std::set<Thread*> newThreads; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 257 | |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame^] | 258 | for(size_t ix = 0; ix < value; ix++) { |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 259 | |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame^] | 260 | class ThreadManager::Worker; |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 261 | |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame^] | 262 | ThreadManager::Worker* worker = new ThreadManager::Worker(this); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 263 | |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame^] | 264 | newThreads.insert(_threadFactory->newThread(worker)); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 265 | } |
| 266 | |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame^] | 267 | for(std::set<Thread*>::iterator ix = newThreads.begin(); ix != newThreads.end(); ix++) { |
| 268 | |
| 269 | ThreadManager::Worker* worker = (ThreadManager::Worker*)(*ix)->runnable(); |
| 270 | |
| 271 | worker->_state = ThreadManager::Worker::STARTING; |
| 272 | |
| 273 | (*ix)->start(); |
| 274 | } |
| 275 | |
| 276 | {Synchronized s(_monitor); |
| 277 | |
| 278 | _workers.insert(newThreads.begin(), newThreads.end()); |
| 279 | |
| 280 | while(_workerCount != _workers.size()) { |
| 281 | _monitor.wait(); |
| 282 | } |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | void ThreadManager::Impl::stop() { |
| 287 | |
| 288 | bool doStop = false; |
| 289 | |
| 290 | {Synchronized s(_monitor); |
| 291 | |
| 292 | if(!_stopped) { |
| 293 | doStop = true; |
| 294 | _stopped = true; |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | if(doStop) { |
| 299 | removeWorker(_workerCount); |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | void ThreadManager::Impl::removeWorker(size_t value) { |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 304 | |
| 305 | std::set<Thread*> removedThreads; |
| 306 | |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 307 | {Synchronized s(_monitor); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 308 | |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 309 | /* Overly clever loop |
| 310 | |
| 311 | First time through, (idleOnly == 1) just look for idle threads. If that didn't find enough, go through again (idleOnly == 0) |
| 312 | and remove a sufficient number of busy threads. */ |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 313 | |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame^] | 314 | for(int idleOnly = 1; idleOnly >= 0; idleOnly--) { |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 315 | |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 316 | for(std::set<Thread*>::iterator workerThread = _workers.begin(); (workerThread != _workers.end()) && (removedThreads.size() < value); workerThread++) { |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 317 | |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 318 | Worker* worker = (Worker*)(*workerThread)->runnable(); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 319 | |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 320 | if(worker->_idle || !idleOnly) { |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame^] | 321 | |
| 322 | if(worker->_state == ThreadManager::Worker::STARTED) { |
| 323 | |
| 324 | worker->_state = ThreadManager::Worker::STOPPING; |
| 325 | } |
| 326 | |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 327 | removedThreads.insert(*workerThread); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 328 | |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 329 | _workers.erase(workerThread); |
| 330 | } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 331 | } |
| 332 | } |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 333 | |
| 334 | _monitor.notifyAll(); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 335 | } |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 336 | |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 337 | |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 338 | // Join removed threads and free worker |
| 339 | |
| 340 | for(std::set<Thread*>::iterator workerThread = removedThreads.begin(); workerThread != removedThreads.end(); workerThread++) { |
| 341 | |
| 342 | Worker* worker = (Worker*)(*workerThread)->runnable(); |
| 343 | |
| 344 | (*workerThread)->join(); |
| 345 | |
| 346 | delete worker; |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | void ThreadManager::Impl::add(Runnable* value) { |
| 351 | |
| 352 | Synchronized s(_monitor); |
| 353 | |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame^] | 354 | bool isEmpty = _tasks.empty(); |
| 355 | |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 356 | _tasks.push(new ThreadManager::Task(value)); |
| 357 | |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame^] | 358 | /* If queue was empty notify a thread, otherwise all worker threads are running and will get around to this |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 359 | task in time. */ |
| 360 | |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame^] | 361 | if(isEmpty) { |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 362 | |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame^] | 363 | assert(_idleCount == _workerCount); |
| 364 | |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 365 | _monitor.notify(); |
| 366 | } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 367 | } |
| 368 | |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 369 | void ThreadManager::Impl::remove(Runnable* task) { |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 370 | |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 371 | Synchronized s(_monitor); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 372 | } |
| 373 | |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame^] | 374 | class SimpleThreadManager : public ThreadManager::Impl { |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 375 | |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame^] | 376 | public: |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 377 | |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame^] | 378 | SimpleThreadManager(size_t workerCount=4) : |
| 379 | _workerCount(workerCount), |
| 380 | _firstTime(true) { |
| 381 | } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 382 | |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame^] | 383 | void add(Runnable* task) { |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 384 | |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame^] | 385 | bool addWorkers = false; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 386 | |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame^] | 387 | {Synchronized s(_monitor); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 388 | |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame^] | 389 | if(_firstTime) { |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 390 | |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame^] | 391 | _firstTime = false; |
| 392 | |
| 393 | addWorkers = true; |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | if(addWorkers) { |
| 398 | |
| 399 | addWorker(_workerCount); |
| 400 | } |
| 401 | |
| 402 | Impl::add(task); |
| 403 | } |
| 404 | |
| 405 | private: |
| 406 | |
| 407 | const size_t _workerCount; |
| 408 | bool _firstTime; |
| 409 | Monitor _monitor; |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 410 | }; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 411 | |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 412 | |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame^] | 413 | ThreadManager* ThreadManager::newThreadManager() { |
| 414 | return new ThreadManager::Impl(); |
| 415 | } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 416 | |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame^] | 417 | ThreadManager* ThreadManager::newSimpleThreadManager() { |
| 418 | return new SimpleThreadManager(); |
| 419 | } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 420 | |
| 421 | }}} // facebook::thrift::concurrency |
| 422 | |