blob: f7acd0add0f65bc262bcacbfc60986db127c7b13 [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 Sleef5f2be42006-09-05 21:05:31 +000032/**
33 * TimerManager class
34 *
Mark Sleef5f2be42006-09-05 21:05:31 +000035 * @version $Id:$
36 */
Marc Slemko0e53ccd2006-07-17 23:51:05 +000037class TimerManager::Task : public Runnable {
38
Mark Sleef5f2be42006-09-05 21:05:31 +000039 public:
Marc Slemko0e53ccd2006-07-17 23:51:05 +000040 enum STATE {
41 WAITING,
42 EXECUTING,
43 CANCELLED,
44 COMPLETE
45 };
46
Marc Slemko6f038a72006-08-03 18:58:09 +000047 Task(shared_ptr<Runnable> runnable) :
Mark Slee2f6404d2006-10-10 01:37:40 +000048 runnable_(runnable),
49 state_(WAITING) {}
David Reiss0c90f6f2008-02-06 22:18:40 +000050
Marc Slemko6f038a72006-08-03 18:58:09 +000051 ~Task() {
Mark Sleef5f2be42006-09-05 21:05:31 +000052 }
David Reiss0c90f6f2008-02-06 22:18:40 +000053
Marc Slemko0e53ccd2006-07-17 23:51:05 +000054 void run() {
Mark Slee2f6404d2006-10-10 01:37:40 +000055 if (state_ == EXECUTING) {
56 runnable_->run();
57 state_ = COMPLETE;
Marc Slemko0e53ccd2006-07-17 23:51:05 +000058 }
59 }
60
61 private:
Mark Slee2f6404d2006-10-10 01:37:40 +000062 shared_ptr<Runnable> runnable_;
Marc Slemko8a40a762006-07-19 17:46:50 +000063 friend class TimerManager::Dispatcher;
Mark Slee2f6404d2006-10-10 01:37:40 +000064 STATE state_;
Marc Slemko0e53ccd2006-07-17 23:51:05 +000065};
66
67class TimerManager::Dispatcher: public Runnable {
68
Mark Sleef5f2be42006-09-05 21:05:31 +000069 public:
David Reiss0c90f6f2008-02-06 22:18:40 +000070 Dispatcher(TimerManager* manager) :
Mark Slee2f6404d2006-10-10 01:37:40 +000071 manager_(manager) {}
David Reiss0c90f6f2008-02-06 22:18:40 +000072
Marc Slemko67606e52007-06-04 21:01:19 +000073 ~Dispatcher() {}
David Reiss0c90f6f2008-02-06 22:18:40 +000074
Mark Sleef5f2be42006-09-05 21:05:31 +000075 /**
76 * Dispatcher entry point
77 *
Mark Slee2f6404d2006-10-10 01:37:40 +000078 * As long as dispatcher thread is running, pull tasks off the task taskMap_
Mark Sleef5f2be42006-09-05 21:05:31 +000079 * and execute.
80 */
Marc Slemko0e53ccd2006-07-17 23:51:05 +000081 void run() {
Mark Sleef5f2be42006-09-05 21:05:31 +000082 {
Mark Slee2f6404d2006-10-10 01:37:40 +000083 Synchronized s(manager_->monitor_);
84 if (manager_->state_ == TimerManager::STARTING) {
David Reiss96d23882007-07-26 21:10:32 +000085 manager_->state_ = TimerManager::STARTED;
86 manager_->monitor_.notifyAll();
Marc Slemko0e53ccd2006-07-17 23:51:05 +000087 }
88 }
89
90 do {
Marc Slemko6f038a72006-08-03 18:58:09 +000091 std::set<shared_ptr<TimerManager::Task> > expiredTasks;
Mark Sleef5f2be42006-09-05 21:05:31 +000092 {
Mark Slee2f6404d2006-10-10 01:37:40 +000093 Synchronized s(manager_->monitor_);
David Reiss96d23882007-07-26 21:10:32 +000094 task_iterator expiredTaskEnd;
95 int64_t now = Util::currentTime();
David Reiss0c90f6f2008-02-06 22:18:40 +000096 while (manager_->state_ == TimerManager::STARTED &&
Mark Slee2f6404d2006-10-10 01:37:40 +000097 (expiredTaskEnd = manager_->taskMap_.upper_bound(now)) == manager_->taskMap_.begin()) {
David Reiss96d23882007-07-26 21:10:32 +000098 int64_t timeout = 0LL;
99 if (!manager_->taskMap_.empty()) {
Mark Slee2f6404d2006-10-10 01:37:40 +0000100 timeout = manager_->taskMap_.begin()->first - now;
David Reiss96d23882007-07-26 21:10:32 +0000101 }
Aditya Agarwal3f234da2007-04-01 01:19:57 +0000102 assert((timeout != 0 && manager_->taskCount_ > 0) || (timeout == 0 && manager_->taskCount_ == 0));
Marc Slemko3a3b53b2007-05-22 23:59:54 +0000103 try {
Mark Slee2782d6d2007-05-23 04:55:30 +0000104 manager_->monitor_.wait(timeout);
105 } catch (TimedOutException &e) {}
David Reiss96d23882007-07-26 21:10:32 +0000106 now = Util::currentTime();
107 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000108
David Reiss96d23882007-07-26 21:10:32 +0000109 if (manager_->state_ == TimerManager::STARTED) {
Mark Slee2f6404d2006-10-10 01:37:40 +0000110 for (task_iterator ix = manager_->taskMap_.begin(); ix != expiredTaskEnd; ix++) {
David Reiss96d23882007-07-26 21:10:32 +0000111 shared_ptr<TimerManager::Task> task = ix->second;
Mark Sleef5f2be42006-09-05 21:05:31 +0000112 expiredTasks.insert(task);
David Reiss96d23882007-07-26 21:10:32 +0000113 if (task->state_ == TimerManager::Task::WAITING) {
114 task->state_ = TimerManager::Task::EXECUTING;
115 }
Mark Slee2f6404d2006-10-10 01:37:40 +0000116 manager_->taskCount_--;
David Reiss96d23882007-07-26 21:10:32 +0000117 }
Mark Slee2f6404d2006-10-10 01:37:40 +0000118 manager_->taskMap_.erase(manager_->taskMap_.begin(), expiredTaskEnd);
David Reiss96d23882007-07-26 21:10:32 +0000119 }
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000120 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000121
Mark Sleef5f2be42006-09-05 21:05:31 +0000122 for (std::set<shared_ptr<Task> >::iterator ix = expiredTasks.begin(); ix != expiredTasks.end(); ix++) {
123 (*ix)->run();
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000124 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000125
Mark Slee2f6404d2006-10-10 01:37:40 +0000126 } while (manager_->state_ == TimerManager::STARTED);
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000127
Mark Sleef5f2be42006-09-05 21:05:31 +0000128 {
Mark Slee2f6404d2006-10-10 01:37:40 +0000129 Synchronized s(manager_->monitor_);
130 if (manager_->state_ == TimerManager::STOPPING) {
David Reiss0c90f6f2008-02-06 22:18:40 +0000131 manager_->state_ = TimerManager::STOPPED;
David Reiss96d23882007-07-26 21:10:32 +0000132 manager_->monitor_.notify();
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000133 }
134 }
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000135 return;
136 }
137
138 private:
Mark Slee2f6404d2006-10-10 01:37:40 +0000139 TimerManager* manager_;
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000140 friend class TimerManager;
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000141};
142
Marc Slemko8a40a762006-07-19 17:46:50 +0000143TimerManager::TimerManager() :
Mark Slee2f6404d2006-10-10 01:37:40 +0000144 taskCount_(0),
145 state_(TimerManager::UNINITIALIZED),
146 dispatcher_(shared_ptr<Dispatcher>(new Dispatcher(this))) {
Marc Slemko8a40a762006-07-19 17:46:50 +0000147}
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000148
Marc Slemko8a40a762006-07-19 17:46:50 +0000149
150TimerManager::~TimerManager() {
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000151
Mark Sleef5f2be42006-09-05 21:05:31 +0000152 // If we haven't been explicitly stopped, do so now. We don't need to grab
153 // the monitor here, since stop already takes care of reentrancy.
David Reiss0c90f6f2008-02-06 22:18:40 +0000154
Mark Slee2f6404d2006-10-10 01:37:40 +0000155 if (state_ != STOPPED) {
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000156 try {
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000157 stop();
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000158 } catch(...) {
Marc Slemko6f038a72006-08-03 18:58:09 +0000159 throw;
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000160 // uhoh
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000161 }
162 }
Marc Slemko8a40a762006-07-19 17:46:50 +0000163}
164
165void TimerManager::start() {
Marc Slemko8a40a762006-07-19 17:46:50 +0000166 bool doStart = false;
Mark Sleef5f2be42006-09-05 21:05:31 +0000167 {
Mark Slee2f6404d2006-10-10 01:37:40 +0000168 Synchronized s(monitor_);
169 if (threadFactory_ == NULL) {
Mark Sleef5f2be42006-09-05 21:05:31 +0000170 throw InvalidArgumentException();
171 }
Mark Slee2f6404d2006-10-10 01:37:40 +0000172 if (state_ == TimerManager::UNINITIALIZED) {
173 state_ = TimerManager::STARTING;
Marc Slemko8a40a762006-07-19 17:46:50 +0000174 doStart = true;
175 }
176 }
177
Mark Sleef5f2be42006-09-05 21:05:31 +0000178 if (doStart) {
Mark Slee2f6404d2006-10-10 01:37:40 +0000179 dispatcherThread_ = threadFactory_->newThread(dispatcher_);
180 dispatcherThread_->start();
Marc Slemko8a40a762006-07-19 17:46:50 +0000181 }
182
Mark Sleef5f2be42006-09-05 21:05:31 +0000183 {
Mark Slee2f6404d2006-10-10 01:37:40 +0000184 Synchronized s(monitor_);
185 while (state_ == TimerManager::STARTING) {
186 monitor_.wait();
Marc Slemko8a40a762006-07-19 17:46:50 +0000187 }
Aditya Agarwal3f234da2007-04-01 01:19:57 +0000188 assert(state_ != TimerManager::STARTING);
Marc Slemko8a40a762006-07-19 17:46:50 +0000189 }
190}
191
192void TimerManager::stop() {
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000193 bool doStop = false;
Mark Sleef5f2be42006-09-05 21:05:31 +0000194 {
Mark Slee2f6404d2006-10-10 01:37:40 +0000195 Synchronized s(monitor_);
196 if (state_ == TimerManager::UNINITIALIZED) {
197 state_ = TimerManager::STOPPED;
198 } else if (state_ != STOPPING && state_ != STOPPED) {
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000199 doStop = true;
Mark Slee2f6404d2006-10-10 01:37:40 +0000200 state_ = STOPPING;
201 monitor_.notifyAll();
Marc Slemko8a40a762006-07-19 17:46:50 +0000202 }
Mark Slee2f6404d2006-10-10 01:37:40 +0000203 while (state_ != STOPPED) {
204 monitor_.wait();
Marc Slemko8a40a762006-07-19 17:46:50 +0000205 }
206 }
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000207
Mark Sleef5f2be42006-09-05 21:05:31 +0000208 if (doStop) {
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000209 // Clean up any outstanding tasks
David Reiss5fa20da2009-06-04 00:32:47 +0000210 taskMap_.clear();
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000211
David Reiss0c90f6f2008-02-06 22:18:40 +0000212 // Remove dispatcher's reference to us.
Mark Slee2f6404d2006-10-10 01:37:40 +0000213 dispatcher_->manager_ = NULL;
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000214 }
Marc Slemko8a40a762006-07-19 17:46:50 +0000215}
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000216
Marc Slemko6f038a72006-08-03 18:58:09 +0000217shared_ptr<const ThreadFactory> TimerManager::threadFactory() const {
David Reiss0c90f6f2008-02-06 22:18:40 +0000218 Synchronized s(monitor_);
Mark Slee2f6404d2006-10-10 01:37:40 +0000219 return threadFactory_;
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000220}
David Reiss0c90f6f2008-02-06 22:18:40 +0000221
Marc Slemko6f038a72006-08-03 18:58:09 +0000222void TimerManager::threadFactory(shared_ptr<const ThreadFactory> value) {
David Reiss0c90f6f2008-02-06 22:18:40 +0000223 Synchronized s(monitor_);
Mark Slee2f6404d2006-10-10 01:37:40 +0000224 threadFactory_ = value;
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000225}
226
Marc Slemko8a40a762006-07-19 17:46:50 +0000227size_t TimerManager::taskCount() const {
Mark Slee2f6404d2006-10-10 01:37:40 +0000228 return taskCount_;
Marc Slemko8a40a762006-07-19 17:46:50 +0000229}
David Reiss0c90f6f2008-02-06 22:18:40 +0000230
Mark Slee9b82d272007-05-23 05:16:07 +0000231void TimerManager::add(shared_ptr<Runnable> task, int64_t timeout) {
232 int64_t now = Util::currentTime();
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000233 timeout += now;
234
Mark Sleef5f2be42006-09-05 21:05:31 +0000235 {
David Reiss0c90f6f2008-02-06 22:18:40 +0000236 Synchronized s(monitor_);
Mark Slee2f6404d2006-10-10 01:37:40 +0000237 if (state_ != TimerManager::STARTED) {
Marc Slemko8a40a762006-07-19 17:46:50 +0000238 throw IllegalStateException();
239 }
240
David Reiss52687eb2009-06-04 00:32:57 +0000241 // If the task map is empty, we will kick the dispatcher for sure. Otherwise, we kick him
242 // if the expiration time is shorter than the current value. Need to test before we insert,
243 // because the new task might insert at the front.
244 bool notifyRequired = (taskCount_ == 0) ? true : timeout < taskMap_.begin()->first;
245
Mark Slee2f6404d2006-10-10 01:37:40 +0000246 taskCount_++;
Mark Slee9b82d272007-05-23 05:16:07 +0000247 taskMap_.insert(std::pair<int64_t, shared_ptr<Task> >(timeout, shared_ptr<Task>(new Task(task))));
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000248
Mark Sleef5f2be42006-09-05 21:05:31 +0000249 // If the task map was empty, or if we have an expiration that is earlier
250 // than any previously seen, kick the dispatcher so it can update its
251 // timeout
David Reiss52687eb2009-06-04 00:32:57 +0000252 if (notifyRequired) {
Mark Slee2f6404d2006-10-10 01:37:40 +0000253 monitor_.notify();
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000254 }
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000255 }
256}
257
Marc Slemko6f038a72006-08-03 18:58:09 +0000258void TimerManager::add(shared_ptr<Runnable> task, const struct timespec& value) {
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000259
Mark Slee9b82d272007-05-23 05:16:07 +0000260 int64_t expiration;
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000261 Util::toMilliseconds(expiration, value);
262
Mark Slee9b82d272007-05-23 05:16:07 +0000263 int64_t now = Util::currentTime();
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000264
Mark Sleef5f2be42006-09-05 21:05:31 +0000265 if (expiration < now) {
Marc Slemko8a40a762006-07-19 17:46:50 +0000266 throw InvalidArgumentException();
267 }
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000268
269 add(task, expiration - now);
270}
271
272
Marc Slemko6f038a72006-08-03 18:58:09 +0000273void TimerManager::remove(shared_ptr<Runnable> task) {
Roger Meier3b771a12010-11-17 22:11:26 +0000274 (void) 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
Roger Meier3b771a12010-11-17 22:11:26 +0000281TimerManager::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