blob: 174288186d602f198ce1ca0de81ae169c28fc754 [file] [log] [blame]
Marc Slemko66949872006-07-15 01:52:39 +00001#if !defined(_concurrency_ThreadManager_h_)
2#define _concurrency_ThreadManager_h_ 1
3
4#include "Monitor.h"
5#include "Thread.h"
6
7#include <set>
8#include <queue>
9
10namespace facebook { namespace thrift { namespace concurrency {
11
12class ThreadManager;
13
14/** PoolPolicy class
15
16 Tracks performance of ThreadManager object and makes desired changes in thread pool count if any. */
17
18class PoolPolicy {
19
20 public:
21
22 virtual ~PoolPolicy() = 0;
23
24 virtual void onlowWatermark(ThreadManager* source) const = 0;
25
26 virtual void onhighWatermark(ThreadManager* source) const = 0;
27
28};
29
30/** ThreadManager class
31
32 This class manages a pool of threads. It uses a ThreadFactory to create threads. It never actually creates or destroys worker threads, rather
33 it maintains statistics on number of idle threads, number of active threads, task backlog, and average wait and service times and informs the
34 PoolPolicy object bound to instances of this manager of interesting transitions. It is then up the PoolPolicy object to decide if the thread pool
35 size needs to be adjusted and call this object addThread and removeThread methods to make changes.
36
37 This design allows different policy implementations to used this code to handle basic worker thread management and worker task execution and focus on
38 policy issues. The simplest policy, StaticPolicy, does nothing other than create a fixed number of threads. */
39
40class ThreadManager {
41
42 public:
43
44 ThreadManager(size_t highWatermark=4, size_t lowWatermark=2);
45
46 virtual ~ThreadManager() = 0;
47
48 virtual const PoolPolicy* poolPolicy() const = 0;
49
50 virtual void poolPolicy(const PoolPolicy* value) = 0;
51
52 virtual const ThreadFactory* threadFactory() const = 0;
53
54 virtual void threadFactory(const ThreadFactory* value) = 0;
55
56 virtual size_t highWatermark() const = 0;
57
58 virtual void highWatermark(size_t value) = 0;
59
60 virtual size_t lowWatermark() const = 0;
61
62 virtual void lowWatermark(size_t value) = 0;
63
64 virtual void addThread(size_t value=1) = 0;
65
66 virtual void removeThread(size_t value=1) = 0;
67
68 /** Gets the current number of idle worker threads */
69
70 virtual size_t idleWorkerCount() const = 0;
71
72 /** Gets the current number of total worker threads */
73
74 virtual size_t workerCount() const = 0;
75
76 /** Gets the current number of pending tasks */
77
78 virtual size_t pendingTaskCount() const = 0;
79
80 /** Gets the current number of pending and executing tasks */
81
82 virtual size_t totalTaskCount() const = 0;
83
84 /** Adds a task to be execued at some time in the future by a worker thread. */
85
86 virtual void add(Runnable* value) = 0;
87
88 /** Removes a pending task */
89
90 virtual void remove(Runnable* task) = 0;
91
92 private:
93
94 size_t _hiwat;
95
96 size_t _lowat;
97
98 size_t _idleCount;
99
100 const PoolPolicy* _poolPolicy;;
101
102 const ThreadFactory* _threadFactory;;
103
104 class Task;
105
106 friend class Task;
107
108 std::queue<Task*> _tasks;
109
110 Monitor _monitor;
111
112 class Worker;
113
114 friend class Worker;
115
116 std::set<Thread*> _workers;
117
118};
119
120}}} // facebook::thrift::concurrency
121
122#endif // !defined(_concurrency_ThreadManager_h_)