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> |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 25 | |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 26 | #include <memory> |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 27 | |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 28 | #include <stdexcept> |
| 29 | #include <deque> |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 30 | #include <set> |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 31 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 32 | namespace apache { |
| 33 | namespace thrift { |
| 34 | namespace concurrency { |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 35 | |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 36 | using std::shared_ptr; |
cyy | bfdbd03 | 2019-01-12 14:38:28 +0800 | [diff] [blame] | 37 | using std::unique_ptr; |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 38 | using std::dynamic_pointer_cast; |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 39 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 40 | /** |
| 41 | * ThreadManager class |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 42 | * |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 43 | * This class manages a pool of threads. It uses a ThreadFactory to create |
| 44 | * threads. It never actually creates or destroys worker threads, rather |
| 45 | * it maintains statistics on number of idle threads, number of active threads, |
| 46 | * task backlog, and average wait and service times. |
| 47 | * |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 48 | * There are three different monitors used for signaling different conditions |
| 49 | * however they all share the same mutex_. |
| 50 | * |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 51 | * @version $Id:$ |
| 52 | */ |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 53 | class ThreadManager::Impl : public ThreadManager { |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 54 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 55 | public: |
| 56 | Impl() |
| 57 | : workerCount_(0), |
| 58 | workerMaxCount_(0), |
| 59 | idleCount_(0), |
| 60 | pendingTaskCountMax_(0), |
| 61 | expiredCount_(0), |
| 62 | state_(ThreadManager::UNINITIALIZED), |
| 63 | monitor_(&mutex_), |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 64 | maxMonitor_(&mutex_), |
| 65 | workerMonitor_(&mutex_) {} |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 66 | |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 67 | ~Impl() override { stop(); } |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 68 | |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 69 | void start() override; |
| 70 | void stop() override; |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 71 | |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 72 | ThreadManager::STATE state() const override { return state_; } |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 73 | |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 74 | shared_ptr<ThreadFactory> threadFactory() const override { |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 75 | Guard g(mutex_); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 76 | return threadFactory_; |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 77 | } |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 78 | |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 79 | void threadFactory(shared_ptr<ThreadFactory> value) override { |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 80 | Guard g(mutex_); |
| 81 | if (threadFactory_ && threadFactory_->isDetached() != value->isDetached()) { |
| 82 | throw InvalidArgumentException(); |
| 83 | } |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 84 | threadFactory_ = value; |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 87 | void addWorker(size_t value) override; |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 88 | |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 89 | void removeWorker(size_t value) override; |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 90 | |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 91 | size_t idleWorkerCount() const override { return idleCount_; } |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 92 | |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 93 | size_t workerCount() const override { |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 94 | Guard g(mutex_); |
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 | |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 98 | size_t pendingTaskCount() const override { |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 99 | Guard g(mutex_); |
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 | |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 103 | size_t totalTaskCount() const override { |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 104 | Guard g(mutex_); |
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 | |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 108 | size_t pendingTaskCountMax() const override { |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 109 | Guard g(mutex_); |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 110 | return pendingTaskCountMax_; |
| 111 | } |
| 112 | |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 113 | size_t expiredTaskCount() const override { |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 114 | Guard g(mutex_); |
| 115 | return expiredCount_; |
David Reiss | 068f416 | 2010-03-09 05:19:45 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 118 | void pendingTaskCountMax(const size_t value) { |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 119 | Guard g(mutex_); |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 120 | pendingTaskCountMax_ = value; |
| 121 | } |
| 122 | |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 123 | void add(shared_ptr<Runnable> value, int64_t timeout, int64_t expiration) override; |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 124 | |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 125 | void remove(shared_ptr<Runnable> task) override; |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 126 | |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 127 | shared_ptr<Runnable> removeNextPending() override; |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 128 | |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 129 | void removeExpiredTasks() override { |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 130 | removeExpired(false); |
| 131 | } |
David Reiss | 068f416 | 2010-03-09 05:19:45 +0000 | [diff] [blame] | 132 | |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 133 | void setExpireCallback(ExpireCallback expireCallback) override; |
David Reiss | 068f416 | 2010-03-09 05:19:45 +0000 | [diff] [blame] | 134 | |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 135 | private: |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 136 | /** |
| 137 | * Remove one or more expired tasks. |
| 138 | * \param[in] justOne if true, try to remove just one task and return |
| 139 | */ |
| 140 | void removeExpired(bool justOne); |
| 141 | |
| 142 | /** |
| 143 | * \returns whether it is acceptable to block, depending on the current thread id |
| 144 | */ |
| 145 | bool canSleep() const; |
| 146 | |
| 147 | /** |
| 148 | * Lowers the maximum worker count and blocks until enough worker threads complete |
| 149 | * to get to the new maximum worker limit. The caller is responsible for acquiring |
| 150 | * a lock on the class mutex_. |
| 151 | */ |
| 152 | void removeWorkersUnderLock(size_t value); |
Mark Slee | 7c10eaf | 2007-03-01 02:45:10 +0000 | [diff] [blame] | 153 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 154 | size_t workerCount_; |
| 155 | size_t workerMaxCount_; |
| 156 | size_t idleCount_; |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 157 | size_t pendingTaskCountMax_; |
David Reiss | 068f416 | 2010-03-09 05:19:45 +0000 | [diff] [blame] | 158 | size_t expiredCount_; |
| 159 | ExpireCallback expireCallback_; |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 160 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 161 | ThreadManager::STATE state_; |
| 162 | shared_ptr<ThreadFactory> threadFactory_; |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 163 | |
| 164 | friend class ThreadManager::Task; |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 165 | typedef std::deque<shared_ptr<Task> > TaskQueue; |
| 166 | TaskQueue tasks_; |
David Reiss | a0dbfef | 2010-03-09 05:19:32 +0000 | [diff] [blame] | 167 | Mutex mutex_; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 168 | Monitor monitor_; |
David Reiss | a0dbfef | 2010-03-09 05:19:32 +0000 | [diff] [blame] | 169 | Monitor maxMonitor_; |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 170 | Monitor workerMonitor_; // used to synchronize changes in worker count |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 171 | |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 172 | friend class ThreadManager::Worker; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 173 | std::set<shared_ptr<Thread> > workers_; |
| 174 | std::set<shared_ptr<Thread> > deadWorkers_; |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 175 | std::map<const Thread::id_t, shared_ptr<Thread> > idMap_; |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 176 | }; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 177 | |
| 178 | class ThreadManager::Task : public Runnable { |
| 179 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 180 | public: |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 181 | enum STATE { WAITING, EXECUTING, TIMEDOUT, COMPLETE }; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 182 | |
cyy | bfdbd03 | 2019-01-12 14:38:28 +0800 | [diff] [blame] | 183 | Task(shared_ptr<Runnable> runnable, uint64_t expiration = 0ULL) |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 184 | : runnable_(runnable), |
cyy | bfdbd03 | 2019-01-12 14:38:28 +0800 | [diff] [blame] | 185 | state_(WAITING) { |
| 186 | if (expiration != 0ULL) { |
| 187 | expireTime_.reset(new std::chrono::steady_clock::time_point(std::chrono::steady_clock::now() + std::chrono::milliseconds(expiration))); |
| 188 | } |
| 189 | } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 190 | |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 191 | ~Task() override = default; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 192 | |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 193 | void run() override { |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 194 | if (state_ == EXECUTING) { |
| 195 | runnable_->run(); |
| 196 | state_ = COMPLETE; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 200 | shared_ptr<Runnable> getRunnable() { return runnable_; } |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 201 | |
cyy | bfdbd03 | 2019-01-12 14:38:28 +0800 | [diff] [blame] | 202 | const unique_ptr<std::chrono::steady_clock::time_point> & getExpireTime() const { return expireTime_; } |
David Reiss | 068f416 | 2010-03-09 05:19:45 +0000 | [diff] [blame] | 203 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 204 | private: |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 205 | shared_ptr<Runnable> runnable_; |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 206 | friend class ThreadManager::Worker; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 207 | STATE state_; |
cyy | bfdbd03 | 2019-01-12 14:38:28 +0800 | [diff] [blame] | 208 | unique_ptr<std::chrono::steady_clock::time_point> expireTime_; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 209 | }; |
| 210 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 211 | class ThreadManager::Worker : public Runnable { |
| 212 | enum STATE { UNINITIALIZED, STARTING, STARTED, STOPPING, STOPPED }; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 213 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 214 | public: |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 215 | Worker(ThreadManager::Impl* manager) : manager_(manager), state_(UNINITIALIZED) {} |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 216 | |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 217 | ~Worker() override = default; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 218 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 219 | private: |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 220 | bool isActive() const { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 221 | return (manager_->workerCount_ <= manager_->workerMaxCount_) |
| 222 | || (manager_->state_ == JOINING && !manager_->tasks_.empty()); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 223 | } |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 224 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 225 | public: |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 226 | /** |
| 227 | * Worker entry point |
| 228 | * |
| 229 | * As long as worker thread is running, pull tasks off the task queue and |
| 230 | * execute. |
| 231 | */ |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 232 | void run() override { |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 233 | Guard g(manager_->mutex_); |
| 234 | |
| 235 | /** |
| 236 | * This method has three parts; one is to check for and account for |
| 237 | * admitting a task which happens under a lock. Then the lock is released |
| 238 | * and the task itself is executed. Finally we do some accounting |
| 239 | * under lock again when the task completes. |
| 240 | */ |
| 241 | |
| 242 | /** |
| 243 | * Admitting |
| 244 | */ |
| 245 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 246 | /** |
| 247 | * Increment worker semaphore and notify manager if worker count reached |
| 248 | * desired max |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 249 | */ |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 250 | bool active = manager_->workerCount_ < manager_->workerMaxCount_; |
| 251 | if (active) { |
| 252 | if (++manager_->workerCount_ == manager_->workerMaxCount_) { |
Jim King | 61b1708 | 2016-04-19 15:57:31 -0400 | [diff] [blame] | 253 | manager_->workerMonitor_.notify(); |
| 254 | } |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 255 | } |
| 256 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 257 | while (active) { |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 258 | /** |
| 259 | * While holding manager monitor block for non-empty task queue (Also |
| 260 | * check that the thread hasn't been requested to stop). Once the queue |
| 261 | * is non-empty, dequeue a task, release monitor, and execute. If the |
| 262 | * worker max count has been decremented such that we exceed it, mark |
| 263 | * ourself inactive, decrement the worker count and notify the manager |
| 264 | * (technically we're notifying the next blocked thread but eventually |
| 265 | * the manager will see it. |
| 266 | */ |
| 267 | active = isActive(); |
| 268 | |
| 269 | while (active && manager_->tasks_.empty()) { |
| 270 | manager_->idleCount_++; |
| 271 | manager_->monitor_.wait(); |
| 272 | active = isActive(); |
| 273 | manager_->idleCount_--; |
| 274 | } |
| 275 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 276 | shared_ptr<ThreadManager::Task> task; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 277 | |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 278 | if (active) { |
| 279 | if (!manager_->tasks_.empty()) { |
| 280 | task = manager_->tasks_.front(); |
| 281 | manager_->tasks_.pop_front(); |
| 282 | if (task->state_ == ThreadManager::Task::WAITING) { |
| 283 | // If the state is changed to anything other than EXECUTING or TIMEDOUT here |
| 284 | // then the execution loop needs to be changed below. |
| 285 | task->state_ = |
cyy | bfdbd03 | 2019-01-12 14:38:28 +0800 | [diff] [blame] | 286 | (task->getExpireTime() && *(task->getExpireTime()) < std::chrono::steady_clock::now()) ? |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 287 | ThreadManager::Task::TIMEDOUT : |
| 288 | ThreadManager::Task::EXECUTING; |
| 289 | } |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 290 | } |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 291 | |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 292 | /* If we have a pending task max and we just dropped below it, wakeup any |
| 293 | thread that might be blocked on add. */ |
| 294 | if (manager_->pendingTaskCountMax_ != 0 |
| 295 | && manager_->tasks_.size() <= manager_->pendingTaskCountMax_ - 1) { |
| 296 | manager_->maxMonitor_.notify(); |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 297 | } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 298 | } |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 299 | |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 300 | /** |
| 301 | * Execution - not holding a lock |
| 302 | */ |
Roger Meier | 7295745 | 2013-06-29 00:28:50 +0200 | [diff] [blame] | 303 | if (task) { |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 304 | if (task->state_ == ThreadManager::Task::EXECUTING) { |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 305 | |
| 306 | // Release the lock so we can run the task without blocking the thread manager |
| 307 | manager_->mutex_.unlock(); |
| 308 | |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 309 | try { |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 310 | task->run(); |
Konrad Grochowski | 3876ea7 | 2014-12-09 15:24:56 +0100 | [diff] [blame] | 311 | } catch (const std::exception& e) { |
Jens Geyer | fb05cf6 | 2014-12-04 21:49:07 +0100 | [diff] [blame] | 312 | GlobalOutput.printf("[ERROR] task->run() raised an exception: %s", e.what()); |
Konrad Grochowski | 3876ea7 | 2014-12-09 15:24:56 +0100 | [diff] [blame] | 313 | } catch (...) { |
Jens Geyer | fb05cf6 | 2014-12-04 21:49:07 +0100 | [diff] [blame] | 314 | GlobalOutput.printf("[ERROR] task->run() raised an unknown exception"); |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 315 | } |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 316 | |
| 317 | // Re-acquire the lock to proceed in the thread manager |
| 318 | manager_->mutex_.lock(); |
| 319 | |
| 320 | } else if (manager_->expireCallback_) { |
| 321 | // The only other state the task could have been in is TIMEDOUT (see above) |
Kanishth Karthik | 8ec5857 | 2020-03-26 01:29:50 +0530 | [diff] [blame] | 322 | manager_->mutex_.unlock(); |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 323 | manager_->expireCallback_(task->getRunnable()); |
Kanishth Karthik | 8ec5857 | 2020-03-26 01:29:50 +0530 | [diff] [blame] | 324 | manager_->mutex_.lock(); |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 325 | manager_->expiredCount_++; |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 326 | } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 327 | } |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 328 | } |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 329 | |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 330 | /** |
| 331 | * Final accounting for the worker thread that is done working |
| 332 | */ |
| 333 | manager_->deadWorkers_.insert(this->thread()); |
| 334 | if (--manager_->workerCount_ == manager_->workerMaxCount_) { |
| 335 | manager_->workerMonitor_.notify(); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 336 | } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 337 | } |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 338 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 339 | private: |
| 340 | ThreadManager::Impl* manager_; |
| 341 | friend class ThreadManager::Impl; |
| 342 | STATE state_; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 343 | }; |
| 344 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 345 | void ThreadManager::Impl::addWorker(size_t value) { |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 346 | std::set<shared_ptr<Thread> > newThreads; |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 347 | for (size_t ix = 0; ix < value; ix++) { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 348 | shared_ptr<ThreadManager::Worker> worker |
cyy | 6475016 | 2019-02-08 13:40:59 +0800 | [diff] [blame] | 349 | = std::make_shared<ThreadManager::Worker>(this); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 350 | newThreads.insert(threadFactory_->newThread(worker)); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 351 | } |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 352 | |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 353 | Guard g(mutex_); |
| 354 | workerMaxCount_ += value; |
| 355 | workers_.insert(newThreads.begin(), newThreads.end()); |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 356 | |
cyy | 6475016 | 2019-02-08 13:40:59 +0800 | [diff] [blame] | 357 | for (const auto & newThread : newThreads) { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 358 | shared_ptr<ThreadManager::Worker> worker |
cyy | 6475016 | 2019-02-08 13:40:59 +0800 | [diff] [blame] | 359 | = dynamic_pointer_cast<ThreadManager::Worker, Runnable>(newThread->runnable()); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 360 | worker->state_ = ThreadManager::Worker::STARTING; |
cyy | 6475016 | 2019-02-08 13:40:59 +0800 | [diff] [blame] | 361 | newThread->start(); |
| 362 | idMap_.insert(std::pair<const Thread::id_t, shared_ptr<Thread> >(newThread->getId(), newThread)); |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 363 | } |
| 364 | |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 365 | while (workerCount_ != workerMaxCount_) { |
| 366 | workerMonitor_.wait(); |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 367 | } |
| 368 | } |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 369 | |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 370 | void ThreadManager::Impl::start() { |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 371 | Guard g(mutex_); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 372 | if (state_ == ThreadManager::STOPPED) { |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 373 | return; |
| 374 | } |
| 375 | |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 376 | if (state_ == ThreadManager::UNINITIALIZED) { |
| 377 | if (!threadFactory_) { |
| 378 | throw InvalidArgumentException(); |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 379 | } |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 380 | state_ = ThreadManager::STARTED; |
| 381 | monitor_.notifyAll(); |
| 382 | } |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 383 | |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 384 | while (state_ == STARTING) { |
| 385 | monitor_.wait(); |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 386 | } |
| 387 | } |
| 388 | |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 389 | void ThreadManager::Impl::stop() { |
| 390 | Guard g(mutex_); |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 391 | bool doStop = false; |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 392 | |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 393 | if (state_ != ThreadManager::STOPPING && state_ != ThreadManager::JOINING |
| 394 | && state_ != ThreadManager::STOPPED) { |
| 395 | doStop = true; |
| 396 | state_ = ThreadManager::JOINING; |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 397 | } |
| 398 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 399 | if (doStop) { |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 400 | removeWorkersUnderLock(workerCount_); |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 401 | } |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 402 | |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 403 | state_ = ThreadManager::STOPPED; |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 404 | } |
Mark Slee | 7c10eaf | 2007-03-01 02:45:10 +0000 | [diff] [blame] | 405 | |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 406 | void ThreadManager::Impl::removeWorker(size_t value) { |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 407 | Guard g(mutex_); |
| 408 | removeWorkersUnderLock(value); |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 409 | } |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 410 | |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 411 | void ThreadManager::Impl::removeWorkersUnderLock(size_t value) { |
| 412 | if (value > workerMaxCount_) { |
| 413 | throw InvalidArgumentException(); |
| 414 | } |
| 415 | |
| 416 | workerMaxCount_ -= value; |
| 417 | |
| 418 | if (idleCount_ > value) { |
| 419 | // There are more idle workers than we need to remove, |
| 420 | // so notify enough of them so they can terminate. |
| 421 | for (size_t ix = 0; ix < value; ix++) { |
| 422 | monitor_.notify(); |
| 423 | } |
| 424 | } else { |
| 425 | // There are as many or less idle workers than we need to remove, |
| 426 | // so just notify them all so they can terminate. |
| 427 | monitor_.notifyAll(); |
| 428 | } |
| 429 | |
| 430 | while (workerCount_ != workerMaxCount_) { |
| 431 | workerMonitor_.wait(); |
| 432 | } |
| 433 | |
cyy | 6475016 | 2019-02-08 13:40:59 +0800 | [diff] [blame] | 434 | for (const auto & deadWorker : deadWorkers_) { |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 435 | |
| 436 | // when used with a joinable thread factory, we join the threads as we remove them |
| 437 | if (!threadFactory_->isDetached()) { |
cyy | 6475016 | 2019-02-08 13:40:59 +0800 | [diff] [blame] | 438 | deadWorker->join(); |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 439 | } |
| 440 | |
cyy | 6475016 | 2019-02-08 13:40:59 +0800 | [diff] [blame] | 441 | idMap_.erase(deadWorker->getId()); |
| 442 | workers_.erase(deadWorker); |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | deadWorkers_.clear(); |
| 446 | } |
| 447 | |
| 448 | bool ThreadManager::Impl::canSleep() const { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 449 | const Thread::id_t id = threadFactory_->getCurrentThreadId(); |
| 450 | return idMap_.find(id) == idMap_.end(); |
| 451 | } |
| 452 | |
| 453 | void ThreadManager::Impl::add(shared_ptr<Runnable> value, int64_t timeout, int64_t expiration) { |
| 454 | Guard g(mutex_, timeout); |
| 455 | |
| 456 | if (!g) { |
| 457 | throw TimedOutException(); |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 458 | } |
| 459 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 460 | if (state_ != ThreadManager::STARTED) { |
| 461 | throw IllegalStateException( |
| 462 | "ThreadManager::Impl::add ThreadManager " |
| 463 | "not started"); |
| 464 | } |
David Reiss | 4e19f19 | 2010-03-09 05:19:59 +0000 | [diff] [blame] | 465 | |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 466 | // if we're at a limit, remove an expired task to see if the limit clears |
| 467 | if (pendingTaskCountMax_ > 0 && (tasks_.size() >= pendingTaskCountMax_)) { |
| 468 | removeExpired(true); |
| 469 | } |
| 470 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 471 | if (pendingTaskCountMax_ > 0 && (tasks_.size() >= pendingTaskCountMax_)) { |
| 472 | if (canSleep() && timeout >= 0) { |
| 473 | while (pendingTaskCountMax_ > 0 && tasks_.size() >= pendingTaskCountMax_) { |
| 474 | // This is thread safe because the mutex is shared between monitors. |
| 475 | maxMonitor_.wait(timeout); |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 476 | } |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 477 | } else { |
| 478 | throw TooManyPendingTasksException(); |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 479 | } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 480 | } |
| 481 | |
cyy | 6475016 | 2019-02-08 13:40:59 +0800 | [diff] [blame] | 482 | tasks_.push_back(std::make_shared<ThreadManager::Task>(value, expiration)); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 483 | |
| 484 | // If idle thread is available notify it, otherwise all worker threads are |
| 485 | // running and will get around to this task in time. |
| 486 | if (idleCount_ > 0) { |
| 487 | monitor_.notify(); |
| 488 | } |
| 489 | } |
| 490 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 491 | void ThreadManager::Impl::remove(shared_ptr<Runnable> task) { |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 492 | Guard g(mutex_); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 493 | if (state_ != ThreadManager::STARTED) { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 494 | throw IllegalStateException( |
| 495 | "ThreadManager::Impl::remove ThreadManager not " |
| 496 | "started"); |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 497 | } |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 498 | |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 499 | for (auto it = tasks_.begin(); it != tasks_.end(); ++it) |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 500 | { |
| 501 | if ((*it)->getRunnable() == task) |
| 502 | { |
| 503 | tasks_.erase(it); |
| 504 | return; |
| 505 | } |
| 506 | } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 507 | } |
| 508 | |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 509 | std::shared_ptr<Runnable> ThreadManager::Impl::removeNextPending() { |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 510 | Guard g(mutex_); |
| 511 | if (state_ != ThreadManager::STARTED) { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 512 | throw IllegalStateException( |
| 513 | "ThreadManager::Impl::removeNextPending " |
| 514 | "ThreadManager not started"); |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | if (tasks_.empty()) { |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 518 | return std::shared_ptr<Runnable>(); |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | shared_ptr<ThreadManager::Task> task = tasks_.front(); |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 522 | tasks_.pop_front(); |
Roger Meier | 8b51bc6 | 2014-07-24 23:33:33 +0200 | [diff] [blame] | 523 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 524 | return task->getRunnable(); |
| 525 | } |
| 526 | |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 527 | void ThreadManager::Impl::removeExpired(bool justOne) { |
| 528 | // this is always called under a lock |
cyy | bfdbd03 | 2019-01-12 14:38:28 +0800 | [diff] [blame] | 529 | if (tasks_.empty()) { |
| 530 | return; |
| 531 | } |
| 532 | auto now = std::chrono::steady_clock::now(); |
David Reiss | 068f416 | 2010-03-09 05:19:45 +0000 | [diff] [blame] | 533 | |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 534 | for (auto it = tasks_.begin(); it != tasks_.end(); ) |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 535 | { |
cyy | bfdbd03 | 2019-01-12 14:38:28 +0800 | [diff] [blame] | 536 | if ((*it)->getExpireTime() && *((*it)->getExpireTime()) < now) { |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 537 | if (expireCallback_) { |
| 538 | expireCallback_((*it)->getRunnable()); |
| 539 | } |
| 540 | it = tasks_.erase(it); |
| 541 | ++expiredCount_; |
| 542 | if (justOne) { |
| 543 | return; |
| 544 | } |
David Reiss | 068f416 | 2010-03-09 05:19:45 +0000 | [diff] [blame] | 545 | } |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 546 | else |
| 547 | { |
| 548 | ++it; |
David Reiss | 068f416 | 2010-03-09 05:19:45 +0000 | [diff] [blame] | 549 | } |
David Reiss | 068f416 | 2010-03-09 05:19:45 +0000 | [diff] [blame] | 550 | } |
| 551 | } |
| 552 | |
David Reiss | 068f416 | 2010-03-09 05:19:45 +0000 | [diff] [blame] | 553 | void ThreadManager::Impl::setExpireCallback(ExpireCallback expireCallback) { |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 554 | Guard g(mutex_); |
David Reiss | 068f416 | 2010-03-09 05:19:45 +0000 | [diff] [blame] | 555 | expireCallback_ = expireCallback; |
| 556 | } |
| 557 | |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 558 | class SimpleThreadManager : public ThreadManager::Impl { |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 559 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 560 | public: |
| 561 | SimpleThreadManager(size_t workerCount = 4, size_t pendingTaskCountMax = 0) |
| 562 | : workerCount_(workerCount), pendingTaskCountMax_(pendingTaskCountMax) {} |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 563 | |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 564 | void start() override { |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 565 | ThreadManager::Impl::pendingTaskCountMax(pendingTaskCountMax_); |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 566 | ThreadManager::Impl::start(); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 567 | addWorker(workerCount_); |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 568 | } |
| 569 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 570 | private: |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 571 | const size_t workerCount_; |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 572 | const size_t pendingTaskCountMax_; |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 573 | }; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 574 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 575 | shared_ptr<ThreadManager> ThreadManager::newThreadManager() { |
| 576 | return shared_ptr<ThreadManager>(new ThreadManager::Impl()); |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 577 | } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 578 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 579 | shared_ptr<ThreadManager> ThreadManager::newSimpleThreadManager(size_t count, |
| 580 | size_t pendingTaskCountMax) { |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 581 | return shared_ptr<ThreadManager>(new SimpleThreadManager(count, pendingTaskCountMax)); |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 582 | } |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 583 | } |
| 584 | } |
| 585 | } // apache::thrift::concurrency |