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 | |
| 85 | /* Update next timeout if necessary */ |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 86 | |
| 87 | task_iterator expiredTaskEnd; |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame^] | 88 | |
| 89 | while(_manager->_state == TimerManager::STARTED && |
| 90 | (expiredTaskEnd = _manager->_taskMap.upper_bound(Util::currentTime())) == _manager->_taskMap.begin()) { |
| 91 | |
| 92 | long long timeout = 0LL; |
| 93 | |
| 94 | if(!_manager->_taskMap.empty()) { |
| 95 | |
| 96 | timeout = Util::currentTime() - _manager->_taskMap.begin()->first; |
| 97 | } |
| 98 | |
| 99 | _manager->_monitor.wait(timeout); |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 100 | |
| 101 | } |
| 102 | |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame^] | 103 | if(_manager->_state == TimerManager::STARTED) { |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 104 | |
| 105 | for(task_iterator ix = _manager->_taskMap.begin(); ix != expiredTaskEnd; ix++) { |
| 106 | |
| 107 | TimerManager::Task* task = ix->second; |
| 108 | |
| 109 | expiredTasks.insert(task); |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame^] | 110 | |
| 111 | if(task->_state == TimerManager::Task::WAITING) { |
| 112 | |
| 113 | task->_state = TimerManager::Task::EXECUTING; |
| 114 | } |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 115 | |
| 116 | _manager->_taskCount--; |
| 117 | } |
| 118 | |
| 119 | _manager->_taskMap.erase(_manager->_taskMap.begin(), expiredTaskEnd); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | for(std::set<Task*>::iterator ix = expiredTasks.begin(); ix != expiredTasks.end(); ix++) { |
| 124 | |
| 125 | (*ix)->run(); |
| 126 | |
| 127 | delete *ix; |
| 128 | } |
| 129 | |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame^] | 130 | } while(_manager->_state == TimerManager::STARTED); |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 131 | |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame^] | 132 | {Synchronized s(_manager->_monitor); |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 133 | |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame^] | 134 | if(_manager->_state == TimerManager::STOPPING) { |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 135 | |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame^] | 136 | _manager->_state = TimerManager::STOPPED; |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 137 | |
| 138 | _manager->_monitor.notify(); |
| 139 | |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | return; |
| 144 | } |
| 145 | |
| 146 | private: |
| 147 | |
| 148 | TimerManager* _manager; |
| 149 | |
| 150 | friend class TimerManager; |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 151 | }; |
| 152 | |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame^] | 153 | TimerManager::TimerManager() : |
| 154 | _state(TimerManager::UNINITIALIZED), |
| 155 | _dispatcher(new Dispatcher(this)) { |
| 156 | } |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 157 | |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame^] | 158 | |
| 159 | TimerManager::~TimerManager() { |
| 160 | delete _dispatcher; |
| 161 | } |
| 162 | |
| 163 | void TimerManager::start() { |
| 164 | |
| 165 | bool doStart = false; |
| 166 | |
| 167 | {Synchronized s(_monitor); |
| 168 | |
| 169 | if(_threadFactory == NULL) {throw InvalidArgumentException();} |
| 170 | |
| 171 | if(_state == TimerManager::UNINITIALIZED) { |
| 172 | |
| 173 | _state = TimerManager::STARTING; |
| 174 | |
| 175 | doStart = true; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | if(doStart) { |
| 180 | |
| 181 | _dispatcherThread = _threadFactory->newThread(_dispatcher); |
| 182 | |
| 183 | _dispatcherThread->start(); |
| 184 | } |
| 185 | |
| 186 | {Synchronized s(_monitor); |
| 187 | |
| 188 | while(_state == TimerManager::STARTING) { |
| 189 | |
| 190 | _monitor.wait(); |
| 191 | } |
| 192 | |
| 193 | assert(_state != TimerManager::STARTING); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | void TimerManager::stop() { |
| 198 | |
| 199 | {Synchronized s(_monitor); |
| 200 | |
| 201 | if(_state == TimerManager::UNINITIALIZED) { |
| 202 | |
| 203 | _state = TimerManager::STOPPED; |
| 204 | |
| 205 | } else if(_state != STOPPING && _state != STOPPED) { |
| 206 | |
| 207 | _state = STOPPING; |
| 208 | |
| 209 | _monitor.notifyAll(); |
| 210 | } |
| 211 | |
| 212 | while(_state != STOPPED) { |
| 213 | |
| 214 | _monitor.wait(); |
| 215 | } |
| 216 | } |
| 217 | } |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 218 | |
| 219 | const ThreadFactory* TimerManager::threadFactory() const { |
| 220 | |
| 221 | Synchronized s(_monitor); |
| 222 | |
| 223 | return _threadFactory; |
| 224 | } |
| 225 | |
| 226 | void TimerManager::threadFactory(const ThreadFactory* value) { |
| 227 | |
| 228 | Synchronized s(_monitor); |
| 229 | |
| 230 | _threadFactory = value; |
| 231 | } |
| 232 | |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame^] | 233 | size_t TimerManager::taskCount() const { |
| 234 | |
| 235 | return _taskCount; |
| 236 | } |
| 237 | |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 238 | void TimerManager::add(Runnable* task, long long timeout) { |
| 239 | |
| 240 | long long now = Util::currentTime(); |
| 241 | |
| 242 | timeout += now; |
| 243 | |
| 244 | {Synchronized s(_monitor); |
| 245 | |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame^] | 246 | if(_state != TimerManager::STARTED) { |
| 247 | throw IllegalStateException(); |
| 248 | } |
| 249 | |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 250 | _taskCount++; |
| 251 | |
| 252 | _taskMap.insert(std::pair<long long, Task*>(timeout, new Task(task))); |
| 253 | |
| 254 | /* If the task map was empty, or if we have an expiration that is earlier than any previously seen, |
| 255 | kick the dispatcher so it can update its timeout */ |
| 256 | |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame^] | 257 | if(_taskCount == 1 || timeout < _taskMap.begin()->first) { |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 258 | |
| 259 | _monitor.notify(); |
| 260 | } |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 261 | } |
| 262 | } |
| 263 | |
| 264 | void TimerManager::add(Runnable* task, const struct timespec& value) { |
| 265 | |
| 266 | long long expiration; |
| 267 | |
| 268 | Util::toMilliseconds(expiration, value); |
| 269 | |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 270 | long long now = Util::currentTime(); |
| 271 | |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame^] | 272 | if(expiration < now) { |
| 273 | throw InvalidArgumentException(); |
| 274 | } |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 275 | |
| 276 | add(task, expiration - now); |
| 277 | } |
| 278 | |
| 279 | |
| 280 | void TimerManager::remove(Runnable* task) { |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame^] | 281 | {Synchronized s(_monitor); |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 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 | } |
| 288 | |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame^] | 289 | const TimerManager::STATE TimerManager::state() const { return _state;} |
| 290 | |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 291 | }}} // facebook::thrift::concurrency |
| 292 | |