blob: aa5a98a94e1ec2dd46ce477977df0c9bf908d3b2 [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
Marc Slemkofe5ba12e2006-07-20 21:16:27 +000035 /** Starts the thread manager. Verifies all attributes have been properly initialized, then allocates necessary resources to begin operation */
36
37 virtual void start() = 0;
38
39 /** Stops the thread manager. Aborts all remaining unprocessed task, shuts down all created worker threads, and realeases all allocated resources.
40 This method blocks for all worker threads to complete, thus it can potentially block forever if a worker thread is running a task that
41 won't terminate. */
42
43 virtual void stop() = 0;
44
45 enum STATE {
46 UNINITIALIZED,
47 STARTING,
48 STARTED,
49 STOPPING,
50 STOPPED
51 };
52
53 virtual const STATE state() const = 0;
54
Marc Slemko66949872006-07-15 01:52:39 +000055 virtual const ThreadFactory* threadFactory() const = 0;
56
57 virtual void threadFactory(const ThreadFactory* value) = 0;
58
Marc Slemkod466b212006-07-20 00:04:18 +000059 virtual void addWorker(size_t value=1) = 0;
Marc Slemko66949872006-07-15 01:52:39 +000060
Marc Slemkod466b212006-07-20 00:04:18 +000061 virtual void removeWorker(size_t value=1) = 0;
Marc Slemko66949872006-07-15 01:52:39 +000062
63 /** Gets the current number of idle worker threads */
64
65 virtual size_t idleWorkerCount() const = 0;
66
67 /** Gets the current number of total worker threads */
68
69 virtual size_t workerCount() const = 0;
70
71 /** Gets the current number of pending tasks */
72
73 virtual size_t pendingTaskCount() const = 0;
74
75 /** Gets the current number of pending and executing tasks */
76
77 virtual size_t totalTaskCount() const = 0;
78
79 /** Adds a task to be execued at some time in the future by a worker thread. */
80
81 virtual void add(Runnable* value) = 0;
82
83 /** Removes a pending task */
84
85 virtual void remove(Runnable* task) = 0;
86
Marc Slemkod466b212006-07-20 00:04:18 +000087 static ThreadManager* newThreadManager();
88
Marc Slemko525c2022006-07-20 00:29:35 +000089 /** Creates a simple thread manager the uses count number of worker threads */
90
91 static ThreadManager* newSimpleThreadManager(size_t count=4);
Marc Slemko66949872006-07-15 01:52:39 +000092
93 class Task;
Marc Slemko0e53ccd2006-07-17 23:51:05 +000094
Marc Slemko66949872006-07-15 01:52:39 +000095 class Worker;
96
Marc Slemko0e53ccd2006-07-17 23:51:05 +000097 class Impl;
Marc Slemko66949872006-07-15 01:52:39 +000098};
99
100}}} // facebook::thrift::concurrency
101
102#endif // !defined(_concurrency_ThreadManager_h_)