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