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