Mark Slee | 9f0c651 | 2007-02-28 23:58:26 +0000 | [diff] [blame^] | 1 | // 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 Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 7 | #include "TimerManager.h" |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 8 | #include "Exception.h" |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 9 | #include "Util.h" |
| 10 | |
| 11 | #include <assert.h> |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 12 | #include <iostream> |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 13 | #include <set> |
| 14 | |
| 15 | namespace facebook { namespace thrift { namespace concurrency { |
| 16 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 17 | typedef std::multimap<long long, shared_ptr<TimerManager::Task> >::iterator task_iterator; |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 18 | typedef std::pair<task_iterator, task_iterator> task_range; |
| 19 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 20 | /** |
| 21 | * TimerManager class |
| 22 | * |
| 23 | * @author marc |
| 24 | * @version $Id:$ |
| 25 | */ |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 26 | class TimerManager::Task : public Runnable { |
| 27 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 28 | public: |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 29 | enum STATE { |
| 30 | WAITING, |
| 31 | EXECUTING, |
| 32 | CANCELLED, |
| 33 | COMPLETE |
| 34 | }; |
| 35 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 36 | Task(shared_ptr<Runnable> runnable) : |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 37 | runnable_(runnable), |
| 38 | state_(WAITING) {} |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 39 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 40 | ~Task() { |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 41 | //debug |
| 42 | std::cerr << "TimerManager::Task.dtor[" << this << "]" << std::endl; |
| 43 | } |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 44 | |
| 45 | void run() { |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 46 | if (state_ == EXECUTING) { |
| 47 | runnable_->run(); |
| 48 | state_ = COMPLETE; |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 49 | } |
| 50 | } |
| 51 | |
| 52 | private: |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 53 | shared_ptr<Runnable> runnable_; |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 54 | class TimerManager::Dispatcher; |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 55 | friend class TimerManager::Dispatcher; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 56 | STATE state_; |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 57 | }; |
| 58 | |
| 59 | class TimerManager::Dispatcher: public Runnable { |
| 60 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 61 | public: |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 62 | Dispatcher(TimerManager* manager) : |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 63 | manager_(manager) {} |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 64 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 65 | ~Dispatcher() { |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 66 | // debug |
| 67 | std::cerr << "Dispatcher::dtor[" << this << "]" << std::endl; |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 68 | } |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 69 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 70 | /** |
| 71 | * Dispatcher entry point |
| 72 | * |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 73 | * As long as dispatcher thread is running, pull tasks off the task taskMap_ |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 74 | * and execute. |
| 75 | */ |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 76 | void run() { |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 77 | { |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 78 | Synchronized s(manager_->monitor_); |
| 79 | if (manager_->state_ == TimerManager::STARTING) { |
| 80 | manager_->state_ = TimerManager::STARTED; |
| 81 | manager_->monitor_.notifyAll(); |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 82 | } |
| 83 | } |
| 84 | |
| 85 | do { |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 86 | std::set<shared_ptr<TimerManager::Task> > expiredTasks; |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 87 | { |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 88 | Synchronized s(manager_->monitor_); |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 89 | task_iterator expiredTaskEnd; |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 90 | long long now = Util::currentTime(); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 91 | while (manager_->state_ == TimerManager::STARTED && |
| 92 | (expiredTaskEnd = manager_->taskMap_.upper_bound(now)) == manager_->taskMap_.begin()) { |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 93 | long long timeout = 0LL; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 94 | if (!manager_->taskMap_.empty()) { |
| 95 | timeout = manager_->taskMap_.begin()->first - now; |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 96 | } |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 97 | assert((timeout != 0 && manager_->taskCount_ > 0) || (timeout == 0 && manager_->taskCount_ == 0)); |
| 98 | manager_->monitor_.wait(timeout); |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 99 | now = Util::currentTime(); |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 102 | if (manager_->state_ == TimerManager::STARTED) { |
| 103 | for (task_iterator ix = manager_->taskMap_.begin(); ix != expiredTaskEnd; ix++) { |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 104 | shared_ptr<TimerManager::Task> task = ix->second; |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 105 | expiredTasks.insert(task); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 106 | if (task->state_ == TimerManager::Task::WAITING) { |
| 107 | task->state_ = TimerManager::Task::EXECUTING; |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 108 | } |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 109 | manager_->taskCount_--; |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 110 | } |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 111 | manager_->taskMap_.erase(manager_->taskMap_.begin(), expiredTaskEnd); |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 115 | for (std::set<shared_ptr<Task> >::iterator ix = expiredTasks.begin(); ix != expiredTasks.end(); ix++) { |
| 116 | (*ix)->run(); |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 119 | } while (manager_->state_ == TimerManager::STARTED); |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 120 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 121 | { |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 122 | Synchronized s(manager_->monitor_); |
| 123 | if (manager_->state_ == TimerManager::STOPPING) { |
| 124 | manager_->state_ = TimerManager::STOPPED; |
| 125 | manager_->monitor_.notify(); |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 126 | } |
| 127 | } |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 128 | return; |
| 129 | } |
| 130 | |
| 131 | private: |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 132 | TimerManager* manager_; |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 133 | friend class TimerManager; |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 134 | }; |
| 135 | |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 136 | TimerManager::TimerManager() : |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 137 | taskCount_(0), |
| 138 | state_(TimerManager::UNINITIALIZED), |
| 139 | dispatcher_(shared_ptr<Dispatcher>(new Dispatcher(this))) { |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 140 | } |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 141 | |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 142 | |
| 143 | TimerManager::~TimerManager() { |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 144 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 145 | // If we haven't been explicitly stopped, do so now. We don't need to grab |
| 146 | // the monitor here, since stop already takes care of reentrancy. |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 147 | std::cerr << "TimerManager::dtor[" << this << "]" << std::endl; |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 148 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 149 | if (state_ != STOPPED) { |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 150 | try { |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 151 | stop(); |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 152 | } catch(...) { |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 153 | std::cerr << "TimerManager::dtor[" << this << "] uhoh " << std::endl; |
| 154 | throw; |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 155 | // uhoh |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 156 | } |
| 157 | } |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | void TimerManager::start() { |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 161 | bool doStart = false; |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 162 | { |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 163 | Synchronized s(monitor_); |
| 164 | if (threadFactory_ == NULL) { |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 165 | throw InvalidArgumentException(); |
| 166 | } |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 167 | if (state_ == TimerManager::UNINITIALIZED) { |
| 168 | state_ = TimerManager::STARTING; |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 169 | doStart = true; |
| 170 | } |
| 171 | } |
| 172 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 173 | if (doStart) { |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 174 | dispatcherThread_ = threadFactory_->newThread(dispatcher_); |
| 175 | dispatcherThread_->start(); |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 176 | } |
| 177 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 178 | { |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 179 | Synchronized s(monitor_); |
| 180 | while (state_ == TimerManager::STARTING) { |
| 181 | monitor_.wait(); |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 182 | } |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 183 | assert(state_ != TimerManager::STARTING); |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | |
| 187 | void TimerManager::stop() { |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 188 | bool doStop = false; |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 189 | { |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 190 | Synchronized s(monitor_); |
| 191 | if (state_ == TimerManager::UNINITIALIZED) { |
| 192 | state_ = TimerManager::STOPPED; |
| 193 | } else if (state_ != STOPPING && state_ != STOPPED) { |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 194 | doStop = true; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 195 | state_ = STOPPING; |
| 196 | monitor_.notifyAll(); |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 197 | } |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 198 | while (state_ != STOPPED) { |
| 199 | monitor_.wait(); |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 200 | } |
| 201 | } |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 202 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 203 | if (doStop) { |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 204 | // Clean up any outstanding tasks |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 205 | for (task_iterator ix = taskMap_.begin(); ix != taskMap_.end(); ix++) { |
| 206 | taskMap_.erase(ix); |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 209 | // Remove dispatcher's reference to us. |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 210 | dispatcher_->manager_ = NULL; |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 211 | } |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 212 | } |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 213 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 214 | shared_ptr<const ThreadFactory> TimerManager::threadFactory() const { |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 215 | Synchronized s(monitor_); |
| 216 | return threadFactory_; |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 219 | void TimerManager::threadFactory(shared_ptr<const ThreadFactory> value) { |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 220 | Synchronized s(monitor_); |
| 221 | threadFactory_ = value; |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 224 | size_t TimerManager::taskCount() const { |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 225 | return taskCount_; |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 226 | } |
| 227 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 228 | void TimerManager::add(shared_ptr<Runnable> task, long long timeout) { |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 229 | long long now = Util::currentTime(); |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 230 | timeout += now; |
| 231 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 232 | { |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 233 | Synchronized s(monitor_); |
| 234 | if (state_ != TimerManager::STARTED) { |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 235 | throw IllegalStateException(); |
| 236 | } |
| 237 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 238 | taskCount_++; |
| 239 | taskMap_.insert(std::pair<long long, shared_ptr<Task> >(timeout, shared_ptr<Task>(new Task(task)))); |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 240 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 241 | // If the task map was empty, or if we have an expiration that is earlier |
| 242 | // than any previously seen, kick the dispatcher so it can update its |
| 243 | // timeout |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 244 | if (taskCount_ == 1 || timeout < taskMap_.begin()->first) { |
| 245 | monitor_.notify(); |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 246 | } |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 247 | } |
| 248 | } |
| 249 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 250 | void TimerManager::add(shared_ptr<Runnable> task, const struct timespec& value) { |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 251 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 252 | long long expiration; |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 253 | Util::toMilliseconds(expiration, value); |
| 254 | |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 255 | long long now = Util::currentTime(); |
| 256 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 257 | if (expiration < now) { |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 258 | throw InvalidArgumentException(); |
| 259 | } |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 260 | |
| 261 | add(task, expiration - now); |
| 262 | } |
| 263 | |
| 264 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 265 | void TimerManager::remove(shared_ptr<Runnable> task) { |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 266 | Synchronized s(monitor_); |
| 267 | if (state_ != TimerManager::STARTED) { |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 268 | throw IllegalStateException(); |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 269 | } |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 270 | } |
| 271 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 272 | const TimerManager::STATE TimerManager::state() const { return state_; } |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 273 | |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 274 | }}} // facebook::thrift::concurrency |
| 275 | |