| 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; | |
| 6 | |
| 7 // Timer heap implemented as a array-based binary heap[0]. | 5 // 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)) | 6 // This allows for O(1) `first`, O(log(n)) `remove`/`removeFirst` and O(log(n)) |
| 9 // `add`. | 7 // `add`. |
| 10 // | 8 // |
| 11 // To ensure the timers are ordered by insertion time, the _Timer class has a | 9 // To ensure the timers are ordered by insertion time, the _Timer class has a |
| 12 // `_id` field set when added to the heap. | 10 // `_id` field set when added to the heap. |
| 13 // | 11 // |
| 14 // [0] http://en.wikipedia.org/wiki/Binary_heap | 12 // [0] http://en.wikipedia.org/wiki/Binary_heap |
| 15 class _TimerHeap { | 13 class _TimerHeap { |
| 16 List<_Timer> _list; | 14 List<_Timer> _list; |
| (...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 256 // While we are already handling callbacks we will not notify the event | 254 // While we are already handling callbacks we will not notify the event |
| 257 // handler. _handleTimeout will call _notifyEventHandler once all pending | 255 // handler. _handleTimeout will call _notifyEventHandler once all pending |
| 258 // timers are processed. | 256 // timers are processed. |
| 259 return; | 257 return; |
| 260 } | 258 } |
| 261 | 259 |
| 262 if ((_firstZeroTimer == null) && _heap.isEmpty) { | 260 if ((_firstZeroTimer == null) && _heap.isEmpty) { |
| 263 // No pending timers: Close the receive port and let the event handler | 261 // No pending timers: Close the receive port and let the event handler |
| 264 // know. | 262 // know. |
| 265 if (_receivePort != null) { | 263 if (_receivePort != null) { |
| 266 _EventHandler._sendData(null, _sendPort, _NO_TIMER); | 264 VMLibraryHooks.eventHandlerSendData(null, _sendPort, _NO_TIMER); |
| 267 _shutdownTimerHandler(); | 265 _shutdownTimerHandler(); |
| 268 } | 266 } |
| 269 } else { | 267 } else { |
| 270 if (_receivePort == null) { | 268 if (_receivePort == null) { |
| 271 // Create a receive port and register a message handler for the timer | 269 // Create a receive port and register a message handler for the timer |
| 272 // events. | 270 // events. |
| 273 _createTimerHandler(); | 271 _createTimerHandler(); |
| 274 } | 272 } |
| 275 if (_firstZeroTimer != null) { | 273 if (_firstZeroTimer != null) { |
| 276 if (!_messagePending) { | 274 if (!_messagePending) { |
| 277 _sendPort.send(null); | 275 _sendPort.send(null); |
| 278 _messagePending = true; // Reset when the port receives a message. | 276 _messagePending = true; // Reset when the port receives a message. |
| 279 } | 277 } |
| 280 } else { | 278 } else { |
| 281 var wakeupTime = _heap.first._wakeupTime; | 279 var wakeupTime = _heap.first._wakeupTime; |
| 282 if ((_scheduledWakeupTime == null) || | 280 if ((_scheduledWakeupTime == null) || |
| 283 (wakeupTime != _scheduledWakeupTime)) { | 281 (wakeupTime != _scheduledWakeupTime)) { |
| 284 _EventHandler._sendData(null, _sendPort, wakeupTime); | 282 VMLibraryHooks.eventHandlerSendData(null, _sendPort, wakeupTime); |
| 285 _scheduledWakeupTime = wakeupTime; | 283 _scheduledWakeupTime = wakeupTime; |
| 286 } | 284 } |
| 287 } | 285 } |
| 288 } | 286 } |
| 289 } | 287 } |
| 290 | 288 |
| 291 static void _handleTimeout(pendingImmediateCallback) { | 289 static void _handleTimeout() { |
| 292 // Fast exit if no timers have been scheduled. | 290 // Fast exit if no timers have been scheduled. |
| 293 if (_heap.isEmpty && (_firstZeroTimer == null)) { | 291 if (_heap.isEmpty && (_firstZeroTimer == null)) { |
| 294 assert(_receivePort == null); | 292 assert(_receivePort == null); |
| 295 return; | 293 return; |
| 296 } | 294 } |
| 297 | 295 |
| 298 // Collect all pending timers. | 296 // Collect all pending timers. |
| 299 var head = null; | 297 var head = null; |
| 300 var tail = null; | 298 var tail = null; |
| 301 if (_heap.isEmpty) { | 299 if (_heap.isEmpty) { |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 391 // Mark timer as inactive. | 389 // Mark timer as inactive. |
| 392 timer._callback = null; | 390 timer._callback = null; |
| 393 } | 391 } |
| 394 callback(timer); | 392 callback(timer); |
| 395 // Re-insert repeating timer if not canceled. | 393 // Re-insert repeating timer if not canceled. |
| 396 if (timer._repeating && (timer._callback != null)) { | 394 if (timer._repeating && (timer._callback != null)) { |
| 397 timer._advanceWakeupTime(); | 395 timer._advanceWakeupTime(); |
| 398 timer._addTimerToHeap(); | 396 timer._addTimerToHeap(); |
| 399 } | 397 } |
| 400 // Execute pending micro tasks. | 398 // Execute pending micro tasks. |
| 401 pendingImmediateCallback(); | 399 _runPendingImmediateCallback(); |
| 402 } | 400 } |
| 403 } | 401 } |
| 404 } finally { | 402 } finally { |
| 405 _handlingCallbacks = false; | 403 _handlingCallbacks = false; |
| 406 _notifyEventHandler(); | 404 _notifyEventHandler(); |
| 407 } | 405 } |
| 408 } | 406 } |
| 409 | 407 |
| 410 // Creates a receive port and registers an empty handler on that port. Just | 408 // Creates a receive port and registers an empty handler on that port. Just |
| 411 // the triggering of the event loop will ensure that timers are executed. | 409 // the triggering of the event loop will ensure that timers are executed. |
| (...skipping 20 matching lines...) Expand all Loading... |
| 432 // The Timer factory registered with the dart:async library by the embedder. | 430 // The Timer factory registered with the dart:async library by the embedder. |
| 433 static Timer _factory(int milliSeconds, | 431 static Timer _factory(int milliSeconds, |
| 434 void callback(Timer timer), | 432 void callback(Timer timer), |
| 435 bool repeating) { | 433 bool repeating) { |
| 436 if (repeating) { | 434 if (repeating) { |
| 437 return new _Timer.periodic(milliSeconds, callback); | 435 return new _Timer.periodic(milliSeconds, callback); |
| 438 } | 436 } |
| 439 return new _Timer(milliSeconds, callback); | 437 return new _Timer(milliSeconds, callback); |
| 440 } | 438 } |
| 441 } | 439 } |
| 440 |
| 441 _setupHooks() { |
| 442 VMLibraryHooks.timerFactory = _Timer._factory; |
| 443 } |
| OLD | NEW |