Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 1 | #if !defined(_concurrency_ThreadManager_h_) |
| 2 | #define _concurrency_ThreadManager_h_ 1 |
| 3 | |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 4 | #include <sys/types.h> |
| 5 | |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 6 | #include "Thread.h" |
| 7 | |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 8 | namespace facebook { namespace thrift { namespace concurrency { |
| 9 | |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 10 | /** Thread Pool Manager and related classes |
| 11 | |
| 12 | @author marc |
| 13 | @version $Id:$ */ |
| 14 | |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 15 | class ThreadManager; |
| 16 | |
| 17 | /** PoolPolicy class |
| 18 | |
| 19 | Tracks performance of ThreadManager object and makes desired changes in thread pool count if any. */ |
| 20 | |
| 21 | class PoolPolicy { |
| 22 | |
| 23 | public: |
| 24 | |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 25 | PoolPolicy() {} |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 26 | |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 27 | virtual ~PoolPolicy() {} |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 28 | |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 29 | virtual void onEmpty(ThreadManager* source) const = 0; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 30 | |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 31 | virtual void onLowWatermark(ThreadManager* source) const = 0; |
| 32 | |
| 33 | virtual void onHighWatermark(ThreadManager* source) const = 0; |
| 34 | |
| 35 | }; |
| 36 | |
| 37 | class BasicPoolPolicy : public PoolPolicy { |
| 38 | |
| 39 | public: |
| 40 | |
| 41 | BasicPoolPolicy(); |
| 42 | |
| 43 | virtual ~BasicPoolPolicy(); |
| 44 | |
| 45 | virtual void onEmpty(ThreadManager* source) const; |
| 46 | |
| 47 | virtual void onLowWatermark(ThreadManager* source) const; |
| 48 | |
| 49 | virtual void onHighWatermark(ThreadManager* source) const; |
| 50 | |
| 51 | private: |
| 52 | |
| 53 | class Impl; |
| 54 | |
| 55 | Impl* _impl; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 56 | }; |
| 57 | |
| 58 | /** ThreadManager class |
| 59 | |
| 60 | This class manages a pool of threads. It uses a ThreadFactory to create threads. It never actually creates or destroys worker threads, rather |
| 61 | it maintains statistics on number of idle threads, number of active threads, task backlog, and average wait and service times and informs the |
| 62 | PoolPolicy object bound to instances of this manager of interesting transitions. It is then up the PoolPolicy object to decide if the thread pool |
| 63 | size needs to be adjusted and call this object addThread and removeThread methods to make changes. |
| 64 | |
| 65 | This design allows different policy implementations to used this code to handle basic worker thread management and worker task execution and focus on |
| 66 | policy issues. The simplest policy, StaticPolicy, does nothing other than create a fixed number of threads. */ |
| 67 | |
| 68 | class ThreadManager { |
| 69 | |
| 70 | public: |
| 71 | |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 72 | ThreadManager(size_t highWatermark=4, size_t lowWatermark=2) {}; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 73 | |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 74 | virtual ~ThreadManager() {}; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 75 | |
| 76 | virtual const PoolPolicy* poolPolicy() const = 0; |
| 77 | |
| 78 | virtual void poolPolicy(const PoolPolicy* value) = 0; |
| 79 | |
| 80 | virtual const ThreadFactory* threadFactory() const = 0; |
| 81 | |
| 82 | virtual void threadFactory(const ThreadFactory* value) = 0; |
| 83 | |
| 84 | virtual size_t highWatermark() const = 0; |
| 85 | |
| 86 | virtual void highWatermark(size_t value) = 0; |
| 87 | |
| 88 | virtual size_t lowWatermark() const = 0; |
| 89 | |
| 90 | virtual void lowWatermark(size_t value) = 0; |
| 91 | |
| 92 | virtual void addThread(size_t value=1) = 0; |
| 93 | |
| 94 | virtual void removeThread(size_t value=1) = 0; |
| 95 | |
| 96 | /** Gets the current number of idle worker threads */ |
| 97 | |
| 98 | virtual size_t idleWorkerCount() const = 0; |
| 99 | |
| 100 | /** Gets the current number of total worker threads */ |
| 101 | |
| 102 | virtual size_t workerCount() const = 0; |
| 103 | |
| 104 | /** Gets the current number of pending tasks */ |
| 105 | |
| 106 | virtual size_t pendingTaskCount() const = 0; |
| 107 | |
| 108 | /** Gets the current number of pending and executing tasks */ |
| 109 | |
| 110 | virtual size_t totalTaskCount() const = 0; |
| 111 | |
| 112 | /** Adds a task to be execued at some time in the future by a worker thread. */ |
| 113 | |
| 114 | virtual void add(Runnable* value) = 0; |
| 115 | |
| 116 | /** Removes a pending task */ |
| 117 | |
| 118 | virtual void remove(Runnable* task) = 0; |
| 119 | |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 120 | static ThreadManager* newThreadManager(size_t lowWatermark=2, size_t highWatermark=4); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 121 | |
| 122 | class Task; |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 123 | |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 124 | class Worker; |
| 125 | |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 126 | class Impl; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 127 | }; |
| 128 | |
| 129 | }}} // facebook::thrift::concurrency |
| 130 | |
| 131 | #endif // !defined(_concurrency_ThreadManager_h_) |