blob: 0383a09220d43d7223971a0daadac074aac98c96 [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
Mark Slee8cbda852007-02-01 23:05:38 +000039 protected:
Marc Slemkod466b212006-07-20 00:04:18 +000040 ThreadManager() {}
Marc Slemko66949872006-07-15 01:52:39 +000041
Mark Slee8cbda852007-02-01 23:05:38 +000042 public:
Marc Slemkod466b212006-07-20 00:04:18 +000043 virtual ~ThreadManager() {}
Marc Slemko66949872006-07-15 01:52:39 +000044
Mark Sleef5f2be42006-09-05 21:05:31 +000045 /**
46 * Starts the thread manager. Verifies all attributes have been properly
47 * initialized, then allocates necessary resources to begin operation
48 */
Marc Slemkofe5ba12e2006-07-20 21:16:27 +000049 virtual void start() = 0;
50
Mark Sleef5f2be42006-09-05 21:05:31 +000051 /**
52 * Stops the thread manager. Aborts all remaining unprocessed task, shuts
53 * down all created worker threads, and realeases all allocated resources.
54 * This method blocks for all worker threads to complete, thus it can
55 * potentially block forever if a worker thread is running a task that
56 * won't terminate.
57 */
Marc Slemkofe5ba12e2006-07-20 21:16:27 +000058 virtual void stop() = 0;
59
60 enum STATE {
61 UNINITIALIZED,
62 STARTING,
63 STARTED,
64 STOPPING,
65 STOPPED
66 };
67
68 virtual const STATE state() const = 0;
69
Marc Slemko6f038a72006-08-03 18:58:09 +000070 virtual shared_ptr<ThreadFactory> threadFactory() const = 0;
Marc Slemko66949872006-07-15 01:52:39 +000071
Marc Slemko6f038a72006-08-03 18:58:09 +000072 virtual void threadFactory(shared_ptr<ThreadFactory> value) = 0;
Marc Slemko66949872006-07-15 01:52:39 +000073
Marc Slemkod466b212006-07-20 00:04:18 +000074 virtual void addWorker(size_t value=1) = 0;
Marc Slemko66949872006-07-15 01:52:39 +000075
Marc Slemkod466b212006-07-20 00:04:18 +000076 virtual void removeWorker(size_t value=1) = 0;
Marc Slemko66949872006-07-15 01:52:39 +000077
Mark Sleef5f2be42006-09-05 21:05:31 +000078 /**
79 * Gets the current number of idle worker threads
80 */
Marc Slemko66949872006-07-15 01:52:39 +000081 virtual size_t idleWorkerCount() const = 0;
82
Mark Sleef5f2be42006-09-05 21:05:31 +000083 /**
84 * Gets the current number of total worker threads
85 */
Marc Slemko66949872006-07-15 01:52:39 +000086 virtual size_t workerCount() const = 0;
87
Mark Sleef5f2be42006-09-05 21:05:31 +000088 /**
89 * Gets the current number of pending tasks
90 */
Marc Slemko66949872006-07-15 01:52:39 +000091 virtual size_t pendingTaskCount() const = 0;
92
Mark Sleef5f2be42006-09-05 21:05:31 +000093 /**
94 * Gets the current number of pending and executing tasks
95 */
Marc Slemko66949872006-07-15 01:52:39 +000096 virtual size_t totalTaskCount() const = 0;
97
Mark Sleef5f2be42006-09-05 21:05:31 +000098 /**
99 * Adds a task to be execued at some time in the future by a worker thread.
100 *
101 * @param value The task to run
102 */
Marc Slemko6f038a72006-08-03 18:58:09 +0000103 virtual void add(shared_ptr<Runnable>value) = 0;
Marc Slemko66949872006-07-15 01:52:39 +0000104
Mark Sleef5f2be42006-09-05 21:05:31 +0000105 /**
106 * Removes a pending task
107 */
Marc Slemko6f038a72006-08-03 18:58:09 +0000108 virtual void remove(shared_ptr<Runnable> task) = 0;
Marc Slemko66949872006-07-15 01:52:39 +0000109
Marc Slemko6f038a72006-08-03 18:58:09 +0000110 static shared_ptr<ThreadManager> newThreadManager();
Marc Slemkod466b212006-07-20 00:04:18 +0000111
Mark Sleef5f2be42006-09-05 21:05:31 +0000112 /**
113 * Creates a simple thread manager the uses count number of worker threads
114 */
Marc Slemko6f038a72006-08-03 18:58:09 +0000115 static shared_ptr<ThreadManager> newSimpleThreadManager(size_t count=4);
Marc Slemko66949872006-07-15 01:52:39 +0000116
117 class Task;
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000118
Marc Slemko66949872006-07-15 01:52:39 +0000119 class Worker;
120
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000121 class Impl;
Marc Slemko66949872006-07-15 01:52:39 +0000122};
123
124}}} // facebook::thrift::concurrency
125
Mark Sleef5f2be42006-09-05 21:05:31 +0000126#endif // #ifndef _THRIFT_CONCURRENCY_THREADMANAGER_H_