| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 class _Timer implements Timer { | 5 class _Timer implements Timer { |
| 6 | 6 |
| 7 /* | 7 /* |
| 8 * Set jitter to wake up timer events that would happen in _TIMER_JITTER ms. | 8 * Set jitter to wake up timer events that would happen in _TIMER_JITTER ms. |
| 9 */ | 9 */ |
| 10 static final int _TIMER_JITTER = 0; | 10 static final int _TIMER_JITTER = 0; |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 86 } | 86 } |
| 87 entry = entry.nextEntry(); | 87 entry = entry.nextEntry(); |
| 88 } | 88 } |
| 89 _timers.addLast(this); | 89 _timers.addLast(this); |
| 90 } | 90 } |
| 91 } | 91 } |
| 92 | 92 |
| 93 | 93 |
| 94 void _notifyEventHandler() { | 94 void _notifyEventHandler() { |
| 95 if (_timers.firstEntry() === null) { | 95 if (_timers.firstEntry() === null) { |
| 96 EventHandler._sendData(-1, _receivePort, _NO_TIMER); | 96 if (_receivePort != null) { |
| 97 _shutdownTimerHandler(); | 97 EventHandler._sendData(-1, _receivePort, _NO_TIMER); |
| 98 _shutdownTimerHandler(); |
| 99 } |
| 98 } else { | 100 } else { |
| 99 EventHandler._sendData(-1, | 101 EventHandler._sendData(-1, |
| 100 _receivePort, | 102 _receivePort, |
| 101 _timers.firstEntry().element._wakeupTime); | 103 _timers.firstEntry().element._wakeupTime); |
| 102 } | 104 } |
| 103 } | 105 } |
| 104 | 106 |
| 105 | 107 |
| 106 /* | 108 /* |
| 107 * Creates a receive port and registers the timer handler on that receive | 109 * Creates a receive port and registers the timer handler on that receive |
| 108 * port. | 110 * port. |
| 109 */ | 111 */ |
| 110 void _createTimerHandler() { | 112 void _createTimerHandler() { |
| 111 | 113 |
| 112 void _handleTimeout() { | 114 void _handleTimeout() { |
| 113 int currentTime = (new Date.now()).value + _TIMER_JITTER; | 115 int currentTime = (new Date.now()).value + _TIMER_JITTER; |
| 114 | 116 |
| 115 DoubleLinkedQueueEntry<_Timer> entry = _timers.firstEntry(); | 117 DoubleLinkedQueueEntry<_Timer> entry = _timers.firstEntry(); |
| 116 DoubleLinkedQueueEntry<_Timer> current; | |
| 117 while (entry !== null) { | 118 while (entry !== null) { |
| 118 current = entry; | 119 _Timer timer = entry.element; |
| 119 _Timer timer = current.element; | |
| 120 entry = entry.nextEntry(); | |
| 121 if (timer._wakeupTime <= currentTime) { | 120 if (timer._wakeupTime <= currentTime) { |
| 122 current.remove(); | 121 entry.remove(); |
| 123 (current.element._callback)(current.element); | 122 timer._callback(timer); |
| 124 if (current.element._repeating) { | 123 // Always process the event with the earliest wakeupTime first. |
| 125 current.element._advanceWakeupTime(); | 124 entry = _timers.firstEntry(); |
| 125 if (timer._repeating) { |
| 126 timer._advanceWakeupTime(); |
| 126 timer._addTimerToList(); | 127 timer._addTimerToList(); |
| 127 _notifyEventHandler(); | 128 _notifyEventHandler(); |
| 128 } | 129 } |
| 129 } else { | 130 } else { |
| 130 break; | 131 break; |
| 131 } | 132 } |
| 132 } | 133 } |
| 133 _notifyEventHandler(); | 134 _notifyEventHandler(); |
| 134 } | 135 } |
| 135 | 136 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 153 static DoubleLinkedQueue<_Timer> _timers; | 154 static DoubleLinkedQueue<_Timer> _timers; |
| 154 | 155 |
| 155 static ReceivePort _receivePort; | 156 static ReceivePort _receivePort; |
| 156 | 157 |
| 157 var _callback; | 158 var _callback; |
| 158 int _milliSeconds; | 159 int _milliSeconds; |
| 159 int _wakeupTime; | 160 int _wakeupTime; |
| 160 bool _repeating; | 161 bool _repeating; |
| 161 } | 162 } |
| 162 | 163 |
| OLD | NEW |