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 | |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame^] | 35 | /** Starts the thread manager. Verifies all attributes have been properly initialized, then allocates necessary resources to begin operation */ |
| 36 | |
| 37 | virtual void start() = 0; |
| 38 | |
| 39 | /** Stops the thread manager. Aborts all remaining unprocessed task, shuts down all created worker threads, and realeases all allocated resources. |
| 40 | This method blocks for all worker threads to complete, thus it can potentially block forever if a worker thread is running a task that |
| 41 | won't terminate. */ |
| 42 | |
| 43 | virtual void stop() = 0; |
| 44 | |
| 45 | enum STATE { |
| 46 | UNINITIALIZED, |
| 47 | STARTING, |
| 48 | STARTED, |
| 49 | STOPPING, |
| 50 | STOPPED |
| 51 | }; |
| 52 | |
| 53 | virtual const STATE state() const = 0; |
| 54 | |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 55 | virtual const ThreadFactory* threadFactory() const = 0; |
| 56 | |
| 57 | virtual void threadFactory(const ThreadFactory* value) = 0; |
| 58 | |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 59 | virtual void addWorker(size_t value=1) = 0; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 60 | |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 61 | virtual void removeWorker(size_t value=1) = 0; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 62 | |
| 63 | /** Gets the current number of idle worker threads */ |
| 64 | |
| 65 | virtual size_t idleWorkerCount() const = 0; |
| 66 | |
| 67 | /** Gets the current number of total worker threads */ |
| 68 | |
| 69 | virtual size_t workerCount() const = 0; |
| 70 | |
| 71 | /** Gets the current number of pending tasks */ |
| 72 | |
| 73 | virtual size_t pendingTaskCount() const = 0; |
| 74 | |
| 75 | /** Gets the current number of pending and executing tasks */ |
| 76 | |
| 77 | virtual size_t totalTaskCount() const = 0; |
| 78 | |
| 79 | /** Adds a task to be execued at some time in the future by a worker thread. */ |
| 80 | |
| 81 | virtual void add(Runnable* value) = 0; |
| 82 | |
| 83 | /** Removes a pending task */ |
| 84 | |
| 85 | virtual void remove(Runnable* task) = 0; |
| 86 | |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 87 | static ThreadManager* newThreadManager(); |
| 88 | |
Marc Slemko | 525c202 | 2006-07-20 00:29:35 +0000 | [diff] [blame] | 89 | /** Creates a simple thread manager the uses count number of worker threads */ |
| 90 | |
| 91 | static ThreadManager* newSimpleThreadManager(size_t count=4); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 92 | |
| 93 | class Task; |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 94 | |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 95 | class Worker; |
| 96 | |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 97 | class Impl; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 98 | }; |
| 99 | |
| 100 | }}} // facebook::thrift::concurrency |
| 101 | |
| 102 | #endif // !defined(_concurrency_ThreadManager_h_) |