blob: 6e5a17817e1af15b77d702e4fdc96f3ad8655f11 [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>
Marc Slemko0e53ccd2006-07-17 23:51:05 +000024#include <sys/types.h>
Marc Slemko66949872006-07-15 01:52:39 +000025#include "Thread.h"
26
T Jake Lucianib5e62212009-01-31 22:36:20 +000027namespace apache { namespace thrift { namespace concurrency {
Marc Slemko66949872006-07-15 01:52:39 +000028
Mark Sleef5f2be42006-09-05 21:05:31 +000029/**
30 * Thread Pool Manager and related classes
31 *
Mark Sleef5f2be42006-09-05 21:05:31 +000032 * @version $Id:$
33 */
Marc Slemko66949872006-07-15 01:52:39 +000034class ThreadManager;
35
Mark Sleef5f2be42006-09-05 21:05:31 +000036/**
37 * ThreadManager class
38 *
39 * This class manages a pool of threads. It uses a ThreadFactory to create
40 * threads. It never actually creates or destroys worker threads, rather
41 * It maintains statistics on number of idle threads, number of active threads,
42 * task backlog, and average wait and service times and informs the PoolPolicy
43 * object bound to instances of this manager of interesting transitions. It is
44 * then up the PoolPolicy object to decide if the thread pool size needs to be
45 * adjusted and call this object addWorker and removeWorker methods to make
46 * changes.
47 *
48 * This design allows different policy implementations to used this code to
49 * handle basic worker thread management and worker task execution and focus on
50 * policy issues. The simplest policy, StaticPolicy, does nothing other than
51 * create a fixed number of threads.
52 */
Marc Slemko66949872006-07-15 01:52:39 +000053class ThreadManager {
54
Mark Slee8cbda852007-02-01 23:05:38 +000055 protected:
Marc Slemkod466b212006-07-20 00:04:18 +000056 ThreadManager() {}
Marc Slemko66949872006-07-15 01:52:39 +000057
Mark Slee8cbda852007-02-01 23:05:38 +000058 public:
Marc Slemkod466b212006-07-20 00:04:18 +000059 virtual ~ThreadManager() {}
Marc Slemko66949872006-07-15 01:52:39 +000060
Mark Sleef5f2be42006-09-05 21:05:31 +000061 /**
62 * Starts the thread manager. Verifies all attributes have been properly
63 * initialized, then allocates necessary resources to begin operation
64 */
Marc Slemkofe5ba12e2006-07-20 21:16:27 +000065 virtual void start() = 0;
66
Mark Sleef5f2be42006-09-05 21:05:31 +000067 /**
68 * Stops the thread manager. Aborts all remaining unprocessed task, shuts
69 * down all created worker threads, and realeases all allocated resources.
70 * This method blocks for all worker threads to complete, thus it can
Marc Slemko3a3b53b2007-05-22 23:59:54 +000071 * potentially block forever if a worker thread is running a task that
Mark Sleef5f2be42006-09-05 21:05:31 +000072 * won't terminate.
73 */
Marc Slemkofe5ba12e2006-07-20 21:16:27 +000074 virtual void stop() = 0;
75
Mark Slee7c10eaf2007-03-01 02:45:10 +000076 /**
77 * Joins the thread manager. This is the same as stop, except that it will
78 * block until all the workers have finished their work. At that point
79 * the ThreadManager will transition into the STOPPED state.
80 */
Mark Slee6e3f6372007-03-01 22:05:46 +000081 virtual void join() = 0;
82
Marc Slemkofe5ba12e2006-07-20 21:16:27 +000083 enum STATE {
84 UNINITIALIZED,
85 STARTING,
86 STARTED,
Mark Slee7c10eaf2007-03-01 02:45:10 +000087 JOINING,
Marc Slemkofe5ba12e2006-07-20 21:16:27 +000088 STOPPING,
89 STOPPED
90 };
Marc Slemko3a3b53b2007-05-22 23:59:54 +000091
Marc Slemkofe5ba12e2006-07-20 21:16:27 +000092 virtual const STATE state() const = 0;
93
Mark Slee5ea15f92007-03-05 22:55:59 +000094 virtual boost::shared_ptr<ThreadFactory> threadFactory() const = 0;
Marc Slemko66949872006-07-15 01:52:39 +000095
Mark Slee5ea15f92007-03-05 22:55:59 +000096 virtual void threadFactory(boost::shared_ptr<ThreadFactory> value) = 0;
Marc Slemko66949872006-07-15 01:52:39 +000097
Marc Slemkod466b212006-07-20 00:04:18 +000098 virtual void addWorker(size_t value=1) = 0;
Marc Slemko66949872006-07-15 01:52:39 +000099
Marc Slemkod466b212006-07-20 00:04:18 +0000100 virtual void removeWorker(size_t value=1) = 0;
Marc Slemko66949872006-07-15 01:52:39 +0000101
Mark Sleef5f2be42006-09-05 21:05:31 +0000102 /**
103 * Gets the current number of idle worker threads
104 */
Marc Slemko66949872006-07-15 01:52:39 +0000105 virtual size_t idleWorkerCount() const = 0;
106
Mark Sleef5f2be42006-09-05 21:05:31 +0000107 /**
108 * Gets the current number of total worker threads
109 */
Marc Slemko66949872006-07-15 01:52:39 +0000110 virtual size_t workerCount() const = 0;
111
Mark Sleef5f2be42006-09-05 21:05:31 +0000112 /**
113 * Gets the current number of pending tasks
114 */
Marc Slemko66949872006-07-15 01:52:39 +0000115 virtual size_t pendingTaskCount() const = 0;
116
Mark Sleef5f2be42006-09-05 21:05:31 +0000117 /**
118 * Gets the current number of pending and executing tasks
119 */
Marc Slemko66949872006-07-15 01:52:39 +0000120 virtual size_t totalTaskCount() const = 0;
121
Mark Sleef5f2be42006-09-05 21:05:31 +0000122 /**
Marc Slemko3a3b53b2007-05-22 23:59:54 +0000123 * Gets the maximum pending task count. 0 indicates no maximum
Mark Sleef5f2be42006-09-05 21:05:31 +0000124 */
Marc Slemko3a3b53b2007-05-22 23:59:54 +0000125 virtual size_t pendingTaskCountMax() const = 0;
126
127 /**
128 * Adds a task to be executed at some time in the future by a worker thread.
129 *
130 * This method will block if pendingTaskCountMax() in not zero and pendingTaskCount()
131 * is greater than or equalt to pendingTaskCountMax(). If this method is called in the
132 * context of a ThreadManager worker thread it will throw a
133 * TooManyPendingTasksException
134 *
135 * @param task The task to queue for execution
136 *
137 * @param timeout Time to wait in milliseconds to add a task when a pending-task-count
Aditya Agarwal4b6ff2d2007-12-25 22:58:50 +0000138 * is specified. Specific cases:
139 * timeout = 0 : Wait forever to queue task.
140 * timeout = -1 : Return immediately if pending task count exceeds specified max
Marc Slemko3a3b53b2007-05-22 23:59:54 +0000141 *
142 * @throws TooManyPendingTasksException Pending task count exceeds max pending task count
143 */
Mark Slee9b82d272007-05-23 05:16:07 +0000144 virtual void add(boost::shared_ptr<Runnable>task, int64_t timeout=0LL) = 0;
Marc Slemko66949872006-07-15 01:52:39 +0000145
Mark Sleef5f2be42006-09-05 21:05:31 +0000146 /**
147 * Removes a pending task
148 */
Mark Slee5ea15f92007-03-05 22:55:59 +0000149 virtual void remove(boost::shared_ptr<Runnable> task) = 0;
Marc Slemko66949872006-07-15 01:52:39 +0000150
Mark Slee5ea15f92007-03-05 22:55:59 +0000151 static boost::shared_ptr<ThreadManager> newThreadManager();
Marc Slemkod466b212006-07-20 00:04:18 +0000152
Mark Sleef5f2be42006-09-05 21:05:31 +0000153 /**
Marc Slemko3a3b53b2007-05-22 23:59:54 +0000154 * Creates a simple thread manager the uses count number of worker threads and has
155 * a pendingTaskCountMax maximum pending tasks. The default, 0, specified no limit
156 * on pending tasks
Mark Sleef5f2be42006-09-05 21:05:31 +0000157 */
Marc Slemko3a3b53b2007-05-22 23:59:54 +0000158 static boost::shared_ptr<ThreadManager> newSimpleThreadManager(size_t count=4, size_t pendingTaskCountMax=0);
Marc Slemko66949872006-07-15 01:52:39 +0000159
160 class Task;
Marc Slemko3a3b53b2007-05-22 23:59:54 +0000161
Marc Slemko66949872006-07-15 01:52:39 +0000162 class Worker;
163
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000164 class Impl;
Marc Slemko66949872006-07-15 01:52:39 +0000165};
166
T Jake Lucianib5e62212009-01-31 22:36:20 +0000167}}} // apache::thrift::concurrency
Marc Slemko66949872006-07-15 01:52:39 +0000168
Mark Sleef5f2be42006-09-05 21:05:31 +0000169#endif // #ifndef _THRIFT_CONCURRENCY_THREADMANAGER_H_