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 | |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 17 | /** ThreadManager class |
| 18 | |
| 19 | This class manages a pool of threads. It uses a ThreadFactory to create threads. It never actually creates or destroys worker threads, rather |
| 20 | it maintains statistics on number of idle threads, number of active threads, task backlog, and average wait and service times and informs the |
| 21 | PoolPolicy object bound to instances of this manager of interesting transitions. It is then up the PoolPolicy object to decide if the thread pool |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame^] | 22 | size needs to be adjusted and call this object addWorker and removeWorker methods to make changes. |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 23 | |
| 24 | This design allows different policy implementations to used this code to handle basic worker thread management and worker task execution and focus on |
| 25 | policy issues. The simplest policy, StaticPolicy, does nothing other than create a fixed number of threads. */ |
| 26 | |
| 27 | class ThreadManager { |
| 28 | |
| 29 | public: |
| 30 | |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame^] | 31 | ThreadManager() {} |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 32 | |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame^] | 33 | virtual ~ThreadManager() {} |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 34 | |
| 35 | virtual const ThreadFactory* threadFactory() const = 0; |
| 36 | |
| 37 | virtual void threadFactory(const ThreadFactory* value) = 0; |
| 38 | |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame^] | 39 | virtual void addWorker(size_t value=1) = 0; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 40 | |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame^] | 41 | virtual void removeWorker(size_t value=1) = 0; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 42 | |
| 43 | /** Gets the current number of idle worker threads */ |
| 44 | |
| 45 | virtual size_t idleWorkerCount() const = 0; |
| 46 | |
| 47 | /** Gets the current number of total worker threads */ |
| 48 | |
| 49 | virtual size_t workerCount() const = 0; |
| 50 | |
| 51 | /** Gets the current number of pending tasks */ |
| 52 | |
| 53 | virtual size_t pendingTaskCount() const = 0; |
| 54 | |
| 55 | /** Gets the current number of pending and executing tasks */ |
| 56 | |
| 57 | virtual size_t totalTaskCount() const = 0; |
| 58 | |
| 59 | /** Adds a task to be execued at some time in the future by a worker thread. */ |
| 60 | |
| 61 | virtual void add(Runnable* value) = 0; |
| 62 | |
| 63 | /** Removes a pending task */ |
| 64 | |
| 65 | virtual void remove(Runnable* task) = 0; |
| 66 | |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame^] | 67 | static ThreadManager* newThreadManager(); |
| 68 | |
| 69 | static ThreadManager* newSimpleThreadManager(); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 70 | |
| 71 | class Task; |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 72 | |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 73 | class Worker; |
| 74 | |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 75 | class Impl; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | }}} // facebook::thrift::concurrency |
| 79 | |
| 80 | #endif // !defined(_concurrency_ThreadManager_h_) |