blob: f0c745fa650b9a385386e9ce7239f2f495ecdd94 [file] [log] [blame]
Mark Slee9f0c6512007-02-28 23:58:26 +00001// Copyright (c) 2006- Facebook
2// Distributed under the Thrift Software License
3//
4// See accompanying file LICENSE or visit the Thrift site at:
5// http://developers.facebook.com/thrift/
6
Mark Sleef5f2be42006-09-05 21:05:31 +00007#ifndef _THRIFT_CONCURRENCY_THREADMANAGER_H_
8#define _THRIFT_CONCURRENCY_THREADMANAGER_H_ 1
Marc Slemko66949872006-07-15 01:52:39 +00009
Marc Slemko6f038a72006-08-03 18:58:09 +000010#include <boost/shared_ptr.hpp>
Marc Slemko0e53ccd2006-07-17 23:51:05 +000011#include <sys/types.h>
Marc Slemko66949872006-07-15 01:52:39 +000012#include "Thread.h"
13
Marc Slemko66949872006-07-15 01:52:39 +000014namespace facebook { namespace thrift { namespace concurrency {
15
Mark Sleef5f2be42006-09-05 21:05:31 +000016/**
17 * Thread Pool Manager and related classes
18 *
19 * @author marc
20 * @version $Id:$
21 */
Marc Slemko66949872006-07-15 01:52:39 +000022class ThreadManager;
23
Mark Sleef5f2be42006-09-05 21:05:31 +000024/**
25 * ThreadManager class
26 *
27 * This class manages a pool of threads. It uses a ThreadFactory to create
28 * threads. It never actually creates or destroys worker threads, rather
29 * It maintains statistics on number of idle threads, number of active threads,
30 * task backlog, and average wait and service times and informs the PoolPolicy
31 * object bound to instances of this manager of interesting transitions. It is
32 * then up the PoolPolicy object to decide if the thread pool size needs to be
33 * adjusted and call this object addWorker and removeWorker methods to make
34 * changes.
35 *
36 * This design allows different policy implementations to used this code to
37 * handle basic worker thread management and worker task execution and focus on
38 * policy issues. The simplest policy, StaticPolicy, does nothing other than
39 * create a fixed number of threads.
40 */
Marc Slemko66949872006-07-15 01:52:39 +000041class ThreadManager {
42
Mark Slee8cbda852007-02-01 23:05:38 +000043 protected:
Marc Slemkod466b212006-07-20 00:04:18 +000044 ThreadManager() {}
Marc Slemko66949872006-07-15 01:52:39 +000045
Mark Slee8cbda852007-02-01 23:05:38 +000046 public:
Marc Slemkod466b212006-07-20 00:04:18 +000047 virtual ~ThreadManager() {}
Marc Slemko66949872006-07-15 01:52:39 +000048
Mark Sleef5f2be42006-09-05 21:05:31 +000049 /**
50 * Starts the thread manager. Verifies all attributes have been properly
51 * initialized, then allocates necessary resources to begin operation
52 */
Marc Slemkofe5ba12e2006-07-20 21:16:27 +000053 virtual void start() = 0;
54
Mark Sleef5f2be42006-09-05 21:05:31 +000055 /**
56 * Stops the thread manager. Aborts all remaining unprocessed task, shuts
57 * down all created worker threads, and realeases all allocated resources.
58 * This method blocks for all worker threads to complete, thus it can
59 * potentially block forever if a worker thread is running a task that
60 * won't terminate.
61 */
Marc Slemkofe5ba12e2006-07-20 21:16:27 +000062 virtual void stop() = 0;
63
Mark Slee7c10eaf2007-03-01 02:45:10 +000064 /**
65 * Joins the thread manager. This is the same as stop, except that it will
66 * block until all the workers have finished their work. At that point
67 * the ThreadManager will transition into the STOPPED state.
68 */
Mark Slee6e3f6372007-03-01 22:05:46 +000069 virtual void join() = 0;
70
Marc Slemkofe5ba12e2006-07-20 21:16:27 +000071 enum STATE {
72 UNINITIALIZED,
73 STARTING,
74 STARTED,
Mark Slee7c10eaf2007-03-01 02:45:10 +000075 JOINING,
Marc Slemkofe5ba12e2006-07-20 21:16:27 +000076 STOPPING,
77 STOPPED
78 };
79
80 virtual const STATE state() const = 0;
81
Mark Slee5ea15f92007-03-05 22:55:59 +000082 virtual boost::shared_ptr<ThreadFactory> threadFactory() const = 0;
Marc Slemko66949872006-07-15 01:52:39 +000083
Mark Slee5ea15f92007-03-05 22:55:59 +000084 virtual void threadFactory(boost::shared_ptr<ThreadFactory> value) = 0;
Marc Slemko66949872006-07-15 01:52:39 +000085
Marc Slemkod466b212006-07-20 00:04:18 +000086 virtual void addWorker(size_t value=1) = 0;
Marc Slemko66949872006-07-15 01:52:39 +000087
Marc Slemkod466b212006-07-20 00:04:18 +000088 virtual void removeWorker(size_t value=1) = 0;
Marc Slemko66949872006-07-15 01:52:39 +000089
Mark Sleef5f2be42006-09-05 21:05:31 +000090 /**
91 * Gets the current number of idle worker threads
92 */
Marc Slemko66949872006-07-15 01:52:39 +000093 virtual size_t idleWorkerCount() const = 0;
94
Mark Sleef5f2be42006-09-05 21:05:31 +000095 /**
96 * Gets the current number of total worker threads
97 */
Marc Slemko66949872006-07-15 01:52:39 +000098 virtual size_t workerCount() const = 0;
99
Mark Sleef5f2be42006-09-05 21:05:31 +0000100 /**
101 * Gets the current number of pending tasks
102 */
Marc Slemko66949872006-07-15 01:52:39 +0000103 virtual size_t pendingTaskCount() const = 0;
104
Mark Sleef5f2be42006-09-05 21:05:31 +0000105 /**
106 * Gets the current number of pending and executing tasks
107 */
Marc Slemko66949872006-07-15 01:52:39 +0000108 virtual size_t totalTaskCount() const = 0;
109
Mark Sleef5f2be42006-09-05 21:05:31 +0000110 /**
111 * Adds a task to be execued at some time in the future by a worker thread.
112 *
113 * @param value The task to run
114 */
Mark Slee5ea15f92007-03-05 22:55:59 +0000115 virtual void add(boost::shared_ptr<Runnable>value) = 0;
Marc Slemko66949872006-07-15 01:52:39 +0000116
Mark Sleef5f2be42006-09-05 21:05:31 +0000117 /**
118 * Removes a pending task
119 */
Mark Slee5ea15f92007-03-05 22:55:59 +0000120 virtual void remove(boost::shared_ptr<Runnable> task) = 0;
Marc Slemko66949872006-07-15 01:52:39 +0000121
Mark Slee5ea15f92007-03-05 22:55:59 +0000122 static boost::shared_ptr<ThreadManager> newThreadManager();
Marc Slemkod466b212006-07-20 00:04:18 +0000123
Mark Sleef5f2be42006-09-05 21:05:31 +0000124 /**
125 * Creates a simple thread manager the uses count number of worker threads
126 */
Mark Slee5ea15f92007-03-05 22:55:59 +0000127 static boost::shared_ptr<ThreadManager> newSimpleThreadManager(size_t count=4);
Marc Slemko66949872006-07-15 01:52:39 +0000128
129 class Task;
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000130
Marc Slemko66949872006-07-15 01:52:39 +0000131 class Worker;
132
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000133 class Impl;
Marc Slemko66949872006-07-15 01:52:39 +0000134};
135
136}}} // facebook::thrift::concurrency
137
Mark Sleef5f2be42006-09-05 21:05:31 +0000138#endif // #ifndef _THRIFT_CONCURRENCY_THREADMANAGER_H_