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