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