blob: 0fedc88ebeaf27a0f1da9c1ae9e2eb9d2ef49eb9 [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
Mark Slee9f0c6512007-02-28 23:58:26 +000019
Mark Sleef5f2be42006-09-05 21:05:31 +000020#ifndef _THRIFT_CONCURRENCY_THREADMANAGER_H_
21#define _THRIFT_CONCURRENCY_THREADMANAGER_H_ 1
Marc Slemko66949872006-07-15 01:52:39 +000022
Marc Slemko6f038a72006-08-03 18:58:09 +000023#include <boost/shared_ptr.hpp>
Roger Meier82525772012-11-16 00:38:27 +000024#include <thrift/cxxfunctional.h>
Marc Slemko0e53ccd2006-07-17 23:51:05 +000025#include <sys/types.h>
Roger Meier4285ba22013-06-10 21:17:23 +020026#include <thrift/concurrency/Thread.h>
Marc Slemko66949872006-07-15 01:52:39 +000027
T Jake Lucianib5e62212009-01-31 22:36:20 +000028namespace apache { namespace thrift { namespace concurrency {
Marc Slemko66949872006-07-15 01:52:39 +000029
Mark Sleef5f2be42006-09-05 21:05:31 +000030/**
31 * Thread Pool Manager and related classes
32 *
Mark Sleef5f2be42006-09-05 21:05:31 +000033 * @version $Id:$
34 */
Marc Slemko66949872006-07-15 01:52:39 +000035class ThreadManager;
36
Mark Sleef5f2be42006-09-05 21:05:31 +000037/**
38 * ThreadManager class
39 *
40 * This class manages a pool of threads. It uses a ThreadFactory to create
41 * threads. It never actually creates or destroys worker threads, rather
42 * It maintains statistics on number of idle threads, number of active threads,
43 * task backlog, and average wait and service times and informs the PoolPolicy
44 * object bound to instances of this manager of interesting transitions. It is
45 * then up the PoolPolicy object to decide if the thread pool size needs to be
46 * adjusted and call this object addWorker and removeWorker methods to make
47 * changes.
48 *
49 * This design allows different policy implementations to used this code to
50 * handle basic worker thread management and worker task execution and focus on
51 * policy issues. The simplest policy, StaticPolicy, does nothing other than
52 * create a fixed number of threads.
53 */
Marc Slemko66949872006-07-15 01:52:39 +000054class ThreadManager {
55
Mark Slee8cbda852007-02-01 23:05:38 +000056 protected:
Marc Slemkod466b212006-07-20 00:04:18 +000057 ThreadManager() {}
Marc Slemko66949872006-07-15 01:52:39 +000058
Mark Slee8cbda852007-02-01 23:05:38 +000059 public:
Roger Meier82525772012-11-16 00:38:27 +000060 typedef apache::thrift::stdcxx::function<void(boost::shared_ptr<Runnable>)> ExpireCallback;
David Reiss068f4162010-03-09 05:19:45 +000061
Marc Slemkod466b212006-07-20 00:04:18 +000062 virtual ~ThreadManager() {}
Marc Slemko66949872006-07-15 01:52:39 +000063
Mark Sleef5f2be42006-09-05 21:05:31 +000064 /**
65 * Starts the thread manager. Verifies all attributes have been properly
66 * initialized, then allocates necessary resources to begin operation
67 */
Marc Slemkofe5ba12e2006-07-20 21:16:27 +000068 virtual void start() = 0;
69
Mark Sleef5f2be42006-09-05 21:05:31 +000070 /**
71 * Stops the thread manager. Aborts all remaining unprocessed task, shuts
72 * down all created worker threads, and realeases all allocated resources.
73 * This method blocks for all worker threads to complete, thus it can
Marc Slemko3a3b53b2007-05-22 23:59:54 +000074 * potentially block forever if a worker thread is running a task that
Mark Sleef5f2be42006-09-05 21:05:31 +000075 * won't terminate.
76 */
Marc Slemkofe5ba12e2006-07-20 21:16:27 +000077 virtual void stop() = 0;
78
Mark Slee7c10eaf2007-03-01 02:45:10 +000079 /**
80 * Joins the thread manager. This is the same as stop, except that it will
81 * block until all the workers have finished their work. At that point
82 * the ThreadManager will transition into the STOPPED state.
83 */
Mark Slee6e3f6372007-03-01 22:05:46 +000084 virtual void join() = 0;
85
Marc Slemkofe5ba12e2006-07-20 21:16:27 +000086 enum STATE {
87 UNINITIALIZED,
88 STARTING,
89 STARTED,
Mark Slee7c10eaf2007-03-01 02:45:10 +000090 JOINING,
Marc Slemkofe5ba12e2006-07-20 21:16:27 +000091 STOPPING,
92 STOPPED
93 };
Marc Slemko3a3b53b2007-05-22 23:59:54 +000094
Roger Meier3b771a12010-11-17 22:11:26 +000095 virtual STATE state() const = 0;
Marc Slemkofe5ba12e2006-07-20 21:16:27 +000096
Mark Slee5ea15f92007-03-05 22:55:59 +000097 virtual boost::shared_ptr<ThreadFactory> threadFactory() const = 0;
Marc Slemko66949872006-07-15 01:52:39 +000098
Mark Slee5ea15f92007-03-05 22:55:59 +000099 virtual void threadFactory(boost::shared_ptr<ThreadFactory> value) = 0;
Marc Slemko66949872006-07-15 01:52:39 +0000100
Marc Slemkod466b212006-07-20 00:04:18 +0000101 virtual void addWorker(size_t value=1) = 0;
Marc Slemko66949872006-07-15 01:52:39 +0000102
Marc Slemkod466b212006-07-20 00:04:18 +0000103 virtual void removeWorker(size_t value=1) = 0;
Marc Slemko66949872006-07-15 01:52:39 +0000104
Mark Sleef5f2be42006-09-05 21:05:31 +0000105 /**
106 * Gets the current number of idle worker threads
107 */
Marc Slemko66949872006-07-15 01:52:39 +0000108 virtual size_t idleWorkerCount() const = 0;
109
Mark Sleef5f2be42006-09-05 21:05:31 +0000110 /**
111 * Gets the current number of total worker threads
112 */
Marc Slemko66949872006-07-15 01:52:39 +0000113 virtual size_t workerCount() const = 0;
114
Mark Sleef5f2be42006-09-05 21:05:31 +0000115 /**
116 * Gets the current number of pending tasks
117 */
Marc Slemko66949872006-07-15 01:52:39 +0000118 virtual size_t pendingTaskCount() const = 0;
119
Mark Sleef5f2be42006-09-05 21:05:31 +0000120 /**
121 * Gets the current number of pending and executing tasks
122 */
Marc Slemko66949872006-07-15 01:52:39 +0000123 virtual size_t totalTaskCount() const = 0;
124
Mark Sleef5f2be42006-09-05 21:05:31 +0000125 /**
Marc Slemko3a3b53b2007-05-22 23:59:54 +0000126 * Gets the maximum pending task count. 0 indicates no maximum
Mark Sleef5f2be42006-09-05 21:05:31 +0000127 */
Marc Slemko3a3b53b2007-05-22 23:59:54 +0000128 virtual size_t pendingTaskCountMax() const = 0;
129
130 /**
David Reiss068f4162010-03-09 05:19:45 +0000131 * Gets the number of tasks which have been expired without being run.
132 */
133 virtual size_t expiredTaskCount() = 0;
134
135 /**
Marc Slemko3a3b53b2007-05-22 23:59:54 +0000136 * Adds a task to be executed at some time in the future by a worker thread.
137 *
138 * This method will block if pendingTaskCountMax() in not zero and pendingTaskCount()
139 * is greater than or equalt to pendingTaskCountMax(). If this method is called in the
140 * context of a ThreadManager worker thread it will throw a
141 * TooManyPendingTasksException
142 *
143 * @param task The task to queue for execution
144 *
145 * @param timeout Time to wait in milliseconds to add a task when a pending-task-count
Aditya Agarwal4b6ff2d2007-12-25 22:58:50 +0000146 * is specified. Specific cases:
147 * timeout = 0 : Wait forever to queue task.
148 * timeout = -1 : Return immediately if pending task count exceeds specified max
David Reiss068f4162010-03-09 05:19:45 +0000149 * @param expiration when nonzero, the number of milliseconds the task is valid
150 * to be run; if exceeded, the task will be dropped off the queue and not run.
Marc Slemko3a3b53b2007-05-22 23:59:54 +0000151 *
152 * @throws TooManyPendingTasksException Pending task count exceeds max pending task count
153 */
David Reiss068f4162010-03-09 05:19:45 +0000154 virtual void add(boost::shared_ptr<Runnable>task,
155 int64_t timeout=0LL,
156 int64_t expiration=0LL) = 0;
Marc Slemko66949872006-07-15 01:52:39 +0000157
Mark Sleef5f2be42006-09-05 21:05:31 +0000158 /**
159 * Removes a pending task
160 */
Mark Slee5ea15f92007-03-05 22:55:59 +0000161 virtual void remove(boost::shared_ptr<Runnable> task) = 0;
Marc Slemko66949872006-07-15 01:52:39 +0000162
David Reiss01fe1532010-03-09 05:19:25 +0000163 /**
164 * Remove the next pending task which would be run.
165 *
166 * @return the task removed.
167 */
168 virtual boost::shared_ptr<Runnable> removeNextPending() = 0;
169
David Reiss068f4162010-03-09 05:19:45 +0000170 /**
171 * Remove tasks from front of task queue that have expired.
172 */
173 virtual void removeExpiredTasks() = 0;
174
175 /**
176 * Set a callback to be called when a task is expired and not run.
177 *
178 * @param expireCallback a function called with the shared_ptr<Runnable> for
179 * the expired task.
180 */
181 virtual void setExpireCallback(ExpireCallback expireCallback) = 0;
182
Mark Slee5ea15f92007-03-05 22:55:59 +0000183 static boost::shared_ptr<ThreadManager> newThreadManager();
Marc Slemkod466b212006-07-20 00:04:18 +0000184
Mark Sleef5f2be42006-09-05 21:05:31 +0000185 /**
Marc Slemko3a3b53b2007-05-22 23:59:54 +0000186 * Creates a simple thread manager the uses count number of worker threads and has
187 * a pendingTaskCountMax maximum pending tasks. The default, 0, specified no limit
188 * on pending tasks
Mark Sleef5f2be42006-09-05 21:05:31 +0000189 */
Marc Slemko3a3b53b2007-05-22 23:59:54 +0000190 static boost::shared_ptr<ThreadManager> newSimpleThreadManager(size_t count=4, size_t pendingTaskCountMax=0);
Marc Slemko66949872006-07-15 01:52:39 +0000191
192 class Task;
Marc Slemko3a3b53b2007-05-22 23:59:54 +0000193
Marc Slemko66949872006-07-15 01:52:39 +0000194 class Worker;
195
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000196 class Impl;
Marc Slemko66949872006-07-15 01:52:39 +0000197};
198
T Jake Lucianib5e62212009-01-31 22:36:20 +0000199}}} // apache::thrift::concurrency
Marc Slemko66949872006-07-15 01:52:39 +0000200
Mark Sleef5f2be42006-09-05 21:05:31 +0000201#endif // #ifndef _THRIFT_CONCURRENCY_THREADMANAGER_H_