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 | |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame^] | 11 | |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 12 | /** ThreadManager class |
| 13 | |
| 14 | 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] | 15 | 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] | 16 | |
| 17 | @author marc |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 18 | @version $Id:$ */ |
| 19 | |
| 20 | class ThreadManager::Impl : public ThreadManager { |
| 21 | |
| 22 | public: |
| 23 | |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame^] | 24 | Impl() : _state(ThreadManager::UNINITIALIZED) {} |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 25 | |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame^] | 26 | ~Impl() {stop();} |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 27 | |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame^] | 28 | void start(); |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 29 | |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 30 | void stop(); |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 31 | |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame^] | 32 | const ThreadManager::STATE state() const { |
| 33 | return _state; |
| 34 | }; |
| 35 | |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 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 | |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame^] | 85 | size_t _workerMaxCount; |
| 86 | |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 87 | size_t _idleCount; |
| 88 | |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame^] | 89 | ThreadManager::STATE _state; |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 90 | |
| 91 | const ThreadFactory* _threadFactory; |
| 92 | |
| 93 | friend class ThreadManager::Task; |
| 94 | |
| 95 | std::queue<Task*> _tasks; |
| 96 | |
| 97 | Monitor _monitor; |
| 98 | |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame^] | 99 | Monitor _workerMonitor; |
| 100 | |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 101 | friend class ThreadManager::Worker; |
| 102 | |
| 103 | std::set<Thread*> _workers; |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame^] | 104 | |
| 105 | std::set<Thread*> _deadWorkers; |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 106 | }; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 107 | |
| 108 | class ThreadManager::Task : public Runnable { |
| 109 | |
| 110 | public: |
| 111 | enum STATE { |
| 112 | WAITING, |
| 113 | EXECUTING, |
| 114 | CANCELLED, |
| 115 | COMPLETE |
| 116 | }; |
| 117 | |
| 118 | Task(Runnable* runnable) : |
| 119 | _runnable(runnable), |
| 120 | _state(WAITING) |
| 121 | {} |
| 122 | |
| 123 | ~Task() {}; |
| 124 | |
| 125 | void run() { |
| 126 | if(_state == EXECUTING) { |
| 127 | _runnable->run(); |
| 128 | _state = COMPLETE; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | private: |
| 133 | |
| 134 | Runnable* _runnable; |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 135 | |
| 136 | friend class ThreadManager::Worker; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 137 | |
| 138 | STATE _state; |
| 139 | }; |
| 140 | |
| 141 | class ThreadManager::Worker: public Runnable { |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame^] | 142 | |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 143 | enum STATE { |
| 144 | UNINITIALIZED, |
| 145 | STARTING, |
| 146 | STARTED, |
| 147 | STOPPING, |
| 148 | STOPPED |
| 149 | }; |
| 150 | |
| 151 | public: |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame^] | 152 | |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 153 | Worker(ThreadManager::Impl* manager) : |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 154 | _manager(manager), |
| 155 | _state(UNINITIALIZED), |
| 156 | _idle(false) |
| 157 | {} |
| 158 | |
| 159 | ~Worker() {} |
| 160 | |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame^] | 161 | bool isActive() const { return _manager->_workerCount <= _manager->_workerMaxCount;} |
| 162 | |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 163 | /** Worker entry point |
| 164 | |
| 165 | As long as worker thread is running, pull tasks off the task queue and execute. */ |
| 166 | |
| 167 | void run() { |
| 168 | |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame^] | 169 | bool active = false; |
| 170 | |
| 171 | bool notifyManager = false; |
| 172 | |
| 173 | /** Increment worker semaphore and notify manager if worker count reached desired max |
| 174 | |
| 175 | Note |
| 176 | We have to release the monitor and acquire the workerMonitor since that is what the manager |
| 177 | blocks on for worker add/remove */ |
| 178 | |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 179 | {Synchronized s(_manager->_monitor); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 180 | |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame^] | 181 | active = _manager->_workerCount < _manager->_workerMaxCount; |
| 182 | |
| 183 | if(active) { |
| 184 | |
| 185 | _manager->_workerCount++; |
| 186 | |
| 187 | notifyManager = _manager->_workerCount == _manager->_workerMaxCount; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame^] | 191 | if(notifyManager) { |
| 192 | |
| 193 | Synchronized s(_manager->_workerMonitor); |
| 194 | |
| 195 | _manager->_workerMonitor.notify(); |
| 196 | |
| 197 | notifyManager = false; |
| 198 | } |
| 199 | |
| 200 | while(active) { |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 201 | |
| 202 | ThreadManager::Task* task = NULL; |
| 203 | |
| 204 | /* While holding manager monitor block for non-empty task queue (Also check that the thread hasn't been requested to stop). |
| 205 | |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame^] | 206 | Once the queue is non-empty, dequeue a task, release monitor, and execute. If the worker max count has been decremented |
| 207 | such that we exceed it, mark ourself inactive, decrement the worker count and notify the manager (technically we're notifying |
| 208 | the next blocked thread but eventually the manager will see it. */ |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 209 | |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 210 | {Synchronized s(_manager->_monitor); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 211 | |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame^] | 212 | active = isActive(); |
| 213 | |
| 214 | while(active && _manager->_tasks.empty()) { |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 215 | |
| 216 | _manager->_idleCount++; |
| 217 | |
| 218 | _idle = true; |
| 219 | |
| 220 | _manager->_monitor.wait(); |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame^] | 221 | |
| 222 | active = isActive(); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 223 | |
| 224 | _idle = false; |
| 225 | |
| 226 | _manager->_idleCount--; |
| 227 | } |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame^] | 228 | |
| 229 | if(active) { |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 230 | |
| 231 | if(!_manager->_tasks.empty()) { |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 232 | |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 233 | task = _manager->_tasks.front(); |
| 234 | |
| 235 | _manager->_tasks.pop(); |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame^] | 236 | |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 237 | if(task->_state == ThreadManager::Task::WAITING) { |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame^] | 238 | |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 239 | task->_state = ThreadManager::Task::EXECUTING; |
| 240 | } |
| 241 | } |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame^] | 242 | } else { |
| 243 | |
| 244 | _idle = true; |
| 245 | |
| 246 | _manager->_workerCount--; |
| 247 | |
| 248 | notifyManager = _manager->_workerCount == _manager->_workerMaxCount; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 249 | } |
| 250 | } |
| 251 | |
| 252 | if(task != NULL) { |
| 253 | |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 254 | if(task->_state == ThreadManager::Task::EXECUTING) { |
| 255 | try { |
| 256 | |
| 257 | task->run(); |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame^] | 258 | |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 259 | } catch(...) { |
| 260 | |
| 261 | // XXX need to log this |
| 262 | } |
| 263 | |
| 264 | delete task; |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame^] | 265 | |
| 266 | task = NULL; |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 267 | } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 268 | } |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame^] | 269 | } |
| 270 | |
| 271 | {Synchronized s(_manager->_workerMonitor); |
| 272 | |
| 273 | _manager->_deadWorkers.insert(this->thread()); |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 274 | |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame^] | 275 | if(notifyManager) { |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 276 | |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame^] | 277 | _manager->_workerMonitor.notify(); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 278 | } |
| 279 | } |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame^] | 280 | |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 281 | return; |
| 282 | } |
| 283 | |
| 284 | private: |
| 285 | |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 286 | ThreadManager::Impl* _manager; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 287 | |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 288 | friend class ThreadManager::Impl; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 289 | |
| 290 | STATE _state; |
| 291 | |
| 292 | bool _idle; |
| 293 | }; |
| 294 | |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 295 | void ThreadManager::Impl::addWorker(size_t value) { |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 296 | |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 297 | std::set<Thread*> newThreads; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 298 | |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 299 | for(size_t ix = 0; ix < value; ix++) { |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 300 | |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 301 | class ThreadManager::Worker; |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 302 | |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 303 | ThreadManager::Worker* worker = new ThreadManager::Worker(this); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 304 | |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 305 | newThreads.insert(_threadFactory->newThread(worker)); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 306 | } |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame^] | 307 | |
| 308 | {Synchronized s(_monitor); |
| 309 | |
| 310 | _workerMaxCount+= value; |
| 311 | |
| 312 | _workers.insert(newThreads.begin(), newThreads.end()); |
| 313 | } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 314 | |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 315 | for(std::set<Thread*>::iterator ix = newThreads.begin(); ix != newThreads.end(); ix++) { |
| 316 | |
| 317 | ThreadManager::Worker* worker = (ThreadManager::Worker*)(*ix)->runnable(); |
| 318 | |
| 319 | worker->_state = ThreadManager::Worker::STARTING; |
| 320 | |
| 321 | (*ix)->start(); |
| 322 | } |
| 323 | |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame^] | 324 | {Synchronized s(_workerMonitor); |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 325 | |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame^] | 326 | while(_workerCount != _workerMaxCount) { |
| 327 | _workerMonitor.wait(); |
| 328 | } |
| 329 | } |
| 330 | } |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 331 | |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame^] | 332 | void ThreadManager::Impl::start() { |
| 333 | |
| 334 | if(_state == ThreadManager::STOPPED) { |
| 335 | return; |
| 336 | } |
| 337 | |
| 338 | {Synchronized s(_monitor); |
| 339 | |
| 340 | if(_state == ThreadManager::UNINITIALIZED) { |
| 341 | |
| 342 | if(_threadFactory == NULL) {throw InvalidArgumentException();} |
| 343 | |
| 344 | _state = ThreadManager::STARTED; |
| 345 | |
| 346 | _monitor.notifyAll(); |
| 347 | } |
| 348 | |
| 349 | while(_state == STARTING) { |
| 350 | |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 351 | _monitor.wait(); |
| 352 | } |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | void ThreadManager::Impl::stop() { |
| 357 | |
| 358 | bool doStop = false; |
| 359 | |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame^] | 360 | if(_state == ThreadManager::STOPPED) { |
| 361 | return; |
| 362 | } |
| 363 | |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 364 | {Synchronized s(_monitor); |
| 365 | |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame^] | 366 | if(!_state != ThreadManager::STOPPING && _state != ThreadManager::STOPPED) { |
| 367 | |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 368 | doStop = true; |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame^] | 369 | |
| 370 | _state = ThreadManager::STOPPING; |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 371 | } |
| 372 | } |
| 373 | |
| 374 | if(doStop) { |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame^] | 375 | |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 376 | removeWorker(_workerCount); |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame^] | 377 | |
| 378 | _state = ThreadManager::STOPPING; |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 379 | } |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame^] | 380 | |
| 381 | // Don't block for stopping->stopped transition here, since if stop is being performed in context of a delete, the monitor may be invalid |
| 382 | |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | void ThreadManager::Impl::removeWorker(size_t value) { |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 386 | |
| 387 | std::set<Thread*> removedThreads; |
| 388 | |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame^] | 389 | {Synchronized s(_monitor); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 390 | |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame^] | 391 | if(value > _workerMaxCount) { |
| 392 | |
| 393 | throw InvalidArgumentException(); |
| 394 | } |
| 395 | |
| 396 | _workerMaxCount-= value; |
| 397 | |
| 398 | if(_idleCount < value) { |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 399 | |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame^] | 400 | for(size_t ix = 0; ix < _idleCount; ix++) { |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 401 | |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame^] | 402 | _monitor.notify(); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 403 | } |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame^] | 404 | } else { |
| 405 | |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 406 | _monitor.notifyAll(); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 407 | } |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 408 | } |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame^] | 409 | |
| 410 | {Synchronized s(_workerMonitor); |
| 411 | |
| 412 | while(_workerCount != _workerMaxCount) { |
| 413 | _workerMonitor.wait(); |
| 414 | } |
| 415 | |
| 416 | for(std::set<Thread*>::iterator ix = _deadWorkers.begin(); ix != _deadWorkers.end(); ix++) { |
| 417 | |
| 418 | _workers.erase(*ix); |
| 419 | |
| 420 | delete (*ix)->runnable(); |
| 421 | |
| 422 | delete (*ix); |
| 423 | } |
| 424 | |
| 425 | _deadWorkers.clear(); |
| 426 | } |
| 427 | } |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 428 | |
| 429 | void ThreadManager::Impl::add(Runnable* value) { |
| 430 | |
| 431 | Synchronized s(_monitor); |
| 432 | |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame^] | 433 | if(_state != ThreadManager::STARTED) { |
| 434 | |
| 435 | throw IllegalStateException(); |
| 436 | } |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 437 | |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 438 | _tasks.push(new ThreadManager::Task(value)); |
| 439 | |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame^] | 440 | /* If idle thread is available notify it, otherwise all worker threads are running and will get around to this |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 441 | task in time. */ |
| 442 | |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame^] | 443 | if(_idleCount > 0) { |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 444 | |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 445 | _monitor.notify(); |
| 446 | } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 447 | } |
| 448 | |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 449 | void ThreadManager::Impl::remove(Runnable* task) { |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 450 | |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 451 | Synchronized s(_monitor); |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame^] | 452 | |
| 453 | if(_state != ThreadManager::STARTED) { |
| 454 | |
| 455 | throw IllegalStateException(); |
| 456 | } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 457 | } |
| 458 | |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 459 | class SimpleThreadManager : public ThreadManager::Impl { |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 460 | |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 461 | public: |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 462 | |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 463 | SimpleThreadManager(size_t workerCount=4) : |
| 464 | _workerCount(workerCount), |
| 465 | _firstTime(true) { |
| 466 | } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 467 | |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame^] | 468 | void start() { |
| 469 | ThreadManager::Impl::start(); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 470 | |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame^] | 471 | addWorker(_workerCount); |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | private: |
| 475 | |
| 476 | const size_t _workerCount; |
| 477 | bool _firstTime; |
| 478 | Monitor _monitor; |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 479 | }; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 480 | |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 481 | |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 482 | ThreadManager* ThreadManager::newThreadManager() { |
| 483 | return new ThreadManager::Impl(); |
| 484 | } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 485 | |
Marc Slemko | 525c202 | 2006-07-20 00:29:35 +0000 | [diff] [blame] | 486 | ThreadManager* ThreadManager::newSimpleThreadManager(size_t count) { |
| 487 | return new SimpleThreadManager(count); |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 488 | } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 489 | |
| 490 | }}} // facebook::thrift::concurrency |
| 491 | |