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