David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
Mark Slee | 9f0c651 | 2007-02-28 23:58:26 +0000 | [diff] [blame] | 19 | |
Konrad Grochowski | 9be4e68 | 2013-06-22 22:03:31 +0200 | [diff] [blame] | 20 | #include <thrift/thrift-config.h> |
Roger Meier | 12d7053 | 2011-12-14 23:35:28 +0000 | [diff] [blame] | 21 | |
Roger Meier | 4285ba2 | 2013-06-10 21:17:23 +0200 | [diff] [blame] | 22 | #include <thrift/concurrency/ThreadManager.h> |
| 23 | #include <thrift/concurrency/Exception.h> |
| 24 | #include <thrift/concurrency/Monitor.h> |
| 25 | #include <thrift/concurrency/Util.h> |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 26 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 27 | #include <boost/shared_ptr.hpp> |
| 28 | |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 29 | #include <assert.h> |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 30 | #include <queue> |
| 31 | #include <set> |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 32 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 33 | #if defined(DEBUG) |
| 34 | #include <iostream> |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 35 | #endif // defined(DEBUG) |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 36 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 37 | namespace apache { |
| 38 | namespace thrift { |
| 39 | namespace concurrency { |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 40 | |
David Reiss | d4a269c | 2007-08-23 02:37:19 +0000 | [diff] [blame] | 41 | using boost::shared_ptr; |
| 42 | using boost::dynamic_pointer_cast; |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 43 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 44 | /** |
| 45 | * ThreadManager class |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 46 | * |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 47 | * This class manages a pool of threads. It uses a ThreadFactory to create |
| 48 | * threads. It never actually creates or destroys worker threads, rather |
| 49 | * it maintains statistics on number of idle threads, number of active threads, |
| 50 | * task backlog, and average wait and service times. |
| 51 | * |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 52 | * @version $Id:$ |
| 53 | */ |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 54 | class ThreadManager::Impl : public ThreadManager { |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 55 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 56 | public: |
| 57 | Impl() |
| 58 | : workerCount_(0), |
| 59 | workerMaxCount_(0), |
| 60 | idleCount_(0), |
| 61 | pendingTaskCountMax_(0), |
| 62 | expiredCount_(0), |
| 63 | state_(ThreadManager::UNINITIALIZED), |
| 64 | monitor_(&mutex_), |
| 65 | maxMonitor_(&mutex_) {} |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 66 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 67 | ~Impl() { stop(); } |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 68 | |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 69 | void start(); |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 70 | |
Mark Slee | 7c10eaf | 2007-03-01 02:45:10 +0000 | [diff] [blame] | 71 | void stop() { stopImpl(false); } |
| 72 | |
| 73 | void join() { stopImpl(true); } |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 74 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 75 | ThreadManager::STATE state() const { return state_; } |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 76 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 77 | shared_ptr<ThreadFactory> threadFactory() const { |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 78 | Synchronized s(monitor_); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 79 | return threadFactory_; |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 80 | } |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 81 | |
| 82 | void threadFactory(shared_ptr<ThreadFactory> value) { |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 83 | Synchronized s(monitor_); |
| 84 | threadFactory_ = value; |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 87 | void addWorker(size_t value); |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 88 | |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 89 | void removeWorker(size_t value); |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 90 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 91 | size_t idleWorkerCount() const { return idleCount_; } |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 92 | |
| 93 | size_t workerCount() const { |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 94 | Synchronized s(monitor_); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 95 | return workerCount_; |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 96 | } |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 97 | |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 98 | size_t pendingTaskCount() const { |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 99 | Synchronized s(monitor_); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 100 | return tasks_.size(); |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | size_t totalTaskCount() const { |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 104 | Synchronized s(monitor_); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 105 | return tasks_.size() + workerCount_ - idleCount_; |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 106 | } |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 107 | |
| 108 | size_t pendingTaskCountMax() const { |
| 109 | Synchronized s(monitor_); |
| 110 | return pendingTaskCountMax_; |
| 111 | } |
| 112 | |
David Reiss | 068f416 | 2010-03-09 05:19:45 +0000 | [diff] [blame] | 113 | size_t expiredTaskCount() { |
| 114 | Synchronized s(monitor_); |
| 115 | size_t result = expiredCount_; |
| 116 | expiredCount_ = 0; |
| 117 | return result; |
| 118 | } |
| 119 | |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 120 | void pendingTaskCountMax(const size_t value) { |
| 121 | Synchronized s(monitor_); |
| 122 | pendingTaskCountMax_ = value; |
| 123 | } |
| 124 | |
| 125 | bool canSleep(); |
| 126 | |
David Reiss | 068f416 | 2010-03-09 05:19:45 +0000 | [diff] [blame] | 127 | void add(shared_ptr<Runnable> value, int64_t timeout, int64_t expiration); |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 128 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 129 | void remove(shared_ptr<Runnable> task); |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 130 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 131 | shared_ptr<Runnable> removeNextPending(); |
| 132 | |
David Reiss | 068f416 | 2010-03-09 05:19:45 +0000 | [diff] [blame] | 133 | void removeExpiredTasks(); |
| 134 | |
| 135 | void setExpireCallback(ExpireCallback expireCallback); |
| 136 | |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 137 | private: |
Mark Slee | 7c10eaf | 2007-03-01 02:45:10 +0000 | [diff] [blame] | 138 | void stopImpl(bool join); |
| 139 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 140 | size_t workerCount_; |
| 141 | size_t workerMaxCount_; |
| 142 | size_t idleCount_; |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 143 | size_t pendingTaskCountMax_; |
David Reiss | 068f416 | 2010-03-09 05:19:45 +0000 | [diff] [blame] | 144 | size_t expiredCount_; |
| 145 | ExpireCallback expireCallback_; |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 146 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 147 | ThreadManager::STATE state_; |
| 148 | shared_ptr<ThreadFactory> threadFactory_; |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 149 | |
| 150 | friend class ThreadManager::Task; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 151 | std::queue<shared_ptr<Task> > tasks_; |
David Reiss | a0dbfef | 2010-03-09 05:19:32 +0000 | [diff] [blame] | 152 | Mutex mutex_; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 153 | Monitor monitor_; |
David Reiss | a0dbfef | 2010-03-09 05:19:32 +0000 | [diff] [blame] | 154 | Monitor maxMonitor_; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 155 | Monitor workerMonitor_; |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 156 | |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 157 | friend class ThreadManager::Worker; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 158 | std::set<shared_ptr<Thread> > workers_; |
| 159 | std::set<shared_ptr<Thread> > deadWorkers_; |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 160 | std::map<const Thread::id_t, shared_ptr<Thread> > idMap_; |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 161 | }; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 162 | |
| 163 | class ThreadManager::Task : public Runnable { |
| 164 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 165 | public: |
| 166 | enum STATE { WAITING, EXECUTING, CANCELLED, COMPLETE }; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 167 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 168 | Task(shared_ptr<Runnable> runnable, int64_t expiration = 0LL) |
| 169 | : runnable_(runnable), |
| 170 | state_(WAITING), |
| 171 | expireTime_(expiration != 0LL ? Util::currentTime() + expiration : 0LL) {} |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 172 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 173 | ~Task() {} |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 174 | |
| 175 | void run() { |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 176 | if (state_ == EXECUTING) { |
| 177 | runnable_->run(); |
| 178 | state_ = COMPLETE; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 179 | } |
| 180 | } |
| 181 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 182 | shared_ptr<Runnable> getRunnable() { return runnable_; } |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 183 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 184 | int64_t getExpireTime() const { return expireTime_; } |
David Reiss | 068f416 | 2010-03-09 05:19:45 +0000 | [diff] [blame] | 185 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 186 | private: |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 187 | shared_ptr<Runnable> runnable_; |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 188 | friend class ThreadManager::Worker; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 189 | STATE state_; |
David Reiss | 068f416 | 2010-03-09 05:19:45 +0000 | [diff] [blame] | 190 | int64_t expireTime_; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 191 | }; |
| 192 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 193 | class ThreadManager::Worker : public Runnable { |
| 194 | enum STATE { UNINITIALIZED, STARTING, STARTED, STOPPING, STOPPED }; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 195 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 196 | public: |
| 197 | Worker(ThreadManager::Impl* manager) : manager_(manager), state_(UNINITIALIZED), idle_(false) {} |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 198 | |
| 199 | ~Worker() {} |
| 200 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 201 | private: |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 202 | bool isActive() const { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 203 | return (manager_->workerCount_ <= manager_->workerMaxCount_) |
| 204 | || (manager_->state_ == JOINING && !manager_->tasks_.empty()); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 205 | } |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 206 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 207 | public: |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 208 | /** |
| 209 | * Worker entry point |
| 210 | * |
| 211 | * As long as worker thread is running, pull tasks off the task queue and |
| 212 | * execute. |
| 213 | */ |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 214 | void run() { |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 215 | bool active = false; |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 216 | /** |
| 217 | * Increment worker semaphore and notify manager if worker count reached |
| 218 | * desired max |
| 219 | * |
| 220 | * Note: We have to release the monitor and acquire the workerMonitor |
| 221 | * since that is what the manager blocks on for worker add/remove |
| 222 | */ |
| 223 | { |
Jim King | 61b1708 | 2016-04-19 15:57:31 -0400 | [diff] [blame] | 224 | bool notifyManager = false; |
| 225 | { |
| 226 | Synchronized s(manager_->monitor_); |
| 227 | active = manager_->workerCount_ < manager_->workerMaxCount_; |
| 228 | if (active) { |
| 229 | manager_->workerCount_++; |
| 230 | notifyManager = manager_->workerCount_ == manager_->workerMaxCount_; |
| 231 | } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 232 | } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 233 | |
Jim King | 61b1708 | 2016-04-19 15:57:31 -0400 | [diff] [blame] | 234 | if (notifyManager) { |
| 235 | Synchronized s(manager_->workerMonitor_); |
| 236 | manager_->workerMonitor_.notify(); |
| 237 | } |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 238 | } |
| 239 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 240 | while (active) { |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 241 | shared_ptr<ThreadManager::Task> task; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 242 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 243 | /** |
| 244 | * While holding manager monitor block for non-empty task queue (Also |
| 245 | * check that the thread hasn't been requested to stop). Once the queue |
| 246 | * is non-empty, dequeue a task, release monitor, and execute. If the |
| 247 | * worker max count has been decremented such that we exceed it, mark |
| 248 | * ourself inactive, decrement the worker count and notify the manager |
| 249 | * (technically we're notifying the next blocked thread but eventually |
| 250 | * the manager will see it. |
| 251 | */ |
| 252 | { |
David Reiss | a0dbfef | 2010-03-09 05:19:32 +0000 | [diff] [blame] | 253 | Guard g(manager_->mutex_); |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 254 | active = isActive(); |
Mark Slee | 7c10eaf | 2007-03-01 02:45:10 +0000 | [diff] [blame] | 255 | |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 256 | while (active && manager_->tasks_.empty()) { |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 257 | manager_->idleCount_++; |
Mark Slee | 7c10eaf | 2007-03-01 02:45:10 +0000 | [diff] [blame] | 258 | idle_ = true; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 259 | manager_->monitor_.wait(); |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 260 | active = isActive(); |
Mark Slee | 7c10eaf | 2007-03-01 02:45:10 +0000 | [diff] [blame] | 261 | idle_ = false; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 262 | manager_->idleCount_--; |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 263 | } |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 264 | |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 265 | if (active) { |
David Reiss | 068f416 | 2010-03-09 05:19:45 +0000 | [diff] [blame] | 266 | manager_->removeExpiredTasks(); |
| 267 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 268 | if (!manager_->tasks_.empty()) { |
| 269 | task = manager_->tasks_.front(); |
| 270 | manager_->tasks_.pop(); |
| 271 | if (task->state_ == ThreadManager::Task::WAITING) { |
| 272 | task->state_ = ThreadManager::Task::EXECUTING; |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 273 | } |
Qiao Mu | 2fadc8d | 2014-12-03 10:48:36 +0800 | [diff] [blame] | 274 | } |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 275 | |
Qiao Mu | 2fadc8d | 2014-12-03 10:48:36 +0800 | [diff] [blame] | 276 | /* If we have a pending task max and we just dropped below it, wakeup any |
| 277 | thread that might be blocked on add. */ |
| 278 | if (manager_->pendingTaskCountMax_ != 0 |
Konrad Grochowski | 7f4be5f | 2015-11-05 20:23:11 +0100 | [diff] [blame] | 279 | && manager_->tasks_.size() <= manager_->pendingTaskCountMax_ - 1) { |
| 280 | manager_->maxMonitor_.notify(); |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 281 | } |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 282 | } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 283 | } |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 284 | |
Roger Meier | 7295745 | 2013-06-29 00:28:50 +0200 | [diff] [blame] | 285 | if (task) { |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 286 | if (task->state_ == ThreadManager::Task::EXECUTING) { |
| 287 | try { |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 288 | task->run(); |
Konrad Grochowski | 3876ea7 | 2014-12-09 15:24:56 +0100 | [diff] [blame] | 289 | } catch (const std::exception& e) { |
Jens Geyer | fb05cf6 | 2014-12-04 21:49:07 +0100 | [diff] [blame] | 290 | GlobalOutput.printf("[ERROR] task->run() raised an exception: %s", e.what()); |
Konrad Grochowski | 3876ea7 | 2014-12-09 15:24:56 +0100 | [diff] [blame] | 291 | } catch (...) { |
Jens Geyer | fb05cf6 | 2014-12-04 21:49:07 +0100 | [diff] [blame] | 292 | GlobalOutput.printf("[ERROR] task->run() raised an unknown exception"); |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 293 | } |
| 294 | } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 295 | } |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 296 | } |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 297 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 298 | { |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 299 | Synchronized s(manager_->workerMonitor_); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 300 | manager_->deadWorkers_.insert(this->thread()); |
Jim King | 61b1708 | 2016-04-19 15:57:31 -0400 | [diff] [blame] | 301 | idle_ = true; |
| 302 | manager_->workerCount_--; |
| 303 | bool notifyManager = (manager_->workerCount_ == manager_->workerMaxCount_); |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 304 | if (notifyManager) { |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 305 | manager_->workerMonitor_.notify(); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 306 | } |
| 307 | } |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 308 | |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 309 | return; |
| 310 | } |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 311 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 312 | private: |
| 313 | ThreadManager::Impl* manager_; |
| 314 | friend class ThreadManager::Impl; |
| 315 | STATE state_; |
| 316 | bool idle_; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 317 | }; |
| 318 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 319 | void ThreadManager::Impl::addWorker(size_t value) { |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 320 | std::set<shared_ptr<Thread> > newThreads; |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 321 | for (size_t ix = 0; ix < value; ix++) { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 322 | shared_ptr<ThreadManager::Worker> worker |
| 323 | = shared_ptr<ThreadManager::Worker>(new ThreadManager::Worker(this)); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 324 | newThreads.insert(threadFactory_->newThread(worker)); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 325 | } |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 326 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 327 | { |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 328 | Synchronized s(monitor_); |
| 329 | workerMaxCount_ += value; |
| 330 | workers_.insert(newThreads.begin(), newThreads.end()); |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 331 | } |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 332 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 333 | for (std::set<shared_ptr<Thread> >::iterator ix = newThreads.begin(); ix != newThreads.end(); |
Roger Meier | 71f2d8a | 2015-04-26 17:00:04 +0200 | [diff] [blame] | 334 | ++ix) { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 335 | shared_ptr<ThreadManager::Worker> worker |
| 336 | = dynamic_pointer_cast<ThreadManager::Worker, Runnable>((*ix)->runnable()); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 337 | worker->state_ = ThreadManager::Worker::STARTING; |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 338 | (*ix)->start(); |
Marc Slemko | a647903 | 2007-06-05 22:20:14 +0000 | [diff] [blame] | 339 | idMap_.insert(std::pair<const Thread::id_t, shared_ptr<Thread> >((*ix)->getId(), *ix)); |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 340 | } |
| 341 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 342 | { |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 343 | Synchronized s(workerMonitor_); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 344 | while (workerCount_ != workerMaxCount_) { |
| 345 | workerMonitor_.wait(); |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 346 | } |
| 347 | } |
| 348 | } |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 349 | |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 350 | void ThreadManager::Impl::start() { |
| 351 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 352 | if (state_ == ThreadManager::STOPPED) { |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 353 | return; |
| 354 | } |
| 355 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 356 | { |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 357 | Synchronized s(monitor_); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 358 | if (state_ == ThreadManager::UNINITIALIZED) { |
Roger Meier | 7295745 | 2013-06-29 00:28:50 +0200 | [diff] [blame] | 359 | if (!threadFactory_) { |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 360 | throw InvalidArgumentException(); |
| 361 | } |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 362 | state_ = ThreadManager::STARTED; |
| 363 | monitor_.notifyAll(); |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 364 | } |
| 365 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 366 | while (state_ == STARTING) { |
| 367 | monitor_.wait(); |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 368 | } |
| 369 | } |
| 370 | } |
| 371 | |
Mark Slee | 7c10eaf | 2007-03-01 02:45:10 +0000 | [diff] [blame] | 372 | void ThreadManager::Impl::stopImpl(bool join) { |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 373 | bool doStop = false; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 374 | if (state_ == ThreadManager::STOPPED) { |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 375 | return; |
| 376 | } |
| 377 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 378 | { |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 379 | Synchronized s(monitor_); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 380 | if (state_ != ThreadManager::STOPPING && state_ != ThreadManager::JOINING |
| 381 | && state_ != ThreadManager::STOPPED) { |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 382 | doStop = true; |
Mark Slee | 7c10eaf | 2007-03-01 02:45:10 +0000 | [diff] [blame] | 383 | state_ = join ? ThreadManager::JOINING : ThreadManager::STOPPING; |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 384 | } |
| 385 | } |
| 386 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 387 | if (doStop) { |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 388 | removeWorker(workerCount_); |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 389 | } |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 390 | |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 391 | // XXX |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 392 | // should be able to block here for transition to STOPPED since we're no |
| 393 | // using shared_ptrs |
Mark Slee | 7c10eaf | 2007-03-01 02:45:10 +0000 | [diff] [blame] | 394 | |
| 395 | { |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 396 | Synchronized s(monitor_); |
Mark Slee | 7c10eaf | 2007-03-01 02:45:10 +0000 | [diff] [blame] | 397 | state_ = ThreadManager::STOPPED; |
| 398 | } |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 399 | } |
Mark Slee | 7c10eaf | 2007-03-01 02:45:10 +0000 | [diff] [blame] | 400 | |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 401 | void ThreadManager::Impl::removeWorker(size_t value) { |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 402 | std::set<shared_ptr<Thread> > removedThreads; |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 403 | { |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 404 | Synchronized s(monitor_); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 405 | if (value > workerMaxCount_) { |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 406 | throw InvalidArgumentException(); |
| 407 | } |
| 408 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 409 | workerMaxCount_ -= value; |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 410 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 411 | if (idleCount_ < value) { |
| 412 | for (size_t ix = 0; ix < idleCount_; ix++) { |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 413 | monitor_.notify(); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 414 | } |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 415 | } else { |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 416 | monitor_.notifyAll(); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 417 | } |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 418 | } |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 419 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 420 | { |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 421 | Synchronized s(workerMonitor_); |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 422 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 423 | while (workerCount_ != workerMaxCount_) { |
| 424 | workerMonitor_.wait(); |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 425 | } |
| 426 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 427 | for (std::set<shared_ptr<Thread> >::iterator ix = deadWorkers_.begin(); |
| 428 | ix != deadWorkers_.end(); |
Roger Meier | 71f2d8a | 2015-04-26 17:00:04 +0200 | [diff] [blame] | 429 | ++ix) { |
Marc Slemko | a647903 | 2007-06-05 22:20:14 +0000 | [diff] [blame] | 430 | idMap_.erase((*ix)->getId()); |
Roger Meier | ffbfd03 | 2013-06-29 14:51:30 +0200 | [diff] [blame] | 431 | workers_.erase(*ix); |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 432 | } |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 433 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 434 | deadWorkers_.clear(); |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 435 | } |
| 436 | } |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 437 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 438 | bool ThreadManager::Impl::canSleep() { |
| 439 | const Thread::id_t id = threadFactory_->getCurrentThreadId(); |
| 440 | return idMap_.find(id) == idMap_.end(); |
| 441 | } |
| 442 | |
| 443 | void ThreadManager::Impl::add(shared_ptr<Runnable> value, int64_t timeout, int64_t expiration) { |
| 444 | Guard g(mutex_, timeout); |
| 445 | |
| 446 | if (!g) { |
| 447 | throw TimedOutException(); |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 448 | } |
| 449 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 450 | if (state_ != ThreadManager::STARTED) { |
| 451 | throw IllegalStateException( |
| 452 | "ThreadManager::Impl::add ThreadManager " |
| 453 | "not started"); |
| 454 | } |
David Reiss | 4e19f19 | 2010-03-09 05:19:59 +0000 | [diff] [blame] | 455 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 456 | removeExpiredTasks(); |
| 457 | if (pendingTaskCountMax_ > 0 && (tasks_.size() >= pendingTaskCountMax_)) { |
| 458 | if (canSleep() && timeout >= 0) { |
| 459 | while (pendingTaskCountMax_ > 0 && tasks_.size() >= pendingTaskCountMax_) { |
| 460 | // This is thread safe because the mutex is shared between monitors. |
| 461 | maxMonitor_.wait(timeout); |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 462 | } |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 463 | } else { |
| 464 | throw TooManyPendingTasksException(); |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 465 | } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 466 | } |
| 467 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 468 | tasks_.push(shared_ptr<ThreadManager::Task>(new ThreadManager::Task(value, expiration))); |
| 469 | |
| 470 | // If idle thread is available notify it, otherwise all worker threads are |
| 471 | // running and will get around to this task in time. |
| 472 | if (idleCount_ > 0) { |
| 473 | monitor_.notify(); |
| 474 | } |
| 475 | } |
| 476 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 477 | void ThreadManager::Impl::remove(shared_ptr<Runnable> task) { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 478 | (void)task; |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 479 | Synchronized s(monitor_); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 480 | if (state_ != ThreadManager::STARTED) { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 481 | throw IllegalStateException( |
| 482 | "ThreadManager::Impl::remove ThreadManager not " |
| 483 | "started"); |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 484 | } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 485 | } |
| 486 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 487 | boost::shared_ptr<Runnable> ThreadManager::Impl::removeNextPending() { |
| 488 | Guard g(mutex_); |
| 489 | if (state_ != ThreadManager::STARTED) { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 490 | throw IllegalStateException( |
| 491 | "ThreadManager::Impl::removeNextPending " |
| 492 | "ThreadManager not started"); |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | if (tasks_.empty()) { |
| 496 | return boost::shared_ptr<Runnable>(); |
| 497 | } |
| 498 | |
| 499 | shared_ptr<ThreadManager::Task> task = tasks_.front(); |
| 500 | tasks_.pop(); |
Roger Meier | 8b51bc6 | 2014-07-24 23:33:33 +0200 | [diff] [blame] | 501 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 502 | return task->getRunnable(); |
| 503 | } |
| 504 | |
David Reiss | 068f416 | 2010-03-09 05:19:45 +0000 | [diff] [blame] | 505 | void ThreadManager::Impl::removeExpiredTasks() { |
| 506 | int64_t now = 0LL; // we won't ask for the time untile we need it |
| 507 | |
| 508 | // note that this loop breaks at the first non-expiring task |
| 509 | while (!tasks_.empty()) { |
| 510 | shared_ptr<ThreadManager::Task> task = tasks_.front(); |
| 511 | if (task->getExpireTime() == 0LL) { |
| 512 | break; |
| 513 | } |
| 514 | if (now == 0LL) { |
| 515 | now = Util::currentTime(); |
| 516 | } |
| 517 | if (task->getExpireTime() > now) { |
| 518 | break; |
| 519 | } |
| 520 | if (expireCallback_) { |
| 521 | expireCallback_(task->getRunnable()); |
| 522 | } |
| 523 | tasks_.pop(); |
| 524 | expiredCount_++; |
| 525 | } |
| 526 | } |
| 527 | |
David Reiss | 068f416 | 2010-03-09 05:19:45 +0000 | [diff] [blame] | 528 | void ThreadManager::Impl::setExpireCallback(ExpireCallback expireCallback) { |
| 529 | expireCallback_ = expireCallback; |
| 530 | } |
| 531 | |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 532 | class SimpleThreadManager : public ThreadManager::Impl { |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 533 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 534 | public: |
| 535 | SimpleThreadManager(size_t workerCount = 4, size_t pendingTaskCountMax = 0) |
| 536 | : workerCount_(workerCount), pendingTaskCountMax_(pendingTaskCountMax) {} |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 537 | |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 538 | void start() { |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 539 | ThreadManager::Impl::pendingTaskCountMax(pendingTaskCountMax_); |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 540 | ThreadManager::Impl::start(); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 541 | addWorker(workerCount_); |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 542 | } |
| 543 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 544 | private: |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 545 | const size_t workerCount_; |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 546 | const size_t pendingTaskCountMax_; |
Jens Geyer | e5fbedd | 2016-07-08 22:00:37 +0200 | [diff] [blame] | 547 | Monitor monitor_; |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 548 | }; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 549 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 550 | shared_ptr<ThreadManager> ThreadManager::newThreadManager() { |
| 551 | return shared_ptr<ThreadManager>(new ThreadManager::Impl()); |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 552 | } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 553 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 554 | shared_ptr<ThreadManager> ThreadManager::newSimpleThreadManager(size_t count, |
| 555 | size_t pendingTaskCountMax) { |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 556 | return shared_ptr<ThreadManager>(new SimpleThreadManager(count, pendingTaskCountMax)); |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 557 | } |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 558 | } |
| 559 | } |
| 560 | } // apache::thrift::concurrency |