blob: f365643592cc108c718016e8df8433537bf5562e [file] [log] [blame]
Marc Slemko66949872006-07-15 01:52:39 +00001#if !defined(_concurrency_ThreadManager_h_)
2#define _concurrency_ThreadManager_h_ 1
3
Marc Slemko6f038a72006-08-03 18:58:09 +00004#include <boost/shared_ptr.hpp>
5
Marc Slemko0e53ccd2006-07-17 23:51:05 +00006#include <sys/types.h>
7
Marc Slemko66949872006-07-15 01:52:39 +00008#include "Thread.h"
9
Marc Slemko66949872006-07-15 01:52:39 +000010namespace facebook { namespace thrift { namespace concurrency {
11
Marc Slemko6f038a72006-08-03 18:58:09 +000012using namespace boost;
13
Marc Slemko0e53ccd2006-07-17 23:51:05 +000014/** Thread Pool Manager and related classes
15
16 @author marc
17 @version $Id:$ */
18
Marc Slemko66949872006-07-15 01:52:39 +000019class ThreadManager;
20
Marc Slemko66949872006-07-15 01:52:39 +000021/** 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 Slemkod466b212006-07-20 00:04:18 +000026 size needs to be adjusted and call this object addWorker and removeWorker methods to make changes.
Marc Slemko66949872006-07-15 01:52:39 +000027
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
31class ThreadManager {
32
33 public:
34
Marc Slemkod466b212006-07-20 00:04:18 +000035 ThreadManager() {}
Marc Slemko66949872006-07-15 01:52:39 +000036
Marc Slemkod466b212006-07-20 00:04:18 +000037 virtual ~ThreadManager() {}
Marc Slemko66949872006-07-15 01:52:39 +000038
Marc Slemkofe5ba12e2006-07-20 21:16:27 +000039 /** 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 Slemko6f038a72006-08-03 18:58:09 +000059 virtual shared_ptr<ThreadFactory> threadFactory() const = 0;
Marc Slemko66949872006-07-15 01:52:39 +000060
Marc Slemko6f038a72006-08-03 18:58:09 +000061 virtual void threadFactory(shared_ptr<ThreadFactory> value) = 0;
Marc Slemko66949872006-07-15 01:52:39 +000062
Marc Slemkod466b212006-07-20 00:04:18 +000063 virtual void addWorker(size_t value=1) = 0;
Marc Slemko66949872006-07-15 01:52:39 +000064
Marc Slemkod466b212006-07-20 00:04:18 +000065 virtual void removeWorker(size_t value=1) = 0;
Marc Slemko66949872006-07-15 01:52:39 +000066
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 Slemko6f038a72006-08-03 18:58:09 +000083 /** Adds a task to be execued at some time in the future by a worker thread.
Marc Slemko66949872006-07-15 01:52:39 +000084
Marc Slemko6f038a72006-08-03 18:58:09 +000085 @param value The task to run */
86
87
88 virtual void add(shared_ptr<Runnable>value) = 0;
Marc Slemko66949872006-07-15 01:52:39 +000089
90 /** Removes a pending task */
91
Marc Slemko6f038a72006-08-03 18:58:09 +000092 virtual void remove(shared_ptr<Runnable> task) = 0;
Marc Slemko66949872006-07-15 01:52:39 +000093
Marc Slemko6f038a72006-08-03 18:58:09 +000094 static shared_ptr<ThreadManager> newThreadManager();
Marc Slemkod466b212006-07-20 00:04:18 +000095
Marc Slemko525c2022006-07-20 00:29:35 +000096 /** Creates a simple thread manager the uses count number of worker threads */
97
Marc Slemko6f038a72006-08-03 18:58:09 +000098 static shared_ptr<ThreadManager> newSimpleThreadManager(size_t count=4);
Marc Slemko66949872006-07-15 01:52:39 +000099
100 class Task;
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000101
Marc Slemko66949872006-07-15 01:52:39 +0000102 class Worker;
103
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000104 class Impl;
Marc Slemko66949872006-07-15 01:52:39 +0000105};
106
107}}} // facebook::thrift::concurrency
108
109#endif // !defined(_concurrency_ThreadManager_h_)