Mark Slee | 9f0c651 | 2007-02-28 23:58:26 +0000 | [diff] [blame] | 1 | // Copyright (c) 2006- Facebook |
| 2 | // Distributed under the Thrift Software License |
| 3 | // |
| 4 | // See accompanying file LICENSE or visit the Thrift site at: |
| 5 | // http://developers.facebook.com/thrift/ |
| 6 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 7 | #ifndef _THRIFT_CONCURRENCY_THREADMANAGER_H_ |
| 8 | #define _THRIFT_CONCURRENCY_THREADMANAGER_H_ 1 |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 9 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 10 | #include <boost/shared_ptr.hpp> |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 11 | #include <sys/types.h> |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 12 | #include "Thread.h" |
| 13 | |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 14 | namespace facebook { namespace thrift { namespace concurrency { |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 15 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 16 | /** |
| 17 | * Thread Pool Manager and related classes |
| 18 | * |
| 19 | * @author marc |
| 20 | * @version $Id:$ |
| 21 | */ |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 22 | class ThreadManager; |
| 23 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 24 | /** |
| 25 | * ThreadManager class |
| 26 | * |
| 27 | * This class manages a pool of threads. It uses a ThreadFactory to create |
| 28 | * threads. It never actually creates or destroys worker threads, rather |
| 29 | * It maintains statistics on number of idle threads, number of active threads, |
| 30 | * task backlog, and average wait and service times and informs the PoolPolicy |
| 31 | * object bound to instances of this manager of interesting transitions. It is |
| 32 | * then up the PoolPolicy object to decide if the thread pool size needs to be |
| 33 | * adjusted and call this object addWorker and removeWorker methods to make |
| 34 | * changes. |
| 35 | * |
| 36 | * This design allows different policy implementations to used this code to |
| 37 | * handle basic worker thread management and worker task execution and focus on |
| 38 | * policy issues. The simplest policy, StaticPolicy, does nothing other than |
| 39 | * create a fixed number of threads. |
| 40 | */ |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 41 | class ThreadManager { |
| 42 | |
Mark Slee | 8cbda85 | 2007-02-01 23:05:38 +0000 | [diff] [blame] | 43 | protected: |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 44 | ThreadManager() {} |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 45 | |
Mark Slee | 8cbda85 | 2007-02-01 23:05:38 +0000 | [diff] [blame] | 46 | public: |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 47 | virtual ~ThreadManager() {} |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 48 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 49 | /** |
| 50 | * Starts the thread manager. Verifies all attributes have been properly |
| 51 | * initialized, then allocates necessary resources to begin operation |
| 52 | */ |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 53 | virtual void start() = 0; |
| 54 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 55 | /** |
| 56 | * Stops the thread manager. Aborts all remaining unprocessed task, shuts |
| 57 | * down all created worker threads, and realeases all allocated resources. |
| 58 | * This method blocks for all worker threads to complete, thus it can |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 59 | * potentially block forever if a worker thread is running a task that |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 60 | * won't terminate. |
| 61 | */ |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 62 | virtual void stop() = 0; |
| 63 | |
Mark Slee | 7c10eaf | 2007-03-01 02:45:10 +0000 | [diff] [blame] | 64 | /** |
| 65 | * Joins the thread manager. This is the same as stop, except that it will |
| 66 | * block until all the workers have finished their work. At that point |
| 67 | * the ThreadManager will transition into the STOPPED state. |
| 68 | */ |
Mark Slee | 6e3f637 | 2007-03-01 22:05:46 +0000 | [diff] [blame] | 69 | virtual void join() = 0; |
| 70 | |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 71 | enum STATE { |
| 72 | UNINITIALIZED, |
| 73 | STARTING, |
| 74 | STARTED, |
Mark Slee | 7c10eaf | 2007-03-01 02:45:10 +0000 | [diff] [blame] | 75 | JOINING, |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 76 | STOPPING, |
| 77 | STOPPED |
| 78 | }; |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 79 | |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 80 | virtual const STATE state() const = 0; |
| 81 | |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 82 | virtual boost::shared_ptr<ThreadFactory> threadFactory() const = 0; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 83 | |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 84 | virtual void threadFactory(boost::shared_ptr<ThreadFactory> value) = 0; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 85 | |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 86 | virtual void addWorker(size_t value=1) = 0; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 87 | |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 88 | virtual void removeWorker(size_t value=1) = 0; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 89 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 90 | /** |
| 91 | * Gets the current number of idle worker threads |
| 92 | */ |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 93 | virtual size_t idleWorkerCount() const = 0; |
| 94 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 95 | /** |
| 96 | * Gets the current number of total worker threads |
| 97 | */ |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 98 | virtual size_t workerCount() const = 0; |
| 99 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 100 | /** |
| 101 | * Gets the current number of pending tasks |
| 102 | */ |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 103 | virtual size_t pendingTaskCount() const = 0; |
| 104 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 105 | /** |
| 106 | * Gets the current number of pending and executing tasks |
| 107 | */ |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 108 | virtual size_t totalTaskCount() const = 0; |
| 109 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 110 | /** |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 111 | * Gets the maximum pending task count. 0 indicates no maximum |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 112 | */ |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 113 | virtual size_t pendingTaskCountMax() const = 0; |
| 114 | |
| 115 | /** |
| 116 | * Adds a task to be executed at some time in the future by a worker thread. |
| 117 | * |
| 118 | * This method will block if pendingTaskCountMax() in not zero and pendingTaskCount() |
| 119 | * is greater than or equalt to pendingTaskCountMax(). If this method is called in the |
| 120 | * context of a ThreadManager worker thread it will throw a |
| 121 | * TooManyPendingTasksException |
| 122 | * |
| 123 | * @param task The task to queue for execution |
| 124 | * |
| 125 | * @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] | 126 | * is specified. Specific cases: |
| 127 | * timeout = 0 : Wait forever to queue task. |
| 128 | * timeout = -1 : Return immediately if pending task count exceeds specified max |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 129 | * |
| 130 | * @throws TooManyPendingTasksException Pending task count exceeds max pending task count |
| 131 | */ |
Mark Slee | 9b82d27 | 2007-05-23 05:16:07 +0000 | [diff] [blame] | 132 | virtual void add(boost::shared_ptr<Runnable>task, int64_t timeout=0LL) = 0; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 133 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 134 | /** |
| 135 | * Removes a pending task |
| 136 | */ |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 137 | virtual void remove(boost::shared_ptr<Runnable> task) = 0; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 138 | |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 139 | static boost::shared_ptr<ThreadManager> newThreadManager(); |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 140 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 141 | /** |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 142 | * Creates a simple thread manager the uses count number of worker threads and has |
| 143 | * a pendingTaskCountMax maximum pending tasks. The default, 0, specified no limit |
| 144 | * on pending tasks |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 145 | */ |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 146 | static boost::shared_ptr<ThreadManager> newSimpleThreadManager(size_t count=4, size_t pendingTaskCountMax=0); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 147 | |
| 148 | class Task; |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 149 | |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 150 | class Worker; |
| 151 | |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 152 | class Impl; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 153 | }; |
| 154 | |
| 155 | }}} // facebook::thrift::concurrency |
| 156 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 157 | #endif // #ifndef _THRIFT_CONCURRENCY_THREADMANAGER_H_ |