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