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 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 20 | #ifndef _THRIFT_CONCURRENCY_THREADMANAGER_H_ |
| 21 | #define _THRIFT_CONCURRENCY_THREADMANAGER_H_ 1 |
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 | #include <boost/shared_ptr.hpp> |
Roger Meier | 8252577 | 2012-11-16 00:38:27 +0000 | [diff] [blame] | 24 | #include <thrift/cxxfunctional.h> |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 25 | #include <sys/types.h> |
Roger Meier | 4285ba2 | 2013-06-10 21:17:23 +0200 | [diff] [blame] | 26 | #include <thrift/concurrency/Thread.h> |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 27 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 28 | namespace apache { |
| 29 | namespace thrift { |
| 30 | namespace concurrency { |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 31 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 32 | /** |
| 33 | * Thread Pool Manager and related classes |
| 34 | * |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 35 | * @version $Id:$ |
| 36 | */ |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 37 | class ThreadManager; |
| 38 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 39 | /** |
| 40 | * ThreadManager class |
| 41 | * |
| 42 | * This class manages a pool of threads. It uses a ThreadFactory to create |
| 43 | * threads. It never actually creates or destroys worker threads, rather |
Jens Geyer | 1a8e048 | 2015-04-30 20:29:20 +0200 | [diff] [blame] | 44 | * it maintains statistics on number of idle threads, number of active threads, |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 45 | * task backlog, and average wait and service times and informs the PoolPolicy |
| 46 | * object bound to instances of this manager of interesting transitions. It is |
| 47 | * then up the PoolPolicy object to decide if the thread pool size needs to be |
| 48 | * adjusted and call this object addWorker and removeWorker methods to make |
| 49 | * changes. |
| 50 | * |
Jens Geyer | 1a8e048 | 2015-04-30 20:29:20 +0200 | [diff] [blame] | 51 | * This design allows different policy implementations to use this code to |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 52 | * handle basic worker thread management and worker task execution and focus on |
| 53 | * policy issues. The simplest policy, StaticPolicy, does nothing other than |
| 54 | * create a fixed number of threads. |
| 55 | */ |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 56 | class ThreadManager { |
| 57 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 58 | protected: |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 59 | ThreadManager() {} |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 60 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 61 | public: |
Roger Meier | 8252577 | 2012-11-16 00:38:27 +0000 | [diff] [blame] | 62 | typedef apache::thrift::stdcxx::function<void(boost::shared_ptr<Runnable>)> ExpireCallback; |
David Reiss | 068f416 | 2010-03-09 05:19:45 +0000 | [diff] [blame] | 63 | |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 64 | virtual ~ThreadManager() {} |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 65 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 66 | /** |
| 67 | * Starts the thread manager. Verifies all attributes have been properly |
| 68 | * initialized, then allocates necessary resources to begin operation |
| 69 | */ |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 70 | virtual void start() = 0; |
| 71 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 72 | /** |
| 73 | * Stops the thread manager. Aborts all remaining unprocessed task, shuts |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 74 | * down all created worker threads, and releases all allocated resources. |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 75 | * This method blocks for all worker threads to complete, thus it can |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 76 | * potentially block forever if a worker thread is running a task that |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 77 | * won't terminate. |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 78 | * |
| 79 | * Worker threads will be joined depending on the threadFactory's detached |
| 80 | * disposition. |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 81 | */ |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 82 | virtual void stop() = 0; |
| 83 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 84 | enum STATE { UNINITIALIZED, STARTING, STARTED, JOINING, STOPPING, STOPPED }; |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 85 | |
Roger Meier | 3b771a1 | 2010-11-17 22:11:26 +0000 | [diff] [blame] | 86 | virtual STATE state() const = 0; |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 87 | |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 88 | /** |
| 89 | * \returns the current thread factory |
| 90 | */ |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 91 | virtual boost::shared_ptr<ThreadFactory> threadFactory() const = 0; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 92 | |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 93 | /** |
| 94 | * Set the thread factory. |
| 95 | * \throws InvalidArgumentException if the new thread factory has a different |
| 96 | * detached disposition than the one replacing it |
| 97 | */ |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 98 | virtual void threadFactory(boost::shared_ptr<ThreadFactory> value) = 0; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 99 | |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 100 | /** |
| 101 | * Adds worker thread(s). |
| 102 | */ |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 103 | virtual void addWorker(size_t value = 1) = 0; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 104 | |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 105 | /** |
| 106 | * Removes worker thread(s). |
| 107 | * Threads are joined if the thread factory detached disposition allows it. |
| 108 | * Blocks until the number of worker threads reaches the new limit. |
| 109 | * \param[in] value the number to remove |
| 110 | * \throws InvalidArgumentException if the value is greater than the number |
| 111 | * of workers |
| 112 | */ |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 113 | virtual void removeWorker(size_t value = 1) = 0; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 114 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 115 | /** |
| 116 | * Gets the current number of idle worker threads |
| 117 | */ |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 118 | virtual size_t idleWorkerCount() const = 0; |
| 119 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 120 | /** |
| 121 | * Gets the current number of total worker threads |
| 122 | */ |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 123 | virtual size_t workerCount() const = 0; |
| 124 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 125 | /** |
| 126 | * Gets the current number of pending tasks |
| 127 | */ |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 128 | virtual size_t pendingTaskCount() const = 0; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 129 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 130 | /** |
| 131 | * Gets the current number of pending and executing tasks |
| 132 | */ |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 133 | virtual size_t totalTaskCount() const = 0; |
| 134 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 135 | /** |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 136 | * Gets the maximum pending task count. 0 indicates no maximum |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 137 | */ |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 138 | virtual size_t pendingTaskCountMax() const = 0; |
| 139 | |
| 140 | /** |
James E. King, III | df89913 | 2016-11-12 15:16:30 -0500 | [diff] [blame] | 141 | * Gets the number of tasks which have been expired without being run |
| 142 | * since start() was called. |
David Reiss | 068f416 | 2010-03-09 05:19:45 +0000 | [diff] [blame] | 143 | */ |
| 144 | virtual size_t expiredTaskCount() = 0; |
| 145 | |
| 146 | /** |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 147 | * Adds a task to be executed at some time in the future by a worker thread. |
| 148 | * |
| 149 | * This method will block if pendingTaskCountMax() in not zero and pendingTaskCount() |
| 150 | * is greater than or equalt to pendingTaskCountMax(). If this method is called in the |
| 151 | * context of a ThreadManager worker thread it will throw a |
| 152 | * TooManyPendingTasksException |
| 153 | * |
| 154 | * @param task The task to queue for execution |
| 155 | * |
| 156 | * @param timeout Time to wait in milliseconds to add a task when a pending-task-count |
Aditya Agarwal | 4b6ff2d | 2007-12-25 22:58:50 +0000 | [diff] [blame] | 157 | * is specified. Specific cases: |
| 158 | * timeout = 0 : Wait forever to queue task. |
| 159 | * timeout = -1 : Return immediately if pending task count exceeds specified max |
David Reiss | 068f416 | 2010-03-09 05:19:45 +0000 | [diff] [blame] | 160 | * @param expiration when nonzero, the number of milliseconds the task is valid |
| 161 | * to be run; if exceeded, the task will be dropped off the queue and not run. |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 162 | * |
| 163 | * @throws TooManyPendingTasksException Pending task count exceeds max pending task count |
| 164 | */ |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 165 | virtual void add(boost::shared_ptr<Runnable> task, |
| 166 | int64_t timeout = 0LL, |
| 167 | int64_t expiration = 0LL) = 0; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 168 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 169 | /** |
| 170 | * Removes a pending task |
| 171 | */ |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 172 | virtual void remove(boost::shared_ptr<Runnable> task) = 0; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 173 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 174 | /** |
| 175 | * Remove the next pending task which would be run. |
| 176 | * |
| 177 | * @return the task removed. |
| 178 | */ |
| 179 | virtual boost::shared_ptr<Runnable> removeNextPending() = 0; |
| 180 | |
David Reiss | 068f416 | 2010-03-09 05:19:45 +0000 | [diff] [blame] | 181 | /** |
| 182 | * Remove tasks from front of task queue that have expired. |
| 183 | */ |
| 184 | virtual void removeExpiredTasks() = 0; |
| 185 | |
| 186 | /** |
| 187 | * Set a callback to be called when a task is expired and not run. |
| 188 | * |
| 189 | * @param expireCallback a function called with the shared_ptr<Runnable> for |
| 190 | * the expired task. |
| 191 | */ |
| 192 | virtual void setExpireCallback(ExpireCallback expireCallback) = 0; |
| 193 | |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 194 | static boost::shared_ptr<ThreadManager> newThreadManager(); |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 195 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 196 | /** |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 197 | * Creates a simple thread manager the uses count number of worker threads and has |
| 198 | * a pendingTaskCountMax maximum pending tasks. The default, 0, specified no limit |
| 199 | * on pending tasks |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 200 | */ |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 201 | static boost::shared_ptr<ThreadManager> newSimpleThreadManager(size_t count = 4, |
| 202 | size_t pendingTaskCountMax = 0); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 203 | |
| 204 | class Task; |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 205 | |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 206 | class Worker; |
| 207 | |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 208 | class Impl; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 209 | }; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 210 | } |
| 211 | } |
| 212 | } // apache::thrift::concurrency |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 213 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 214 | #endif // #ifndef _THRIFT_CONCURRENCY_THREADMANAGER_H_ |