Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 1 | #ifndef _THRIFT_CONCURRENCY_THREADMANAGER_H_ |
| 2 | #define _THRIFT_CONCURRENCY_THREADMANAGER_H_ 1 |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 3 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 4 | #include <boost/shared_ptr.hpp> |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 5 | #include <sys/types.h> |
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 | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 10 | using namespace boost; |
| 11 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 12 | /** |
| 13 | * Thread Pool Manager and related classes |
| 14 | * |
| 15 | * @author marc |
| 16 | * @version $Id:$ |
| 17 | */ |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 18 | class ThreadManager; |
| 19 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 20 | /** |
| 21 | * ThreadManager class |
| 22 | * |
| 23 | * This class manages a pool of threads. It uses a ThreadFactory to create |
| 24 | * threads. It never actually creates or destroys worker threads, rather |
| 25 | * It maintains statistics on number of idle threads, number of active threads, |
| 26 | * task backlog, and average wait and service times and informs the PoolPolicy |
| 27 | * object bound to instances of this manager of interesting transitions. It is |
| 28 | * then up the PoolPolicy object to decide if the thread pool size needs to be |
| 29 | * adjusted and call this object addWorker and removeWorker methods to make |
| 30 | * changes. |
| 31 | * |
| 32 | * This design allows different policy implementations to used this code to |
| 33 | * handle basic worker thread management and worker task execution and focus on |
| 34 | * policy issues. The simplest policy, StaticPolicy, does nothing other than |
| 35 | * create a fixed number of threads. |
| 36 | */ |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 37 | class ThreadManager { |
| 38 | |
Mark Slee | 8cbda85 | 2007-02-01 23:05:38 +0000 | [diff] [blame] | 39 | protected: |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 40 | ThreadManager() {} |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 41 | |
Mark Slee | 8cbda85 | 2007-02-01 23:05:38 +0000 | [diff] [blame] | 42 | public: |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 43 | virtual ~ThreadManager() {} |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 44 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 45 | /** |
| 46 | * Starts the thread manager. Verifies all attributes have been properly |
| 47 | * initialized, then allocates necessary resources to begin operation |
| 48 | */ |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 49 | virtual void start() = 0; |
| 50 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 51 | /** |
| 52 | * Stops the thread manager. Aborts all remaining unprocessed task, shuts |
| 53 | * down all created worker threads, and realeases all allocated resources. |
| 54 | * This method blocks for all worker threads to complete, thus it can |
| 55 | * potentially block forever if a worker thread is running a task that |
| 56 | * won't terminate. |
| 57 | */ |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 58 | virtual void stop() = 0; |
| 59 | |
| 60 | enum STATE { |
| 61 | UNINITIALIZED, |
| 62 | STARTING, |
| 63 | STARTED, |
| 64 | STOPPING, |
| 65 | STOPPED |
| 66 | }; |
| 67 | |
| 68 | virtual const STATE state() const = 0; |
| 69 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 70 | virtual shared_ptr<ThreadFactory> threadFactory() const = 0; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 71 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 72 | virtual void threadFactory(shared_ptr<ThreadFactory> value) = 0; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 73 | |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 74 | virtual void addWorker(size_t value=1) = 0; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 75 | |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 76 | virtual void removeWorker(size_t value=1) = 0; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 77 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 78 | /** |
| 79 | * Gets the current number of idle worker threads |
| 80 | */ |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 81 | virtual size_t idleWorkerCount() const = 0; |
| 82 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 83 | /** |
| 84 | * Gets the current number of total worker threads |
| 85 | */ |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 86 | virtual size_t workerCount() const = 0; |
| 87 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 88 | /** |
| 89 | * Gets the current number of pending tasks |
| 90 | */ |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 91 | virtual size_t pendingTaskCount() const = 0; |
| 92 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 93 | /** |
| 94 | * Gets the current number of pending and executing tasks |
| 95 | */ |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 96 | virtual size_t totalTaskCount() const = 0; |
| 97 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 98 | /** |
| 99 | * Adds a task to be execued at some time in the future by a worker thread. |
| 100 | * |
| 101 | * @param value The task to run |
| 102 | */ |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 103 | virtual void add(shared_ptr<Runnable>value) = 0; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 104 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 105 | /** |
| 106 | * Removes a pending task |
| 107 | */ |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 108 | virtual void remove(shared_ptr<Runnable> task) = 0; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 109 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 110 | static shared_ptr<ThreadManager> newThreadManager(); |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 111 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 112 | /** |
| 113 | * Creates a simple thread manager the uses count number of worker threads |
| 114 | */ |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 115 | static shared_ptr<ThreadManager> newSimpleThreadManager(size_t count=4); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 116 | |
| 117 | class Task; |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 118 | |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 119 | class Worker; |
| 120 | |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 121 | class Impl; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 122 | }; |
| 123 | |
| 124 | }}} // facebook::thrift::concurrency |
| 125 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 126 | #endif // #ifndef _THRIFT_CONCURRENCY_THREADMANAGER_H_ |