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