blob: 0540d9c7a545b517f7184e1bc2fb6a0301b28e6c [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 class TimerManager::Dispatcher;
Marc Slemko8a40a762006-07-19 17:46:50 +000064 friend class TimerManager::Dispatcher;
Mark Slee2f6404d2006-10-10 01:37:40 +000065 STATE state_;
Marc Slemko0e53ccd2006-07-17 23:51:05 +000066};
67
68class TimerManager::Dispatcher: public Runnable {
69
Mark Sleef5f2be42006-09-05 21:05:31 +000070 public:
David Reiss0c90f6f2008-02-06 22:18:40 +000071 Dispatcher(TimerManager* manager) :
Mark Slee2f6404d2006-10-10 01:37:40 +000072 manager_(manager) {}
David Reiss0c90f6f2008-02-06 22:18:40 +000073
Marc Slemko67606e52007-06-04 21:01:19 +000074 ~Dispatcher() {}
David Reiss0c90f6f2008-02-06 22:18:40 +000075
Mark Sleef5f2be42006-09-05 21:05:31 +000076 /**
77 * Dispatcher entry point
78 *
Mark Slee2f6404d2006-10-10 01:37:40 +000079 * As long as dispatcher thread is running, pull tasks off the task taskMap_
Mark Sleef5f2be42006-09-05 21:05:31 +000080 * and execute.
81 */
Marc Slemko0e53ccd2006-07-17 23:51:05 +000082 void run() {
Mark Sleef5f2be42006-09-05 21:05:31 +000083 {
Mark Slee2f6404d2006-10-10 01:37:40 +000084 Synchronized s(manager_->monitor_);
85 if (manager_->state_ == TimerManager::STARTING) {
David Reiss96d23882007-07-26 21:10:32 +000086 manager_->state_ = TimerManager::STARTED;
87 manager_->monitor_.notifyAll();
Marc Slemko0e53ccd2006-07-17 23:51:05 +000088 }
89 }
90
91 do {
Marc Slemko6f038a72006-08-03 18:58:09 +000092 std::set<shared_ptr<TimerManager::Task> > expiredTasks;
Mark Sleef5f2be42006-09-05 21:05:31 +000093 {
Mark Slee2f6404d2006-10-10 01:37:40 +000094 Synchronized s(manager_->monitor_);
David Reiss96d23882007-07-26 21:10:32 +000095 task_iterator expiredTaskEnd;
96 int64_t now = Util::currentTime();
David Reiss0c90f6f2008-02-06 22:18:40 +000097 while (manager_->state_ == TimerManager::STARTED &&
Mark Slee2f6404d2006-10-10 01:37:40 +000098 (expiredTaskEnd = manager_->taskMap_.upper_bound(now)) == manager_->taskMap_.begin()) {
David Reiss96d23882007-07-26 21:10:32 +000099 int64_t timeout = 0LL;
100 if (!manager_->taskMap_.empty()) {
Mark Slee2f6404d2006-10-10 01:37:40 +0000101 timeout = manager_->taskMap_.begin()->first - now;
David Reiss96d23882007-07-26 21:10:32 +0000102 }
Aditya Agarwal3f234da2007-04-01 01:19:57 +0000103 assert((timeout != 0 && manager_->taskCount_ > 0) || (timeout == 0 && manager_->taskCount_ == 0));
Marc Slemko3a3b53b2007-05-22 23:59:54 +0000104 try {
Mark Slee2782d6d2007-05-23 04:55:30 +0000105 manager_->monitor_.wait(timeout);
106 } catch (TimedOutException &e) {}
David Reiss96d23882007-07-26 21:10:32 +0000107 now = Util::currentTime();
108 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000109
David Reiss96d23882007-07-26 21:10:32 +0000110 if (manager_->state_ == TimerManager::STARTED) {
Mark Slee2f6404d2006-10-10 01:37:40 +0000111 for (task_iterator ix = manager_->taskMap_.begin(); ix != expiredTaskEnd; ix++) {
David Reiss96d23882007-07-26 21:10:32 +0000112 shared_ptr<TimerManager::Task> task = ix->second;
Mark Sleef5f2be42006-09-05 21:05:31 +0000113 expiredTasks.insert(task);
David Reiss96d23882007-07-26 21:10:32 +0000114 if (task->state_ == TimerManager::Task::WAITING) {
115 task->state_ = TimerManager::Task::EXECUTING;
116 }
Mark Slee2f6404d2006-10-10 01:37:40 +0000117 manager_->taskCount_--;
David Reiss96d23882007-07-26 21:10:32 +0000118 }
Mark Slee2f6404d2006-10-10 01:37:40 +0000119 manager_->taskMap_.erase(manager_->taskMap_.begin(), expiredTaskEnd);
David Reiss96d23882007-07-26 21:10:32 +0000120 }
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000121 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000122
Mark Sleef5f2be42006-09-05 21:05:31 +0000123 for (std::set<shared_ptr<Task> >::iterator ix = expiredTasks.begin(); ix != expiredTasks.end(); ix++) {
124 (*ix)->run();
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000125 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000126
Mark Slee2f6404d2006-10-10 01:37:40 +0000127 } while (manager_->state_ == TimerManager::STARTED);
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000128
Mark Sleef5f2be42006-09-05 21:05:31 +0000129 {
Mark Slee2f6404d2006-10-10 01:37:40 +0000130 Synchronized s(manager_->monitor_);
131 if (manager_->state_ == TimerManager::STOPPING) {
David Reiss0c90f6f2008-02-06 22:18:40 +0000132 manager_->state_ = TimerManager::STOPPED;
David Reiss96d23882007-07-26 21:10:32 +0000133 manager_->monitor_.notify();
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000134 }
135 }
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000136 return;
137 }
138
139 private:
Mark Slee2f6404d2006-10-10 01:37:40 +0000140 TimerManager* manager_;
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000141 friend class TimerManager;
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000142};
143
Marc Slemko8a40a762006-07-19 17:46:50 +0000144TimerManager::TimerManager() :
Mark Slee2f6404d2006-10-10 01:37:40 +0000145 taskCount_(0),
146 state_(TimerManager::UNINITIALIZED),
147 dispatcher_(shared_ptr<Dispatcher>(new Dispatcher(this))) {
Marc Slemko8a40a762006-07-19 17:46:50 +0000148}
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000149
Marc Slemko8a40a762006-07-19 17:46:50 +0000150
151TimerManager::~TimerManager() {
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000152
Mark Sleef5f2be42006-09-05 21:05:31 +0000153 // If we haven't been explicitly stopped, do so now. We don't need to grab
154 // the monitor here, since stop already takes care of reentrancy.
David Reiss0c90f6f2008-02-06 22:18:40 +0000155
Mark Slee2f6404d2006-10-10 01:37:40 +0000156 if (state_ != STOPPED) {
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000157 try {
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000158 stop();
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000159 } catch(...) {
Marc Slemko6f038a72006-08-03 18:58:09 +0000160 throw;
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000161 // uhoh
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000162 }
163 }
Marc Slemko8a40a762006-07-19 17:46:50 +0000164}
165
166void TimerManager::start() {
Marc Slemko8a40a762006-07-19 17:46:50 +0000167 bool doStart = false;
Mark Sleef5f2be42006-09-05 21:05:31 +0000168 {
Mark Slee2f6404d2006-10-10 01:37:40 +0000169 Synchronized s(monitor_);
170 if (threadFactory_ == NULL) {
Mark Sleef5f2be42006-09-05 21:05:31 +0000171 throw InvalidArgumentException();
172 }
Mark Slee2f6404d2006-10-10 01:37:40 +0000173 if (state_ == TimerManager::UNINITIALIZED) {
174 state_ = TimerManager::STARTING;
Marc Slemko8a40a762006-07-19 17:46:50 +0000175 doStart = true;
176 }
177 }
178
Mark Sleef5f2be42006-09-05 21:05:31 +0000179 if (doStart) {
Mark Slee2f6404d2006-10-10 01:37:40 +0000180 dispatcherThread_ = threadFactory_->newThread(dispatcher_);
181 dispatcherThread_->start();
Marc Slemko8a40a762006-07-19 17:46:50 +0000182 }
183
Mark Sleef5f2be42006-09-05 21:05:31 +0000184 {
Mark Slee2f6404d2006-10-10 01:37:40 +0000185 Synchronized s(monitor_);
186 while (state_ == TimerManager::STARTING) {
187 monitor_.wait();
Marc Slemko8a40a762006-07-19 17:46:50 +0000188 }
Aditya Agarwal3f234da2007-04-01 01:19:57 +0000189 assert(state_ != TimerManager::STARTING);
Marc Slemko8a40a762006-07-19 17:46:50 +0000190 }
191}
192
193void TimerManager::stop() {
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000194 bool doStop = false;
Mark Sleef5f2be42006-09-05 21:05:31 +0000195 {
Mark Slee2f6404d2006-10-10 01:37:40 +0000196 Synchronized s(monitor_);
197 if (state_ == TimerManager::UNINITIALIZED) {
198 state_ = TimerManager::STOPPED;
199 } else if (state_ != STOPPING && state_ != STOPPED) {
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000200 doStop = true;
Mark Slee2f6404d2006-10-10 01:37:40 +0000201 state_ = STOPPING;
202 monitor_.notifyAll();
Marc Slemko8a40a762006-07-19 17:46:50 +0000203 }
Mark Slee2f6404d2006-10-10 01:37:40 +0000204 while (state_ != STOPPED) {
205 monitor_.wait();
Marc Slemko8a40a762006-07-19 17:46:50 +0000206 }
207 }
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000208
Mark Sleef5f2be42006-09-05 21:05:31 +0000209 if (doStop) {
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000210 // Clean up any outstanding tasks
David Reiss5fa20da2009-06-04 00:32:47 +0000211 taskMap_.clear();
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000212
David Reiss0c90f6f2008-02-06 22:18:40 +0000213 // Remove dispatcher's reference to us.
Mark Slee2f6404d2006-10-10 01:37:40 +0000214 dispatcher_->manager_ = NULL;
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000215 }
Marc Slemko8a40a762006-07-19 17:46:50 +0000216}
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000217
Marc Slemko6f038a72006-08-03 18:58:09 +0000218shared_ptr<const ThreadFactory> TimerManager::threadFactory() const {
David Reiss0c90f6f2008-02-06 22:18:40 +0000219 Synchronized s(monitor_);
Mark Slee2f6404d2006-10-10 01:37:40 +0000220 return threadFactory_;
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000221}
David Reiss0c90f6f2008-02-06 22:18:40 +0000222
Marc Slemko6f038a72006-08-03 18:58:09 +0000223void TimerManager::threadFactory(shared_ptr<const ThreadFactory> value) {
David Reiss0c90f6f2008-02-06 22:18:40 +0000224 Synchronized s(monitor_);
Mark Slee2f6404d2006-10-10 01:37:40 +0000225 threadFactory_ = value;
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000226}
227
Marc Slemko8a40a762006-07-19 17:46:50 +0000228size_t TimerManager::taskCount() const {
Mark Slee2f6404d2006-10-10 01:37:40 +0000229 return taskCount_;
Marc Slemko8a40a762006-07-19 17:46:50 +0000230}
David Reiss0c90f6f2008-02-06 22:18:40 +0000231
Mark Slee9b82d272007-05-23 05:16:07 +0000232void TimerManager::add(shared_ptr<Runnable> task, int64_t timeout) {
233 int64_t now = Util::currentTime();
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000234 timeout += now;
235
Mark Sleef5f2be42006-09-05 21:05:31 +0000236 {
David Reiss0c90f6f2008-02-06 22:18:40 +0000237 Synchronized s(monitor_);
Mark Slee2f6404d2006-10-10 01:37:40 +0000238 if (state_ != TimerManager::STARTED) {
Marc Slemko8a40a762006-07-19 17:46:50 +0000239 throw IllegalStateException();
240 }
241
David Reiss52687eb2009-06-04 00:32:57 +0000242 // If the task map is empty, we will kick the dispatcher for sure. Otherwise, we kick him
243 // if the expiration time is shorter than the current value. Need to test before we insert,
244 // because the new task might insert at the front.
245 bool notifyRequired = (taskCount_ == 0) ? true : timeout < taskMap_.begin()->first;
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
David Reiss52687eb2009-06-04 00:32:57 +0000253 if (notifyRequired) {
Mark Slee2f6404d2006-10-10 01:37:40 +0000254 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