blob: 352afb9bb9eeb4b675a4f05afc4a7163317dc150 [file] [log] [blame]
Marc Slemko66949872006-07-15 01:52:39 +00001#if !defined(_concurrency_ThreadManager_h_)
2#define _concurrency_ThreadManager_h_ 1
3
Marc Slemko0e53ccd2006-07-17 23:51:05 +00004#include <sys/types.h>
5
Marc Slemko66949872006-07-15 01:52:39 +00006#include "Thread.h"
7
Marc Slemko66949872006-07-15 01:52:39 +00008namespace facebook { namespace thrift { namespace concurrency {
9
Marc Slemko0e53ccd2006-07-17 23:51:05 +000010/** Thread Pool Manager and related classes
11
12 @author marc
13 @version $Id:$ */
14
Marc Slemko66949872006-07-15 01:52:39 +000015class ThreadManager;
16
17/** PoolPolicy class
18
19 Tracks performance of ThreadManager object and makes desired changes in thread pool count if any. */
20
21class PoolPolicy {
22
23 public:
24
Marc Slemko0e53ccd2006-07-17 23:51:05 +000025 PoolPolicy() {}
Marc Slemko66949872006-07-15 01:52:39 +000026
Marc Slemko0e53ccd2006-07-17 23:51:05 +000027 virtual ~PoolPolicy() {}
Marc Slemko66949872006-07-15 01:52:39 +000028
Marc Slemko0e53ccd2006-07-17 23:51:05 +000029 virtual void onEmpty(ThreadManager* source) const = 0;
Marc Slemko66949872006-07-15 01:52:39 +000030
Marc Slemko0e53ccd2006-07-17 23:51:05 +000031 virtual void onLowWatermark(ThreadManager* source) const = 0;
32
33 virtual void onHighWatermark(ThreadManager* source) const = 0;
34
35};
36
37class BasicPoolPolicy : public PoolPolicy {
38
39 public:
40
41 BasicPoolPolicy();
42
43 virtual ~BasicPoolPolicy();
44
45 virtual void onEmpty(ThreadManager* source) const;
46
47 virtual void onLowWatermark(ThreadManager* source) const;
48
49 virtual void onHighWatermark(ThreadManager* source) const;
50
51 private:
52
53 class Impl;
54
55 Impl* _impl;
Marc Slemko66949872006-07-15 01:52:39 +000056};
57
58/** ThreadManager class
59
60 This class manages a pool of threads. It uses a ThreadFactory to create threads. It never actually creates or destroys worker threads, rather
61 it maintains statistics on number of idle threads, number of active threads, task backlog, and average wait and service times and informs the
62 PoolPolicy object bound to instances of this manager of interesting transitions. It is then up the PoolPolicy object to decide if the thread pool
63 size needs to be adjusted and call this object addThread and removeThread methods to make changes.
64
65 This design allows different policy implementations to used this code to handle basic worker thread management and worker task execution and focus on
66 policy issues. The simplest policy, StaticPolicy, does nothing other than create a fixed number of threads. */
67
68class ThreadManager {
69
70 public:
71
Marc Slemko0e53ccd2006-07-17 23:51:05 +000072 ThreadManager(size_t highWatermark=4, size_t lowWatermark=2) {};
Marc Slemko66949872006-07-15 01:52:39 +000073
Marc Slemko0e53ccd2006-07-17 23:51:05 +000074 virtual ~ThreadManager() {};
Marc Slemko66949872006-07-15 01:52:39 +000075
76 virtual const PoolPolicy* poolPolicy() const = 0;
77
78 virtual void poolPolicy(const PoolPolicy* value) = 0;
79
80 virtual const ThreadFactory* threadFactory() const = 0;
81
82 virtual void threadFactory(const ThreadFactory* value) = 0;
83
84 virtual size_t highWatermark() const = 0;
85
86 virtual void highWatermark(size_t value) = 0;
87
88 virtual size_t lowWatermark() const = 0;
89
90 virtual void lowWatermark(size_t value) = 0;
91
92 virtual void addThread(size_t value=1) = 0;
93
94 virtual void removeThread(size_t value=1) = 0;
95
96 /** Gets the current number of idle worker threads */
97
98 virtual size_t idleWorkerCount() const = 0;
99
100 /** Gets the current number of total worker threads */
101
102 virtual size_t workerCount() const = 0;
103
104 /** Gets the current number of pending tasks */
105
106 virtual size_t pendingTaskCount() const = 0;
107
108 /** Gets the current number of pending and executing tasks */
109
110 virtual size_t totalTaskCount() const = 0;
111
112 /** Adds a task to be execued at some time in the future by a worker thread. */
113
114 virtual void add(Runnable* value) = 0;
115
116 /** Removes a pending task */
117
118 virtual void remove(Runnable* task) = 0;
119
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000120 static ThreadManager* newThreadManager(size_t lowWatermark=2, size_t highWatermark=4);
Marc Slemko66949872006-07-15 01:52:39 +0000121
122 class Task;
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000123
Marc Slemko66949872006-07-15 01:52:39 +0000124 class Worker;
125
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000126 class Impl;
Marc Slemko66949872006-07-15 01:52:39 +0000127};
128
129}}} // facebook::thrift::concurrency
130
131#endif // !defined(_concurrency_ThreadManager_h_)