blob: 3b87c214ec799fcbc2434c34ed422dea424d4cd3 [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
Marc Slemko66949872006-07-15 01:52:39 +000017/** 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 Slemkod466b212006-07-20 00:04:18 +000022 size needs to be adjusted and call this object addWorker and removeWorker methods to make changes.
Marc Slemko66949872006-07-15 01:52:39 +000023
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
27class ThreadManager {
28
29 public:
30
Marc Slemkod466b212006-07-20 00:04:18 +000031 ThreadManager() {}
Marc Slemko66949872006-07-15 01:52:39 +000032
Marc Slemkod466b212006-07-20 00:04:18 +000033 virtual ~ThreadManager() {}
Marc Slemko66949872006-07-15 01:52:39 +000034
35 virtual const ThreadFactory* threadFactory() const = 0;
36
37 virtual void threadFactory(const ThreadFactory* value) = 0;
38
Marc Slemkod466b212006-07-20 00:04:18 +000039 virtual void addWorker(size_t value=1) = 0;
Marc Slemko66949872006-07-15 01:52:39 +000040
Marc Slemkod466b212006-07-20 00:04:18 +000041 virtual void removeWorker(size_t value=1) = 0;
Marc Slemko66949872006-07-15 01:52:39 +000042
43 /** Gets the current number of idle worker threads */
44
45 virtual size_t idleWorkerCount() const = 0;
46
47 /** Gets the current number of total worker threads */
48
49 virtual size_t workerCount() const = 0;
50
51 /** Gets the current number of pending tasks */
52
53 virtual size_t pendingTaskCount() const = 0;
54
55 /** Gets the current number of pending and executing tasks */
56
57 virtual size_t totalTaskCount() const = 0;
58
59 /** Adds a task to be execued at some time in the future by a worker thread. */
60
61 virtual void add(Runnable* value) = 0;
62
63 /** Removes a pending task */
64
65 virtual void remove(Runnable* task) = 0;
66
Marc Slemkod466b212006-07-20 00:04:18 +000067 static ThreadManager* newThreadManager();
68
69 static ThreadManager* newSimpleThreadManager();
Marc Slemko66949872006-07-15 01:52:39 +000070
71 class Task;
Marc Slemko0e53ccd2006-07-17 23:51:05 +000072
Marc Slemko66949872006-07-15 01:52:39 +000073 class Worker;
74
Marc Slemko0e53ccd2006-07-17 23:51:05 +000075 class Impl;
Marc Slemko66949872006-07-15 01:52:39 +000076};
77
78}}} // facebook::thrift::concurrency
79
80#endif // !defined(_concurrency_ThreadManager_h_)