blob: a90c5d2815fffeec53cbfa30dc2ac6849eff09fb [file] [log] [blame]
Mark Sleef5f2be42006-09-05 21:05:31 +00001#ifndef _THRIFT_CONCURRENCY_THREADMANAGER_H_
2#define _THRIFT_CONCURRENCY_THREADMANAGER_H_ 1
Marc Slemko66949872006-07-15 01:52:39 +00003
Marc Slemko6f038a72006-08-03 18:58:09 +00004#include <boost/shared_ptr.hpp>
Marc Slemko0e53ccd2006-07-17 23:51:05 +00005#include <sys/types.h>
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 Slemko6f038a72006-08-03 18:58:09 +000010using namespace boost;
11
Mark Sleef5f2be42006-09-05 21:05:31 +000012/**
13 * Thread Pool Manager and related classes
14 *
15 * @author marc
16 * @version $Id:$
17 */
Marc Slemko66949872006-07-15 01:52:39 +000018class ThreadManager;
19
Mark Sleef5f2be42006-09-05 21:05:31 +000020/**
21 * ThreadManager class
22 *
23 * This class manages a pool of threads. It uses a ThreadFactory to create
24 * threads. It never actually creates or destroys worker threads, rather
25 * It maintains statistics on number of idle threads, number of active threads,
26 * task backlog, and average wait and service times and informs the PoolPolicy
27 * object bound to instances of this manager of interesting transitions. It is
28 * then up the PoolPolicy object to decide if the thread pool size needs to be
29 * adjusted and call this object addWorker and removeWorker methods to make
30 * changes.
31 *
32 * This design allows different policy implementations to used this code to
33 * handle basic worker thread management and worker task execution and focus on
34 * policy issues. The simplest policy, StaticPolicy, does nothing other than
35 * create a fixed number of threads.
36 */
Marc Slemko66949872006-07-15 01:52:39 +000037class ThreadManager {
38
39 public:
Marc Slemkod466b212006-07-20 00:04:18 +000040 ThreadManager() {}
Marc Slemko66949872006-07-15 01:52:39 +000041
Marc Slemkod466b212006-07-20 00:04:18 +000042 virtual ~ThreadManager() {}
Marc Slemko66949872006-07-15 01:52:39 +000043
Mark Sleef5f2be42006-09-05 21:05:31 +000044 /**
45 * Starts the thread manager. Verifies all attributes have been properly
46 * initialized, then allocates necessary resources to begin operation
47 */
Marc Slemkofe5ba12e2006-07-20 21:16:27 +000048 virtual void start() = 0;
49
Mark Sleef5f2be42006-09-05 21:05:31 +000050 /**
51 * Stops the thread manager. Aborts all remaining unprocessed task, shuts
52 * down all created worker threads, and realeases all allocated resources.
53 * This method blocks for all worker threads to complete, thus it can
54 * potentially block forever if a worker thread is running a task that
55 * won't terminate.
56 */
Marc Slemkofe5ba12e2006-07-20 21:16:27 +000057 virtual void stop() = 0;
58
59 enum STATE {
60 UNINITIALIZED,
61 STARTING,
62 STARTED,
63 STOPPING,
64 STOPPED
65 };
66
67 virtual const STATE state() const = 0;
68
Marc Slemko6f038a72006-08-03 18:58:09 +000069 virtual shared_ptr<ThreadFactory> threadFactory() const = 0;
Marc Slemko66949872006-07-15 01:52:39 +000070
Marc Slemko6f038a72006-08-03 18:58:09 +000071 virtual void threadFactory(shared_ptr<ThreadFactory> value) = 0;
Marc Slemko66949872006-07-15 01:52:39 +000072
Marc Slemkod466b212006-07-20 00:04:18 +000073 virtual void addWorker(size_t value=1) = 0;
Marc Slemko66949872006-07-15 01:52:39 +000074
Marc Slemkod466b212006-07-20 00:04:18 +000075 virtual void removeWorker(size_t value=1) = 0;
Marc Slemko66949872006-07-15 01:52:39 +000076
Mark Sleef5f2be42006-09-05 21:05:31 +000077 /**
78 * Gets the current number of idle worker threads
79 */
Marc Slemko66949872006-07-15 01:52:39 +000080 virtual size_t idleWorkerCount() const = 0;
81
Mark Sleef5f2be42006-09-05 21:05:31 +000082 /**
83 * Gets the current number of total worker threads
84 */
Marc Slemko66949872006-07-15 01:52:39 +000085 virtual size_t workerCount() const = 0;
86
Mark Sleef5f2be42006-09-05 21:05:31 +000087 /**
88 * Gets the current number of pending tasks
89 */
Marc Slemko66949872006-07-15 01:52:39 +000090 virtual size_t pendingTaskCount() const = 0;
91
Mark Sleef5f2be42006-09-05 21:05:31 +000092 /**
93 * Gets the current number of pending and executing tasks
94 */
Marc Slemko66949872006-07-15 01:52:39 +000095 virtual size_t totalTaskCount() const = 0;
96
Mark Sleef5f2be42006-09-05 21:05:31 +000097 /**
98 * Adds a task to be execued at some time in the future by a worker thread.
99 *
100 * @param value The task to run
101 */
Marc Slemko6f038a72006-08-03 18:58:09 +0000102 virtual void add(shared_ptr<Runnable>value) = 0;
Marc Slemko66949872006-07-15 01:52:39 +0000103
Mark Sleef5f2be42006-09-05 21:05:31 +0000104 /**
105 * Removes a pending task
106 */
Marc Slemko6f038a72006-08-03 18:58:09 +0000107 virtual void remove(shared_ptr<Runnable> task) = 0;
Marc Slemko66949872006-07-15 01:52:39 +0000108
Marc Slemko6f038a72006-08-03 18:58:09 +0000109 static shared_ptr<ThreadManager> newThreadManager();
Marc Slemkod466b212006-07-20 00:04:18 +0000110
Mark Sleef5f2be42006-09-05 21:05:31 +0000111 /**
112 * Creates a simple thread manager the uses count number of worker threads
113 */
Marc Slemko6f038a72006-08-03 18:58:09 +0000114 static shared_ptr<ThreadManager> newSimpleThreadManager(size_t count=4);
Marc Slemko66949872006-07-15 01:52:39 +0000115
116 class Task;
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000117
Marc Slemko66949872006-07-15 01:52:39 +0000118 class Worker;
119
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000120 class Impl;
Marc Slemko66949872006-07-15 01:52:39 +0000121};
122
123}}} // facebook::thrift::concurrency
124
Mark Sleef5f2be42006-09-05 21:05:31 +0000125#endif // #ifndef _THRIFT_CONCURRENCY_THREADMANAGER_H_