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 import 'dart:_internal' hide Symbol; | 5 import 'dart:_internal' hide Symbol; |
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 330 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
341 | 341 |
342 // Fast exit if no pending timers. | 342 // Fast exit if no pending timers. |
343 if (pendingTimers.length == 0) { | 343 if (pendingTimers.length == 0) { |
344 return; | 344 return; |
345 } | 345 } |
346 | 346 |
347 // Trigger all of the pending timers. New timers added as part of the | 347 // Trigger all of the pending timers. New timers added as part of the |
348 // callbacks will be enqueued now and notified in the next spin at the | 348 // callbacks will be enqueued now and notified in the next spin at the |
349 // earliest. | 349 // earliest. |
350 _handlingCallbacks = true; | 350 _handlingCallbacks = true; |
351 var i = 0; | |
352 try { | 351 try { |
353 for (; i < pendingTimers.length; i++) { | 352 for (var i = 0; i < pendingTimers.length; i++) { |
354 // Next pending timer. | 353 // Next pending timer. |
355 var timer = pendingTimers[i]; | 354 var timer = pendingTimers[i]; |
356 timer._indexOrNext = null; | 355 timer._indexOrNext = null; |
357 | 356 |
358 // One of the timers in the pending_timers list can cancel | 357 // One of the timers in the pending_timers list can cancel |
359 // one of the later timers which will set the callback to | 358 // one of the later timers which will set the callback to |
360 // null. Or the pending zero timer has been canceled earlier. | 359 // null. Or the pending zero timer has been canceled earlier. |
361 if (timer._callback != null) { | 360 if (timer._callback != null) { |
362 var callback = timer._callback; | 361 var callback = timer._callback; |
363 if (!timer._repeating) { | 362 if (!timer._repeating) { |
364 // Mark timer as inactive. | 363 // Mark timer as inactive. |
365 timer._callback = null; | 364 timer._callback = null; |
366 } | 365 } |
367 callback(timer); | 366 callback(timer); |
368 // Re-insert repeating timer if not canceled. | 367 // Re-insert repeating timer if not canceled. |
369 if (timer._repeating && (timer._callback != null)) { | 368 if (timer._repeating && (timer._callback != null)) { |
370 timer._advanceWakeupTime(); | 369 timer._advanceWakeupTime(); |
371 timer._enqueue(); | 370 timer._enqueue(); |
372 } | 371 } |
373 // Execute pending micro tasks. | 372 // Execute pending micro tasks. |
374 var immediateCallback = _removePendingImmediateCallback(); | 373 var immediateCallback = _removePendingImmediateCallback(); |
375 if (immediateCallback != null) { | 374 if (immediateCallback != null) { |
376 immediateCallback(); | 375 immediateCallback(); |
377 } | 376 } |
378 } | 377 } |
379 } | 378 } |
380 } finally { | 379 } finally { |
381 _handlingCallbacks = false; | 380 _handlingCallbacks = false; |
382 // Re-queue timers we didn't get to. | |
383 for (i++; i < pendingTimers.length; i++) { | |
384 var timer = pendingTimers[i]; | |
385 timer._enqueue(); | |
386 } | |
387 _notifyEventHandler(); | |
388 } | 381 } |
389 } | 382 } |
390 | 383 |
391 static void _handleMessage(msg) { | 384 static void _handleMessage(msg) { |
392 var pendingTimers; | 385 var pendingTimers; |
393 if (msg == _ZERO_EVENT) { | 386 if (msg == _ZERO_EVENT) { |
394 pendingTimers = _queueFromZeroEvent(); | 387 pendingTimers = _queueFromZeroEvent(); |
395 assert(pendingTimers.length > 0); | 388 assert(pendingTimers.length > 0); |
396 } else { | 389 } else { |
397 assert(msg == _TIMEOUT_EVENT); | 390 assert(msg == _TIMEOUT_EVENT); |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
443 if (repeating) { | 436 if (repeating) { |
444 return new _Timer.periodic(milliSeconds, callback); | 437 return new _Timer.periodic(milliSeconds, callback); |
445 } | 438 } |
446 return new _Timer(milliSeconds, callback); | 439 return new _Timer(milliSeconds, callback); |
447 } | 440 } |
448 } | 441 } |
449 | 442 |
450 _setupHooks() { | 443 _setupHooks() { |
451 VMLibraryHooks.timerFactory = _Timer._factory; | 444 VMLibraryHooks.timerFactory = _Timer._factory; |
452 } | 445 } |
OLD | NEW |