Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(421)

Side by Side Diff: sdk/lib/io/timer_impl.dart

Issue 11361190: a === b -> identical(a, b) (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Address comments. Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « sdk/lib/io/string_stream.dart ('k') | sdk/lib/io/websocket_impl.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, 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 // Set jitter to wake up timer events that would happen in _TIMER_JITTER ms. 6 // Set jitter to wake up timer events that would happen in _TIMER_JITTER ms.
7 static const int _TIMER_JITTER = 0; 7 static const int _TIMER_JITTER = 0;
8 8
9 // Disables the timer. 9 // Disables the timer.
10 static const int _NO_TIMER = -1; 10 static const int _NO_TIMER = -1;
11 11
12 static Timer _createTimer(void callback(Timer timer), 12 static Timer _createTimer(void callback(Timer timer),
13 int milliSeconds, 13 int milliSeconds,
14 bool repeating) { 14 bool repeating) {
15 _EventHandler._start(); 15 _EventHandler._start();
16 if (_timers === null) { 16 if (_timers == null) {
17 _timers = new DoubleLinkedQueue<_Timer>(); 17 _timers = new DoubleLinkedQueue<_Timer>();
18 } 18 }
19 Timer timer = new _Timer._internal(); 19 Timer timer = new _Timer._internal();
20 timer._callback = callback; 20 timer._callback = callback;
21 timer._milliSeconds = milliSeconds; 21 timer._milliSeconds = milliSeconds;
22 timer._wakeupTime = (new Date.now()).millisecondsSinceEpoch + milliSeconds; 22 timer._wakeupTime = (new Date.now()).millisecondsSinceEpoch + milliSeconds;
23 timer._repeating = repeating; 23 timer._repeating = repeating;
24 timer._addTimerToList(); 24 timer._addTimerToList();
25 timer._notifyEventHandler(); 25 timer._notifyEventHandler();
26 return timer; 26 return timer;
(...skipping 17 matching lines...) Expand all
44 } 44 }
45 45
46 46
47 // 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
48 // 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.
49 void cancel() { 49 void cancel() {
50 _clear(); 50 _clear();
51 DoubleLinkedQueueEntry<_Timer> entry = _timers.firstEntry(); 51 DoubleLinkedQueueEntry<_Timer> entry = _timers.firstEntry();
52 DoubleLinkedQueueEntry<_Timer> first = _timers.firstEntry(); 52 DoubleLinkedQueueEntry<_Timer> first = _timers.firstEntry();
53 53
54 while (entry !== null) { 54 while (entry != null) {
55 if (entry.element === this) { 55 if (identical(entry.element, this)) {
56 entry.remove(); 56 entry.remove();
57 if (first.element == this) { 57 if (first.element == this) {
58 entry = _timers.firstEntry(); 58 entry = _timers.firstEntry();
59 _notifyEventHandler(); 59 _notifyEventHandler();
60 } 60 }
61 return; 61 return;
62 } 62 }
63 entry = entry.nextEntry(); 63 entry = entry.nextEntry();
64 } 64 }
65 } 65 }
66 66
67 void _advanceWakeupTime() { 67 void _advanceWakeupTime() {
68 _wakeupTime += _milliSeconds; 68 _wakeupTime += _milliSeconds;
69 } 69 }
70 70
71 // Adds a timer to the timer list and resets the native timer if it is the 71 // 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 72 // earliest timer in the list. Timers with the same wakeup time are enqueued
73 // in order and notified in FIFO order. 73 // in order and notified in FIFO order.
74 void _addTimerToList() { 74 void _addTimerToList() {
75 if (_callback !== null) { 75 if (_callback != null) {
76 76
77 DoubleLinkedQueueEntry<_Timer> entry = _timers.firstEntry(); 77 DoubleLinkedQueueEntry<_Timer> entry = _timers.firstEntry();
78 while (entry !== null) { 78 while (entry != null) {
79 if (_wakeupTime < entry.element._wakeupTime) { 79 if (_wakeupTime < entry.element._wakeupTime) {
80 entry.prepend(this); 80 entry.prepend(this);
81 return; 81 return;
82 } 82 }
83 entry = entry.nextEntry(); 83 entry = entry.nextEntry();
84 } 84 }
85 _timers.addLast(this); 85 _timers.addLast(this);
86 } 86 }
87 } 87 }
88 88
89 89
90 void _notifyEventHandler() { 90 void _notifyEventHandler() {
91 if (_handling_callbacks) { 91 if (_handling_callbacks) {
92 // While we are already handling callbacks we will not notify the event 92 // While we are already handling callbacks we will not notify the event
93 // handler. _handleTimeout will call _notifyEventHandler once all pending 93 // handler. _handleTimeout will call _notifyEventHandler once all pending
94 // timers are processed. 94 // timers are processed.
95 return; 95 return;
96 } 96 }
97 97
98 if (_timers.firstEntry() === null) { 98 if (_timers.firstEntry() == null) {
99 // No pending timers: Close the receive port and let the event handler 99 // No pending timers: Close the receive port and let the event handler
100 // know. 100 // know.
101 if (_receivePort !== null) { 101 if (_receivePort != null) {
102 _EventHandler._sendData(null, _receivePort, _NO_TIMER); 102 _EventHandler._sendData(null, _receivePort, _NO_TIMER);
103 _shutdownTimerHandler(); 103 _shutdownTimerHandler();
104 } 104 }
105 } else { 105 } else {
106 if (_receivePort === null) { 106 if (_receivePort == null) {
107 // Create a receive port and register a message handler for the timer 107 // Create a receive port and register a message handler for the timer
108 // events. 108 // events.
109 _createTimerHandler(); 109 _createTimerHandler();
110 } 110 }
111 _EventHandler._sendData(null, 111 _EventHandler._sendData(null,
112 _receivePort, 112 _receivePort,
113 _timers.firstEntry().element._wakeupTime); 113 _timers.firstEntry().element._wakeupTime);
114 } 114 }
115 } 115 }
116 116
117 117
118 // Creates a receive port and registers the timer handler on that 118 // Creates a receive port and registers the timer handler on that
119 // receive port. 119 // receive port.
120 void _createTimerHandler() { 120 void _createTimerHandler() {
121 121
122 void _handleTimeout() { 122 void _handleTimeout() {
123 int currentTime = 123 int currentTime =
124 (new Date.now()).millisecondsSinceEpoch + _TIMER_JITTER; 124 (new Date.now()).millisecondsSinceEpoch + _TIMER_JITTER;
125 125
126 // Collect all pending timers. 126 // Collect all pending timers.
127 DoubleLinkedQueueEntry<_Timer> entry = _timers.firstEntry(); 127 DoubleLinkedQueueEntry<_Timer> entry = _timers.firstEntry();
128 var pending_timers = new List(); 128 var pending_timers = new List();
129 while (entry !== null) { 129 while (entry != null) {
130 _Timer timer = entry.element; 130 _Timer timer = entry.element;
131 if (timer._wakeupTime <= currentTime) { 131 if (timer._wakeupTime <= currentTime) {
132 entry.remove(); 132 entry.remove();
133 pending_timers.addLast(timer); 133 pending_timers.addLast(timer);
134 entry = _timers.firstEntry(); 134 entry = _timers.firstEntry();
135 } else { 135 } else {
136 break; 136 break;
137 } 137 }
138 } 138 }
139 139
(...skipping 13 matching lines...) Expand all
153 timer._addTimerToList(); 153 timer._addTimerToList();
154 } 154 }
155 } 155 }
156 } 156 }
157 } finally { 157 } finally {
158 _handling_callbacks = false; 158 _handling_callbacks = false;
159 } 159 }
160 _notifyEventHandler(); 160 _notifyEventHandler();
161 } 161 }
162 162
163 if(_receivePort === null) { 163 if(_receivePort == null) {
164 _receivePort = new ReceivePort(); 164 _receivePort = new ReceivePort();
165 _receivePort.receive((var message, ignored) { 165 _receivePort.receive((var message, ignored) {
166 _handleTimeout(); 166 _handleTimeout();
167 }); 167 });
168 } 168 }
169 } 169 }
170 170
171 void _shutdownTimerHandler() { 171 void _shutdownTimerHandler() {
172 _receivePort.close(); 172 _receivePort.close();
173 _receivePort = null; 173 _receivePort = null;
(...skipping 17 matching lines...) Expand all
191 _getTimerFactoryClosure() { 191 _getTimerFactoryClosure() {
192 return (int milliSeconds, void callback(Timer timer), bool repeating) { 192 return (int milliSeconds, void callback(Timer timer), bool repeating) {
193 if (repeating) { 193 if (repeating) {
194 return new _Timer.repeating(milliSeconds, callback); 194 return new _Timer.repeating(milliSeconds, callback);
195 } 195 }
196 return new _Timer(milliSeconds, callback); 196 return new _Timer(milliSeconds, callback);
197 }; 197 };
198 } 198 }
199 199
200 200
OLDNEW
« no previous file with comments | « sdk/lib/io/string_stream.dart ('k') | sdk/lib/io/websocket_impl.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698