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

Side by Side Diff: runtime/lib/timer_impl.dart

Issue 1519563003: Use a monotonic clock in the implementation of Timer. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 5 years 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
« no previous file with comments | « runtime/lib/internal_patch.dart ('k') | no next file » | 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) 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 // Timer heap implemented as a array-based binary heap[0]. 5 // Timer heap implemented as a array-based binary heap[0].
6 // 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))
7 // `add`. 7 // `add`.
8 // 8 //
9 // 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
10 // `_id` field set when added to the heap. 10 // `_id` field set when added to the heap.
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after
165 int milliSeconds, 165 int milliSeconds,
166 bool repeating) { 166 bool repeating) {
167 // Negative timeouts are treated as if 0 timeout. 167 // Negative timeouts are treated as if 0 timeout.
168 if (milliSeconds < 0) { 168 if (milliSeconds < 0) {
169 milliSeconds = 0; 169 milliSeconds = 0;
170 } 170 }
171 // Add one because DateTime.now() is assumed to round down 171 // Add one because DateTime.now() is assumed to round down
172 // to nearest millisecond, not up, so that time + duration is before 172 // to nearest millisecond, not up, so that time + duration is before
173 // duration milliseconds from now. Using microsecond timers like 173 // duration milliseconds from now. Using microsecond timers like
174 // Stopwatch allows detecting that the timer fires early. 174 // Stopwatch allows detecting that the timer fires early.
175 int now = new DateTime.now().millisecondsSinceEpoch; 175 int now = VMLibraryHooks.timerMillisecondClock();
176 int wakeupTime = (milliSeconds == 0) ? now : (now + 1 + milliSeconds); 176 int wakeupTime = (milliSeconds == 0) ? now : (now + 1 + milliSeconds);
177 177
178 _Timer timer = new _Timer._internal(callback, 178 _Timer timer = new _Timer._internal(callback,
179 wakeupTime, 179 wakeupTime,
180 milliSeconds, 180 milliSeconds,
181 repeating); 181 repeating);
182 // Enqueue this newly created timer in the appropriate structure and 182 // Enqueue this newly created timer in the appropriate structure and
183 // notify if necessary. 183 // notify if necessary.
184 timer._enqueue(); 184 timer._enqueue();
185 return timer; 185 return timer;
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
225 } 225 }
226 226
227 227
228 void _advanceWakeupTime() { 228 void _advanceWakeupTime() {
229 // Recalculate the next wakeup time. For repeating timers with a 0 timeout 229 // Recalculate the next wakeup time. For repeating timers with a 0 timeout
230 // the next wakeup time is now. 230 // the next wakeup time is now.
231 _id = _nextId(); 231 _id = _nextId();
232 if (_milliSeconds > 0) { 232 if (_milliSeconds > 0) {
233 _wakeupTime += _milliSeconds; 233 _wakeupTime += _milliSeconds;
234 } else { 234 } else {
235 _wakeupTime = new DateTime.now().millisecondsSinceEpoch; 235 _wakeupTime = VMLibraryHooks.timerMillisecondClock();
236 } 236 }
237 } 237 }
238 238
239 239
240 // Adds a timer to the heap or timer list. Timers with the same wakeup time 240 // Adds a timer to the heap or timer list. Timers with the same wakeup time
241 // are enqueued in order and notified in FIFO order. 241 // are enqueued in order and notified in FIFO order.
242 void _enqueue() { 242 void _enqueue() {
243 if (_milliSeconds == 0) { 243 if (_milliSeconds == 0) {
244 if (_firstZeroTimer == null) { 244 if (_firstZeroTimer == null) {
245 _lastZeroTimer = this; 245 _lastZeroTimer = this;
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
333 // timer are expired. The first zero timer will be dispatched when its 333 // timer are expired. The first zero timer will be dispatched when its
334 // corresponding message is delivered. 334 // corresponding message is delivered.
335 var timer; 335 var timer;
336 while (!_heap.isEmpty && (_heap.first._compareTo(_firstZeroTimer) < 0)) { 336 while (!_heap.isEmpty && (_heap.first._compareTo(_firstZeroTimer) < 0)) {
337 timer = _heap.removeFirst(); 337 timer = _heap.removeFirst();
338 pendingTimers.add(timer); 338 pendingTimers.add(timer);
339 } 339 }
340 } else { 340 } else {
341 // Collect pending timers from the timer heap which have expired at this 341 // Collect pending timers from the timer heap which have expired at this
342 // time. 342 // time.
343 var currentTime = new DateTime.now().millisecondsSinceEpoch; 343 var currentTime = VMLibraryHooks.timerMillisecondClock();
344 var timer; 344 var timer;
345 while (!_heap.isEmpty && (_heap.first._wakeupTime <= currentTime)) { 345 while (!_heap.isEmpty && (_heap.first._wakeupTime <= currentTime)) {
346 timer = _heap.removeFirst(); 346 timer = _heap.removeFirst();
347 pendingTimers.add(timer); 347 pendingTimers.add(timer);
348 } 348 }
349 } 349 }
350 return pendingTimers; 350 return pendingTimers;
351 } 351 }
352 352
353 353
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
463 return new _Timer.periodic(milliSeconds, callback); 463 return new _Timer.periodic(milliSeconds, callback);
464 } 464 }
465 return new _Timer(milliSeconds, callback); 465 return new _Timer(milliSeconds, callback);
466 } 466 }
467 } 467 }
468 468
469 469
470 _setupHooks() { 470 _setupHooks() {
471 VMLibraryHooks.timerFactory = _Timer._factory; 471 VMLibraryHooks.timerFactory = _Timer._factory;
472 } 472 }
OLDNEW
« no previous file with comments | « runtime/lib/internal_patch.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698