blob: 050885d40bfb326e3a41b9c1ec6d8207da59f123 [file] [log] [blame]
Mark Slee9f0c6512007-02-28 23:58:26 +00001// Copyright (c) 2006- Facebook
2// Distributed under the Thrift Software License
3//
4// See accompanying file LICENSE or visit the Thrift site at:
5// http://developers.facebook.com/thrift/
6
Marc Slemko0e53ccd2006-07-17 23:51:05 +00007#include "TimerManager.h"
Marc Slemko8a40a762006-07-19 17:46:50 +00008#include "Exception.h"
Marc Slemko0e53ccd2006-07-17 23:51:05 +00009#include "Util.h"
10
11#include <assert.h>
Marc Slemko8a40a762006-07-19 17:46:50 +000012#include <iostream>
Marc Slemko0e53ccd2006-07-17 23:51:05 +000013#include <set>
14
15namespace facebook { namespace thrift { namespace concurrency {
16
Mark Slee5ea15f92007-03-05 22:55:59 +000017using boost::shared_ptr;
18
Marc Slemko6f038a72006-08-03 18:58:09 +000019typedef std::multimap<long long, shared_ptr<TimerManager::Task> >::iterator task_iterator;
Marc Slemko0e53ccd2006-07-17 23:51:05 +000020typedef std::pair<task_iterator, task_iterator> task_range;
21
Mark Sleef5f2be42006-09-05 21:05:31 +000022/**
23 * TimerManager class
24 *
25 * @author marc
26 * @version $Id:$
27 */
Marc Slemko0e53ccd2006-07-17 23:51:05 +000028class TimerManager::Task : public Runnable {
29
Mark Sleef5f2be42006-09-05 21:05:31 +000030 public:
Marc Slemko0e53ccd2006-07-17 23:51:05 +000031 enum STATE {
32 WAITING,
33 EXECUTING,
34 CANCELLED,
35 COMPLETE
36 };
37
Marc Slemko6f038a72006-08-03 18:58:09 +000038 Task(shared_ptr<Runnable> runnable) :
Mark Slee2f6404d2006-10-10 01:37:40 +000039 runnable_(runnable),
40 state_(WAITING) {}
Marc Slemko0e53ccd2006-07-17 23:51:05 +000041
Marc Slemko6f038a72006-08-03 18:58:09 +000042 ~Task() {
Mark Sleef5f2be42006-09-05 21:05:31 +000043 //debug
44 std::cerr << "TimerManager::Task.dtor[" << this << "]" << std::endl;
45 }
Marc Slemko0e53ccd2006-07-17 23:51:05 +000046
47 void run() {
Mark Slee2f6404d2006-10-10 01:37:40 +000048 if (state_ == EXECUTING) {
49 runnable_->run();
50 state_ = COMPLETE;
Marc Slemko0e53ccd2006-07-17 23:51:05 +000051 }
52 }
53
54 private:
Mark Slee2f6404d2006-10-10 01:37:40 +000055 shared_ptr<Runnable> runnable_;
Marc Slemko8a40a762006-07-19 17:46:50 +000056 class TimerManager::Dispatcher;
Marc Slemko8a40a762006-07-19 17:46:50 +000057 friend class TimerManager::Dispatcher;
Mark Slee2f6404d2006-10-10 01:37:40 +000058 STATE state_;
Marc Slemko0e53ccd2006-07-17 23:51:05 +000059};
60
61class TimerManager::Dispatcher: public Runnable {
62
Mark Sleef5f2be42006-09-05 21:05:31 +000063 public:
Marc Slemko0e53ccd2006-07-17 23:51:05 +000064 Dispatcher(TimerManager* manager) :
Mark Slee2f6404d2006-10-10 01:37:40 +000065 manager_(manager) {}
Marc Slemko0e53ccd2006-07-17 23:51:05 +000066
Marc Slemko6f038a72006-08-03 18:58:09 +000067 ~Dispatcher() {
Mark Sleef5f2be42006-09-05 21:05:31 +000068 // debug
69 std::cerr << "Dispatcher::dtor[" << this << "]" << std::endl;
Marc Slemko6f038a72006-08-03 18:58:09 +000070 }
Marc Slemko0e53ccd2006-07-17 23:51:05 +000071
Mark Sleef5f2be42006-09-05 21:05:31 +000072 /**
73 * Dispatcher entry point
74 *
Mark Slee2f6404d2006-10-10 01:37:40 +000075 * As long as dispatcher thread is running, pull tasks off the task taskMap_
Mark Sleef5f2be42006-09-05 21:05:31 +000076 * and execute.
77 */
Marc Slemko0e53ccd2006-07-17 23:51:05 +000078 void run() {
Mark Sleef5f2be42006-09-05 21:05:31 +000079 {
Mark Slee2f6404d2006-10-10 01:37:40 +000080 Synchronized s(manager_->monitor_);
81 if (manager_->state_ == TimerManager::STARTING) {
82 manager_->state_ = TimerManager::STARTED;
83 manager_->monitor_.notifyAll();
Marc Slemko0e53ccd2006-07-17 23:51:05 +000084 }
85 }
86
87 do {
Marc Slemko6f038a72006-08-03 18:58:09 +000088 std::set<shared_ptr<TimerManager::Task> > expiredTasks;
Mark Sleef5f2be42006-09-05 21:05:31 +000089 {
Mark Slee2f6404d2006-10-10 01:37:40 +000090 Synchronized s(manager_->monitor_);
Marc Slemko0e53ccd2006-07-17 23:51:05 +000091 task_iterator expiredTaskEnd;
Marc Slemko9f27a4e2006-07-19 20:02:22 +000092 long long now = Util::currentTime();
Mark Slee2f6404d2006-10-10 01:37:40 +000093 while (manager_->state_ == TimerManager::STARTED &&
94 (expiredTaskEnd = manager_->taskMap_.upper_bound(now)) == manager_->taskMap_.begin()) {
Marc Slemko8a40a762006-07-19 17:46:50 +000095 long long timeout = 0LL;
Mark Slee2f6404d2006-10-10 01:37:40 +000096 if (!manager_->taskMap_.empty()) {
97 timeout = manager_->taskMap_.begin()->first - now;
Marc Slemko8a40a762006-07-19 17:46:50 +000098 }
Aditya Agarwal3f234da2007-04-01 01:19:57 +000099 assert((timeout != 0 && manager_->taskCount_ > 0) || (timeout == 0 && manager_->taskCount_ == 0));
Mark Slee2f6404d2006-10-10 01:37:40 +0000100 manager_->monitor_.wait(timeout);
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000101 now = Util::currentTime();
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000102 }
103
Mark Slee2f6404d2006-10-10 01:37:40 +0000104 if (manager_->state_ == TimerManager::STARTED) {
105 for (task_iterator ix = manager_->taskMap_.begin(); ix != expiredTaskEnd; ix++) {
Marc Slemko6f038a72006-08-03 18:58:09 +0000106 shared_ptr<TimerManager::Task> task = ix->second;
Mark Sleef5f2be42006-09-05 21:05:31 +0000107 expiredTasks.insert(task);
Mark Slee2f6404d2006-10-10 01:37:40 +0000108 if (task->state_ == TimerManager::Task::WAITING) {
109 task->state_ = TimerManager::Task::EXECUTING;
Marc Slemko8a40a762006-07-19 17:46:50 +0000110 }
Mark Slee2f6404d2006-10-10 01:37:40 +0000111 manager_->taskCount_--;
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000112 }
Mark Slee2f6404d2006-10-10 01:37:40 +0000113 manager_->taskMap_.erase(manager_->taskMap_.begin(), expiredTaskEnd);
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000114 }
115 }
116
Mark Sleef5f2be42006-09-05 21:05:31 +0000117 for (std::set<shared_ptr<Task> >::iterator ix = expiredTasks.begin(); ix != expiredTasks.end(); ix++) {
118 (*ix)->run();
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000119 }
120
Mark Slee2f6404d2006-10-10 01:37:40 +0000121 } while (manager_->state_ == TimerManager::STARTED);
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000122
Mark Sleef5f2be42006-09-05 21:05:31 +0000123 {
Mark Slee2f6404d2006-10-10 01:37:40 +0000124 Synchronized s(manager_->monitor_);
125 if (manager_->state_ == TimerManager::STOPPING) {
126 manager_->state_ = TimerManager::STOPPED;
127 manager_->monitor_.notify();
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000128 }
129 }
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000130 return;
131 }
132
133 private:
Mark Slee2f6404d2006-10-10 01:37:40 +0000134 TimerManager* manager_;
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000135 friend class TimerManager;
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000136};
137
Marc Slemko8a40a762006-07-19 17:46:50 +0000138TimerManager::TimerManager() :
Mark Slee2f6404d2006-10-10 01:37:40 +0000139 taskCount_(0),
140 state_(TimerManager::UNINITIALIZED),
141 dispatcher_(shared_ptr<Dispatcher>(new Dispatcher(this))) {
Marc Slemko8a40a762006-07-19 17:46:50 +0000142}
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000143
Marc Slemko8a40a762006-07-19 17:46:50 +0000144
145TimerManager::~TimerManager() {
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000146
Mark Sleef5f2be42006-09-05 21:05:31 +0000147 // If we haven't been explicitly stopped, do so now. We don't need to grab
148 // the monitor here, since stop already takes care of reentrancy.
Marc Slemko6f038a72006-08-03 18:58:09 +0000149 std::cerr << "TimerManager::dtor[" << this << "]" << std::endl;
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000150
Mark Slee2f6404d2006-10-10 01:37:40 +0000151 if (state_ != STOPPED) {
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000152 try {
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000153 stop();
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000154 } catch(...) {
Marc Slemko6f038a72006-08-03 18:58:09 +0000155 std::cerr << "TimerManager::dtor[" << this << "] uhoh " << std::endl;
156 throw;
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000157 // uhoh
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000158 }
159 }
Marc Slemko8a40a762006-07-19 17:46:50 +0000160}
161
162void TimerManager::start() {
Marc Slemko8a40a762006-07-19 17:46:50 +0000163 bool doStart = false;
Mark Sleef5f2be42006-09-05 21:05:31 +0000164 {
Mark Slee2f6404d2006-10-10 01:37:40 +0000165 Synchronized s(monitor_);
166 if (threadFactory_ == NULL) {
Mark Sleef5f2be42006-09-05 21:05:31 +0000167 throw InvalidArgumentException();
168 }
Mark Slee2f6404d2006-10-10 01:37:40 +0000169 if (state_ == TimerManager::UNINITIALIZED) {
170 state_ = TimerManager::STARTING;
Marc Slemko8a40a762006-07-19 17:46:50 +0000171 doStart = true;
172 }
173 }
174
Mark Sleef5f2be42006-09-05 21:05:31 +0000175 if (doStart) {
Mark Slee2f6404d2006-10-10 01:37:40 +0000176 dispatcherThread_ = threadFactory_->newThread(dispatcher_);
177 dispatcherThread_->start();
Marc Slemko8a40a762006-07-19 17:46:50 +0000178 }
179
Mark Sleef5f2be42006-09-05 21:05:31 +0000180 {
Mark Slee2f6404d2006-10-10 01:37:40 +0000181 Synchronized s(monitor_);
182 while (state_ == TimerManager::STARTING) {
183 monitor_.wait();
Marc Slemko8a40a762006-07-19 17:46:50 +0000184 }
Aditya Agarwal3f234da2007-04-01 01:19:57 +0000185 assert(state_ != TimerManager::STARTING);
Marc Slemko8a40a762006-07-19 17:46:50 +0000186 }
187}
188
189void TimerManager::stop() {
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000190 bool doStop = false;
Mark Sleef5f2be42006-09-05 21:05:31 +0000191 {
Mark Slee2f6404d2006-10-10 01:37:40 +0000192 Synchronized s(monitor_);
193 if (state_ == TimerManager::UNINITIALIZED) {
194 state_ = TimerManager::STOPPED;
195 } else if (state_ != STOPPING && state_ != STOPPED) {
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000196 doStop = true;
Mark Slee2f6404d2006-10-10 01:37:40 +0000197 state_ = STOPPING;
198 monitor_.notifyAll();
Marc Slemko8a40a762006-07-19 17:46:50 +0000199 }
Mark Slee2f6404d2006-10-10 01:37:40 +0000200 while (state_ != STOPPED) {
201 monitor_.wait();
Marc Slemko8a40a762006-07-19 17:46:50 +0000202 }
203 }
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000204
Mark Sleef5f2be42006-09-05 21:05:31 +0000205 if (doStop) {
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000206 // Clean up any outstanding tasks
Mark Slee2f6404d2006-10-10 01:37:40 +0000207 for (task_iterator ix = taskMap_.begin(); ix != taskMap_.end(); ix++) {
208 taskMap_.erase(ix);
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000209 }
210
Marc Slemko6f038a72006-08-03 18:58:09 +0000211 // Remove dispatcher's reference to us.
Mark Slee2f6404d2006-10-10 01:37:40 +0000212 dispatcher_->manager_ = NULL;
Marc Slemko9f27a4e2006-07-19 20:02:22 +0000213 }
Marc Slemko8a40a762006-07-19 17:46:50 +0000214}
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000215
Marc Slemko6f038a72006-08-03 18:58:09 +0000216shared_ptr<const ThreadFactory> TimerManager::threadFactory() const {
Mark Slee2f6404d2006-10-10 01:37:40 +0000217 Synchronized s(monitor_);
218 return threadFactory_;
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000219}
220
Marc Slemko6f038a72006-08-03 18:58:09 +0000221void TimerManager::threadFactory(shared_ptr<const ThreadFactory> value) {
Mark Slee2f6404d2006-10-10 01:37:40 +0000222 Synchronized s(monitor_);
223 threadFactory_ = value;
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000224}
225
Marc Slemko8a40a762006-07-19 17:46:50 +0000226size_t TimerManager::taskCount() const {
Mark Slee2f6404d2006-10-10 01:37:40 +0000227 return taskCount_;
Marc Slemko8a40a762006-07-19 17:46:50 +0000228}
229
Marc Slemko6f038a72006-08-03 18:58:09 +0000230void TimerManager::add(shared_ptr<Runnable> task, long long timeout) {
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000231 long long now = Util::currentTime();
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000232 timeout += now;
233
Mark Sleef5f2be42006-09-05 21:05:31 +0000234 {
Mark Slee2f6404d2006-10-10 01:37:40 +0000235 Synchronized s(monitor_);
236 if (state_ != TimerManager::STARTED) {
Marc Slemko8a40a762006-07-19 17:46:50 +0000237 throw IllegalStateException();
238 }
239
Mark Slee2f6404d2006-10-10 01:37:40 +0000240 taskCount_++;
241 taskMap_.insert(std::pair<long long, shared_ptr<Task> >(timeout, shared_ptr<Task>(new Task(task))));
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000242
Mark Sleef5f2be42006-09-05 21:05:31 +0000243 // If the task map was empty, or if we have an expiration that is earlier
244 // than any previously seen, kick the dispatcher so it can update its
245 // timeout
Mark Slee2f6404d2006-10-10 01:37:40 +0000246 if (taskCount_ == 1 || timeout < taskMap_.begin()->first) {
247 monitor_.notify();
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000248 }
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000249 }
250}
251
Marc Slemko6f038a72006-08-03 18:58:09 +0000252void TimerManager::add(shared_ptr<Runnable> task, const struct timespec& value) {
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000253
Mark Sleef5f2be42006-09-05 21:05:31 +0000254 long long expiration;
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000255 Util::toMilliseconds(expiration, value);
256
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000257 long long now = Util::currentTime();
258
Mark Sleef5f2be42006-09-05 21:05:31 +0000259 if (expiration < now) {
Marc Slemko8a40a762006-07-19 17:46:50 +0000260 throw InvalidArgumentException();
261 }
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000262
263 add(task, expiration - now);
264}
265
266
Marc Slemko6f038a72006-08-03 18:58:09 +0000267void TimerManager::remove(shared_ptr<Runnable> task) {
Mark Slee2f6404d2006-10-10 01:37:40 +0000268 Synchronized s(monitor_);
269 if (state_ != TimerManager::STARTED) {
Mark Sleef5f2be42006-09-05 21:05:31 +0000270 throw IllegalStateException();
Marc Slemko8a40a762006-07-19 17:46:50 +0000271 }
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000272}
273
Mark Slee2f6404d2006-10-10 01:37:40 +0000274const TimerManager::STATE TimerManager::state() const { return state_; }
Marc Slemko8a40a762006-07-19 17:46:50 +0000275
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000276}}} // facebook::thrift::concurrency
277