| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 part of dart.io; | 5 part of dart.io; |
| 6 | 6 |
| 7 // Timer heap implemented as a array-based binary heap[0]. | 7 // Timer heap implemented as a array-based binary heap[0]. |
| 8 // This allows for O(1) `first`, O(log(n)) `remove`/`removeFirst` and O(log(n)) | 8 // This allows for O(1) `first`, O(log(n)) `remove`/`removeFirst` and O(log(n)) |
| 9 // `add`. | 9 // `add`. |
| 10 // | 10 // |
| (...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 183 bool get _repeating => _milliSeconds >= 0; | 183 bool get _repeating => _milliSeconds >= 0; |
| 184 | 184 |
| 185 bool get isActive => _callback != null; | 185 bool get isActive => _callback != null; |
| 186 | 186 |
| 187 // Cancels a set timer. The timer is removed from the timer list and if | 187 // Cancels a set timer. The timer is removed from the timer list and if |
| 188 // the given timer is the earliest timer the native timer is reset. | 188 // the given timer is the earliest timer the native timer is reset. |
| 189 void cancel() { | 189 void cancel() { |
| 190 _clear(); | 190 _clear(); |
| 191 if (!_isInHeap) return; | 191 if (!_isInHeap) return; |
| 192 assert(_wakeupTime != 0); | 192 assert(_wakeupTime != 0); |
| 193 bool update = _firstZeroTimer == null && _heap.isFirst(this); | 193 bool update = (_firstZeroTimer == null) && _heap.isFirst(this); |
| 194 _heap.remove(this); | 194 _heap.remove(this); |
| 195 if (update) { | 195 if (update) { |
| 196 _notifyEventHandler(); | 196 _notifyEventHandler(); |
| 197 } | 197 } |
| 198 } | 198 } |
| 199 | 199 |
| 200 void _advanceWakeupTime() { | 200 void _advanceWakeupTime() { |
| 201 assert(_milliSeconds >= 0); | 201 assert(_milliSeconds >= 0); |
| 202 _wakeupTime += _milliSeconds; | 202 _wakeupTime += _milliSeconds; |
| 203 } | 203 } |
| 204 | 204 |
| 205 // Adds a timer to the timer list. Timers with the same wakeup time are | 205 // Adds a timer to the timer list. Timers with the same wakeup time are |
| 206 // enqueued in order and notified in FIFO order. | 206 // enqueued in order and notified in FIFO order. |
| 207 bool _addTimerToHeap() { | 207 bool _addTimerToHeap() { |
| 208 if (_wakeupTime == 0) { | 208 if (_wakeupTime == 0) { |
| 209 if (_firstZeroTimer == null) { | 209 if (_firstZeroTimer == null) { |
| 210 _lastZeroTimer = _firstZeroTimer = this; | 210 _lastZeroTimer = this; |
| 211 _firstZeroTimer = this; |
| 211 return true; | 212 return true; |
| 212 } else { | 213 } else { |
| 213 _lastZeroTimer = _lastZeroTimer._indexOrNext = this; | 214 _lastZeroTimer._indexOrNext = this; |
| 215 _lastZeroTimer = this; |
| 214 return false; | 216 return false; |
| 215 } | 217 } |
| 216 } else { | 218 } else { |
| 217 _id = _idCount++; | 219 _id = _idCount++; |
| 218 _heap.add(this); | 220 _heap.add(this); |
| 219 return _firstZeroTimer == null && _heap.isFirst(this); | 221 return _firstZeroTimer == null && _heap.isFirst(this); |
| 220 } | 222 } |
| 221 } | 223 } |
| 222 | 224 |
| 223 | 225 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 250 _heap.first._wakeupTime); | 252 _heap.first._wakeupTime); |
| 251 } | 253 } |
| 252 } | 254 } |
| 253 } | 255 } |
| 254 | 256 |
| 255 static void _handleTimeout(_) { | 257 static void _handleTimeout(_) { |
| 256 int currentTime = new DateTime.now().millisecondsSinceEpoch; | 258 int currentTime = new DateTime.now().millisecondsSinceEpoch; |
| 257 // Collect all pending timers. | 259 // Collect all pending timers. |
| 258 var timer = _firstZeroTimer; | 260 var timer = _firstZeroTimer; |
| 259 var nextTimer = _lastZeroTimer; | 261 var nextTimer = _lastZeroTimer; |
| 260 _firstZeroTimer = _lastZeroTimer = null; | 262 _firstZeroTimer = null; |
| 263 _lastZeroTimer = null; |
| 261 while (_heap.isNotEmpty && _heap.first._wakeupTime <= currentTime) { | 264 while (_heap.isNotEmpty && _heap.first._wakeupTime <= currentTime) { |
| 262 var next = _heap.removeFirst(); | 265 var next = _heap.removeFirst(); |
| 263 if (timer == null) { | 266 if (timer == null) { |
| 264 nextTimer = timer = next; | 267 nextTimer = next; |
| 268 timer = next; |
| 265 } else { | 269 } else { |
| 266 nextTimer = nextTimer._indexOrNext = next; | 270 nextTimer._indexOrNext = next; |
| 271 nextTimer = next; |
| 267 } | 272 } |
| 268 } | 273 } |
| 269 | 274 |
| 270 // Trigger all of the pending timers. New timers added as part of the | 275 // Trigger all of the pending timers. New timers added as part of the |
| 271 // callbacks will be enqueued now and notified in the next spin at the | 276 // callbacks will be enqueued now and notified in the next spin at the |
| 272 // earliest. | 277 // earliest. |
| 273 _handlingCallbacks = true; | 278 _handlingCallbacks = true; |
| 274 try { | 279 try { |
| 275 while (timer != null) { | 280 while (timer != null) { |
| 276 var next = timer._indexOrNext; | 281 var next = timer._indexOrNext; |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 319 _getTimerFactoryClosure() { | 324 _getTimerFactoryClosure() { |
| 320 return (int milliSeconds, void callback(Timer timer), bool repeating) { | 325 return (int milliSeconds, void callback(Timer timer), bool repeating) { |
| 321 if (repeating) { | 326 if (repeating) { |
| 322 return new _Timer.periodic(milliSeconds, callback); | 327 return new _Timer.periodic(milliSeconds, callback); |
| 323 } | 328 } |
| 324 return new _Timer(milliSeconds, callback); | 329 return new _Timer(milliSeconds, callback); |
| 325 }; | 330 }; |
| 326 } | 331 } |
| 327 | 332 |
| 328 | 333 |
| OLD | NEW |