| 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 // Set jitter to wake up timer events that would happen in _TIMER_JITTER ms. |
| 7 /* | |
| 8 * Set jitter to wake up timer events that would happen in _TIMER_JITTER ms. | |
| 9 */ | |
| 10 static final int _TIMER_JITTER = 0; | 7 static final int _TIMER_JITTER = 0; |
| 11 | 8 |
| 12 /* | 9 // Disables the timer. |
| 13 * Disables the timer. | |
| 14 */ | |
| 15 static final int _NO_TIMER = -1; | 10 static final int _NO_TIMER = -1; |
| 16 | 11 |
| 17 static Timer _createTimer(void callback(Timer timer), | 12 static Timer _createTimer(void callback(Timer timer), |
| 18 int milliSeconds, | 13 int milliSeconds, |
| 19 bool repeating) { | 14 bool repeating) { |
| 20 EventHandler._start(); | 15 EventHandler._start(); |
| 21 if (_timers === null) { | 16 if (_timers === null) { |
| 22 _timers = new DoubleLinkedQueue<_Timer>(); | 17 _timers = new DoubleLinkedQueue<_Timer>(); |
| 23 } | 18 } |
| 24 Timer timer = new _Timer._internal(); | 19 Timer timer = new _Timer._internal(); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 41 | 36 |
| 42 _Timer._internal() {} | 37 _Timer._internal() {} |
| 43 | 38 |
| 44 void _clear() { | 39 void _clear() { |
| 45 _callback = null; | 40 _callback = null; |
| 46 _milliSeconds = 0; | 41 _milliSeconds = 0; |
| 47 _wakeupTime = 0; | 42 _wakeupTime = 0; |
| 48 _repeating = false; | 43 _repeating = false; |
| 49 } | 44 } |
| 50 | 45 |
| 51 /* | 46 |
| 52 * Cancels a set timer. The timer is removed from the timer list and if | 47 // Cancels a set timer. The timer is removed from the timer list and if |
| 53 * the given timer is the earliest timer the native timer is reset. | 48 // the given timer is the earliest timer the native timer is reset. |
| 54 */ | |
| 55 void cancel() { | 49 void cancel() { |
| 56 _clear(); | 50 _clear(); |
| 57 DoubleLinkedQueueEntry<_Timer> entry = _timers.firstEntry(); | 51 DoubleLinkedQueueEntry<_Timer> entry = _timers.firstEntry(); |
| 58 DoubleLinkedQueueEntry<_Timer> first = _timers.firstEntry(); | 52 DoubleLinkedQueueEntry<_Timer> first = _timers.firstEntry(); |
| 59 | 53 |
| 60 while (entry !== null) { | 54 while (entry !== null) { |
| 61 if (entry.element === this) { | 55 if (entry.element === this) { |
| 62 entry.remove(); | 56 entry.remove(); |
| 63 if (first.element == this) { | 57 if (first.element == this) { |
| 64 entry = _timers.firstEntry(); | 58 entry = _timers.firstEntry(); |
| 65 _notifyEventHandler(); | 59 _notifyEventHandler(); |
| 66 } | 60 } |
| 67 return; | 61 return; |
| 68 } | 62 } |
| 69 entry = entry.nextEntry(); | 63 entry = entry.nextEntry(); |
| 70 } | 64 } |
| 71 } | 65 } |
| 72 | 66 |
| 73 void _advanceWakeupTime() { | 67 void _advanceWakeupTime() { |
| 74 _wakeupTime += _milliSeconds; | 68 _wakeupTime += _milliSeconds; |
| 75 } | 69 } |
| 76 | 70 |
| 77 /* | 71 // Adds a timer to the timer list and resets the native timer if it is the |
| 78 * Adds a timer to the timer list and resets the native timer if it is the | 72 // earliest timer in the list. Timers with the same wakeup time are enqueued |
| 79 * earliest timer in the list. Timers with the same wakeup time are enqueued | 73 // in order and notified in FIFO order. |
| 80 * in order and notified in FIFO order. | |
| 81 */ | |
| 82 void _addTimerToList() { | 74 void _addTimerToList() { |
| 83 if (_callback !== null) { | 75 if (_callback !== null) { |
| 84 | 76 |
| 85 DoubleLinkedQueueEntry<_Timer> entry = _timers.firstEntry(); | 77 DoubleLinkedQueueEntry<_Timer> entry = _timers.firstEntry(); |
| 86 while (entry !== null) { | 78 while (entry !== null) { |
| 87 if (_wakeupTime < entry.element._wakeupTime) { | 79 if (_wakeupTime < entry.element._wakeupTime) { |
| 88 entry.prepend(this); | 80 entry.prepend(this); |
| 89 return; | 81 return; |
| 90 } | 82 } |
| 91 entry = entry.nextEntry(); | 83 entry = entry.nextEntry(); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 116 // events. | 108 // events. |
| 117 _createTimerHandler(); | 109 _createTimerHandler(); |
| 118 } | 110 } |
| 119 EventHandler._sendData(-1, | 111 EventHandler._sendData(-1, |
| 120 _receivePort, | 112 _receivePort, |
| 121 _timers.firstEntry().element._wakeupTime); | 113 _timers.firstEntry().element._wakeupTime); |
| 122 } | 114 } |
| 123 } | 115 } |
| 124 | 116 |
| 125 | 117 |
| 126 /* | 118 // Creates a receive port and registers the timer handler on that |
| 127 * Creates a receive port and registers the timer handler on that receive | 119 // receive port. |
| 128 * port. | |
| 129 */ | |
| 130 void _createTimerHandler() { | 120 void _createTimerHandler() { |
| 131 | 121 |
| 132 void _handleTimeout() { | 122 void _handleTimeout() { |
| 133 int currentTime = (new Date.now()).value + _TIMER_JITTER; | 123 int currentTime = (new Date.now()).value + _TIMER_JITTER; |
| 134 | 124 |
| 135 // Collect all pending timers. | 125 // Collect all pending timers. |
| 136 DoubleLinkedQueueEntry<_Timer> entry = _timers.firstEntry(); | 126 DoubleLinkedQueueEntry<_Timer> entry = _timers.firstEntry(); |
| 137 var pending_timers = new List(); | 127 var pending_timers = new List(); |
| 138 while (entry !== null) { | 128 while (entry !== null) { |
| 139 _Timer timer = entry.element; | 129 _Timer timer = entry.element; |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 171 }); | 161 }); |
| 172 } | 162 } |
| 173 } | 163 } |
| 174 | 164 |
| 175 void _shutdownTimerHandler() { | 165 void _shutdownTimerHandler() { |
| 176 _receivePort.close(); | 166 _receivePort.close(); |
| 177 _receivePort = null; | 167 _receivePort = null; |
| 178 } | 168 } |
| 179 | 169 |
| 180 | 170 |
| 181 /* | 171 // Timers are ordered by wakeup time. |
| 182 * Timers are ordered by wakeup time. | |
| 183 */ | |
| 184 static DoubleLinkedQueue<_Timer> _timers; | 172 static DoubleLinkedQueue<_Timer> _timers; |
| 185 | 173 |
| 186 static ReceivePort _receivePort; | 174 static ReceivePort _receivePort; |
| 187 static bool _handling_callbacks = false; | 175 static bool _handling_callbacks = false; |
| 188 | 176 |
| 189 var _callback; | 177 var _callback; |
| 190 int _milliSeconds; | 178 int _milliSeconds; |
| 191 int _wakeupTime; | 179 int _wakeupTime; |
| 192 bool _repeating; | 180 bool _repeating; |
| 193 } | 181 } |
| 194 | 182 |
| OLD | NEW |