blob: 25515dc820343de1856a4fe1e122bd27700e5f1e [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
Marc Slemko0e53ccd2006-07-17 23:51:05 +000020#include "TimerManager.h"
Marc Slemko8a40a762006-07-19 17:46:50 +000021#include "Exception.h"
Marc Slemko0e53ccd2006-07-17 23:51:05 +000022#include "Util.h"
23
24#include <assert.h>
Marc Slemko8a40a762006-07-19 17:46:50 +000025#include <iostream>
Marc Slemko0e53ccd2006-07-17 23:51:05 +000026#include <set>
27
T Jake Lucianib5e62212009-01-31 22:36:20 +000028namespace apache { namespace thrift { namespace concurrency {
Marc Slemko0e53ccd2006-07-17 23:51:05 +000029
Mark Slee5ea15f92007-03-05 22:55:59 +000030using boost::shared_ptr;
31
Mark Slee9b82d272007-05-23 05:16:07 +000032typedef std::multimap<int64_t, shared_ptr<TimerManager::Task> >::iterator task_iterator;
Marc Slemko0e53ccd2006-07-17 23:51:05 +000033typedef std::pair<task_iterator, task_iterator> task_range;
34
Mark Sleef5f2be42006-09-05 21:05:31 +000035/**
36 * TimerManager class
37 *
Mark Sleef5f2be42006-09-05 21:05:31 +000038 * @version $Id:$
39 */
Marc Slemko0e53ccd2006-07-17 23:51:05 +000040class TimerManager::Task : public Runnable {
41
Mark Sleef5f2be42006-09-05 21:05:31 +000042 public:
Marc Slemko0e53ccd2006-07-17 23:51:05 +000043 enum STATE {
44 WAITING,
45 EXECUTING,
46 CANCELLED,
47 COMPLETE
48 };
49
Marc Slemko6f038a72006-08-03 18:58:09 +000050 Task(shared_ptr<Runnable> runnable) :
Mark Slee2f6404d2006-10-10 01:37:40 +000051 runnable_(runnable),
52 state_(WAITING) {}
David Reiss0c90f6f2008-02-06 22:18:40 +000053
Marc Slemko6f038a72006-08-03 18:58:09 +000054 ~Task() {
Mark Sleef5f2be42006-09-05 21:05:31 +000055 }
David Reiss0c90f6f2008-02-06 22:18:40 +000056
Marc Slemko0e53ccd2006-07-17 23:51:05 +000057 void run() {
Mark Slee2f6404d2006-10-10 01:37:40 +000058 if (state_ == EXECUTING) {
59 runnable_->run();
60 state_ = COMPLETE;
Marc Slemko0e53ccd2006-07-17 23:51:05 +000061 }
62 }
63
64 private:
Mark Slee2f6404d2006-10-10 01:37:40 +000065 shared_ptr<Runnable> runnable_;
Marc Slemko8a40a762006-07-19 17:46:50 +000066 class TimerManager::Dispatcher;
Marc Slemko8a40a762006-07-19 17:46:50 +000067 friend class TimerManager::Dispatcher;
Mark Slee2f6404d2006-10-10 01:37:40 +000068 STATE state_;
Marc Slemko0e53ccd2006-07-17 23:51:05 +000069};
70
71class TimerManager::Dispatcher: public Runnable {
72
Mark Sleef5f2be42006-09-05 21:05:31 +000073 public:
David Reiss0c90f6f2008-02-06 22:18:40 +000074 Dispatcher(TimerManager* manager) :
Mark Slee2f6404d2006-10-10 01:37:40 +000075 manager_(manager) {}
David Reiss0c90f6f2008-02-06 22:18:40 +000076
Marc Slemko67606e52007-06-04 21:01:19 +000077 ~Dispatcher() {}
David Reiss0c90f6f2008-02-06 22:18:40 +000078
Mark Sleef5f2be42006-09-05 21:05:31 +000079 /**
80 * Dispatcher entry point
81 *
Mark Slee2f6404d2006-10-10 01:37:40 +000082 * As long as dispatcher thread is running, pull tasks off the task taskMap_
Mark Sleef5f2be42006-09-05 21:05:31 +000083 * and execute.
84 */
Marc Slemko0e53ccd2006-07-17 23:51:05 +000085 void run() {
Mark Sleef5f2be42006-09-05 21:05:31 +000086 {
Mark Slee2f6404d2006-10-10 01:37:40 +000087 Synchronized s(manager_->monitor_);
88 if (manager_->state_ == TimerManager::STARTING) {
David Reiss96d23882007-07-26 21:10:32 +000089 manager_->state_ = TimerManager::STARTED;
90 manager_->monitor_.notifyAll();
Marc Slemko0e53ccd2006-07-17 23:51:05 +000091 }
92 }
93
94 do {
Marc Slemko6f038a72006-08-03 18:58:09 +000095 std::set<shared_ptr<TimerManager::Task> > expiredTasks;
Mark Sleef5f2be42006-09-05 21:05:31 +000096 {
Mark Slee2f6404d2006-10-10 01:37:40 +000097 Synchronized s(manager_->monitor_);
David Reiss96d23882007-07-26 21:10:32 +000098 task_iterator expiredTaskEnd;
99 int64_t now = Util::currentTime();
David Reiss0c90f6f2008-02-06 22:18:40 +0000100 while (manager_->state_ == TimerManager::STARTED &&
Mark Slee2f6404d2006-10-10 01:37:40 +0000101 (expiredTaskEnd = manager_->taskMap_.upper_bound(now)) == manager_->taskMap_.begin()) {
David Reiss96d23882007-07-26 21:10:32 +0000102 int64_t timeout = 0LL;
103 if (!manager_->taskMap_.empty()) {
Mark Slee2f6404d2006-10-10 01:37:40 +0000104 timeout = manager_->taskMap_.begin()->first - now;
David Reiss96d23882007-07-26 21:10:32 +0000105 }
Aditya Agarwal3f234da2007-04-01 01:19:57 +0000106 assert((timeout != 0 && manager_->taskCount_ > 0) || (timeout == 0 && manager_->taskCount_ == 0));
Marc Slemko3a3b53b2007-05-22 23:59:54 +0000107 try {
Mark Slee2782d6d2007-05-23 04:55:30 +0000108 manager_->monitor_.wait(timeout);
109 } catch (TimedOutException &e) {}
David Reiss96d23882007-07-26 21:10:32 +0000110 now = Util::currentTime();
111 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000112
David Reiss96d23882007-07-26 21:10:32 +0000113 if (manager_->state_ == TimerManager::STARTED) {
Mark Slee2f6404d2006-10-10 01:37:40 +0000114 for (task_iterator ix = manager_->taskMap_.begin(); ix != expiredTaskEnd; ix++) {
David Reiss96d23882007-07-26 21:10:32 +0000115 shared_ptr<TimerManager::Task> task = ix->second;
Mark Sleef5f2be42006-09-05 21:05:31 +0000116 expiredTasks.insert(task);
David Reiss96d23882007-07-26 21:10:32 +0000117 if (task->state_ == TimerManager::Task::WAITING) {
118 task->state_ = TimerManager::Task::EXECUTING;
119 }
Mark Slee2f6404d2006-10-10 01:37:40 +0000120 manager_->taskCount_--;
David Reiss96d23882007-07-26 21:10:32 +0000121 }
Mark Slee2f6404d2006-10-10 01:37:40 +0000122 manager_->taskMap_.erase(manager_->taskMap_.begin(), expiredTaskEnd);
David Reiss96d23882007-07-26 21:10:32 +0000123 }
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000124 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000125
Mark Sleef5f2be42006-09-05 21:05:31 +0000126 for (std::set<shared_ptr<Task> >::iterator ix = expiredTasks.begin(); ix != expiredTasks.end(); ix++) {
127 (*ix)->run();
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000128 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000129
Mark Slee2f6404d2006-10-10 01:37:40 +0000130 } while (manager_->state_ == TimerManager::STARTED);
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000131
Mark Sleef5f2be42006-09-05 21:05:31 +0000132 {
Mark Slee2f6404d2006-10-10 01:37:40 +0000133 Synchronized s(manager_->monitor_);
134 if (manager_->state_ == TimerManager::STOPPING) {
David Reiss0c90f6f2008-02-06 22:18:40 +0000135 manager_->state_ = TimerManager::STOPPED;
David Reiss96d23882007-07-26 21:10:32 +0000136 manager_->monitor_.notify();
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000137 }
138 }
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000139 return;
140 }
141
142 private:
Mark Slee2f6404d2006-10-10 01:37:40 +0000143 TimerManager* manager_;
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000144 friend class TimerManager;
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000145};
146
Marc Slemko8a40a762006-07-19 17:46:50 +0000147TimerManager::TimerManager() :
Mark Slee2f6404d2006-10-10 01:37:40 +0000148 taskCount_(0),
149 state_(TimerManager::UNINITIALIZED),
150 dispatcher_(shared_ptr<Dispatcher>(new Dispatcher(this))) {
Marc Slemko8a40a762006-07-19 17:46:50 +0000151}
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000152
Marc Slemko8a40a762006-07-19 17:46:50 +0000153
154TimerManager::~TimerManager() {
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000155
Mark Sleef5f2be42006-09-05 21:05:31 +0000156 // If we haven't been explicitly stopped, do so now. We don't need to grab
157 // the monitor here, since stop already takes care of reentrancy.
David Reiss0c90f6f2008-02-06 22:18:40 +0000158
Mark Slee2f6404d2006-10-10 01:37:40 +0000159 if (state_ != STOPPED) {
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000160 try {
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000161 stop();
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000162 } catch(...) {
Marc Slemko6f038a72006-08-03 18:58:09 +0000163 throw;
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000164 // uhoh
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000165 }
166 }
Marc Slemko8a40a762006-07-19 17:46:50 +0000167}
168
169void TimerManager::start() {
Marc Slemko8a40a762006-07-19 17:46:50 +0000170 bool doStart = false;
Mark Sleef5f2be42006-09-05 21:05:31 +0000171 {
Mark Slee2f6404d2006-10-10 01:37:40 +0000172 Synchronized s(monitor_);
173 if (threadFactory_ == NULL) {
Mark Sleef5f2be42006-09-05 21:05:31 +0000174 throw InvalidArgumentException();
175 }
Mark Slee2f6404d2006-10-10 01:37:40 +0000176 if (state_ == TimerManager::UNINITIALIZED) {
177 state_ = TimerManager::STARTING;
Marc Slemko8a40a762006-07-19 17:46:50 +0000178 doStart = true;
179 }
180 }
181
Mark Sleef5f2be42006-09-05 21:05:31 +0000182 if (doStart) {
Mark Slee2f6404d2006-10-10 01:37:40 +0000183 dispatcherThread_ = threadFactory_->newThread(dispatcher_);
184 dispatcherThread_->start();
Marc Slemko8a40a762006-07-19 17:46:50 +0000185 }
186
Mark Sleef5f2be42006-09-05 21:05:31 +0000187 {
Mark Slee2f6404d2006-10-10 01:37:40 +0000188 Synchronized s(monitor_);
189 while (state_ == TimerManager::STARTING) {
190 monitor_.wait();
Marc Slemko8a40a762006-07-19 17:46:50 +0000191 }
Aditya Agarwal3f234da2007-04-01 01:19:57 +0000192 assert(state_ != TimerManager::STARTING);
Marc Slemko8a40a762006-07-19 17:46:50 +0000193 }
194}
195
196void TimerManager::stop() {
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000197 bool doStop = false;
Mark Sleef5f2be42006-09-05 21:05:31 +0000198 {
Mark Slee2f6404d2006-10-10 01:37:40 +0000199 Synchronized s(monitor_);
200 if (state_ == TimerManager::UNINITIALIZED) {
201 state_ = TimerManager::STOPPED;
202 } else if (state_ != STOPPING && state_ != STOPPED) {
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000203 doStop = true;
Mark Slee2f6404d2006-10-10 01:37:40 +0000204 state_ = STOPPING;
205 monitor_.notifyAll();
Marc Slemko8a40a762006-07-19 17:46:50 +0000206 }
Mark Slee2f6404d2006-10-10 01:37:40 +0000207 while (state_ != STOPPED) {
208 monitor_.wait();
Marc Slemko8a40a762006-07-19 17:46:50 +0000209 }
210 }
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000211
Mark Sleef5f2be42006-09-05 21:05:31 +0000212 if (doStop) {
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000213 // Clean up any outstanding tasks
Mark Slee2f6404d2006-10-10 01:37:40 +0000214 for (task_iterator ix = taskMap_.begin(); ix != taskMap_.end(); ix++) {
215 taskMap_.erase(ix);
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000216 }
217
David Reiss0c90f6f2008-02-06 22:18:40 +0000218 // Remove dispatcher's reference to us.
Mark Slee2f6404d2006-10-10 01:37:40 +0000219 dispatcher_->manager_ = NULL;
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000220 }
Marc Slemko8a40a762006-07-19 17:46:50 +0000221}
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000222
Marc Slemko6f038a72006-08-03 18:58:09 +0000223shared_ptr<const ThreadFactory> TimerManager::threadFactory() const {
David Reiss0c90f6f2008-02-06 22:18:40 +0000224 Synchronized s(monitor_);
Mark Slee2f6404d2006-10-10 01:37:40 +0000225 return threadFactory_;
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000226}
David Reiss0c90f6f2008-02-06 22:18:40 +0000227
Marc Slemko6f038a72006-08-03 18:58:09 +0000228void TimerManager::threadFactory(shared_ptr<const ThreadFactory> value) {
David Reiss0c90f6f2008-02-06 22:18:40 +0000229 Synchronized s(monitor_);
Mark Slee2f6404d2006-10-10 01:37:40 +0000230 threadFactory_ = value;
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000231}
232
Marc Slemko8a40a762006-07-19 17:46:50 +0000233size_t TimerManager::taskCount() const {
Mark Slee2f6404d2006-10-10 01:37:40 +0000234 return taskCount_;
Marc Slemko8a40a762006-07-19 17:46:50 +0000235}
David Reiss0c90f6f2008-02-06 22:18:40 +0000236
Mark Slee9b82d272007-05-23 05:16:07 +0000237void TimerManager::add(shared_ptr<Runnable> task, int64_t timeout) {
238 int64_t now = Util::currentTime();
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000239 timeout += now;
240
Mark Sleef5f2be42006-09-05 21:05:31 +0000241 {
David Reiss0c90f6f2008-02-06 22:18:40 +0000242 Synchronized s(monitor_);
Mark Slee2f6404d2006-10-10 01:37:40 +0000243 if (state_ != TimerManager::STARTED) {
Marc Slemko8a40a762006-07-19 17:46:50 +0000244 throw IllegalStateException();
245 }
246
Mark Slee2f6404d2006-10-10 01:37:40 +0000247 taskCount_++;
Mark Slee9b82d272007-05-23 05:16:07 +0000248 taskMap_.insert(std::pair<int64_t, shared_ptr<Task> >(timeout, shared_ptr<Task>(new Task(task))));
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000249
Mark Sleef5f2be42006-09-05 21:05:31 +0000250 // If the task map was empty, or if we have an expiration that is earlier
251 // than any previously seen, kick the dispatcher so it can update its
252 // timeout
Mark Slee2f6404d2006-10-10 01:37:40 +0000253 if (taskCount_ == 1 || timeout < taskMap_.begin()->first) {
254 monitor_.notify();
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000255 }
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000256 }
257}
258
Marc Slemko6f038a72006-08-03 18:58:09 +0000259void TimerManager::add(shared_ptr<Runnable> task, const struct timespec& value) {
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000260
Mark Slee9b82d272007-05-23 05:16:07 +0000261 int64_t expiration;
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000262 Util::toMilliseconds(expiration, value);
263
Mark Slee9b82d272007-05-23 05:16:07 +0000264 int64_t now = Util::currentTime();
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000265
Mark Sleef5f2be42006-09-05 21:05:31 +0000266 if (expiration < now) {
Marc Slemko8a40a762006-07-19 17:46:50 +0000267 throw InvalidArgumentException();
268 }
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000269
270 add(task, expiration - now);
271}
272
273
Marc Slemko6f038a72006-08-03 18:58:09 +0000274void TimerManager::remove(shared_ptr<Runnable> task) {
David Reiss0c90f6f2008-02-06 22:18:40 +0000275 Synchronized s(monitor_);
Mark Slee2f6404d2006-10-10 01:37:40 +0000276 if (state_ != TimerManager::STARTED) {
Mark Sleef5f2be42006-09-05 21:05:31 +0000277 throw IllegalStateException();
Marc Slemko8a40a762006-07-19 17:46:50 +0000278 }
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000279}
280
Mark Slee2f6404d2006-10-10 01:37:40 +0000281const TimerManager::STATE TimerManager::state() const { return state_; }
Marc Slemko8a40a762006-07-19 17:46:50 +0000282
T Jake Lucianib5e62212009-01-31 22:36:20 +0000283}}} // apache::thrift::concurrency
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000284