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

Side by Side Diff: Source/platform/Timer.cpp

Issue 1261993006: Fix the drift in repeating timers. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Fixed tests Created 5 years, 4 months 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 | « no previous file | Source/platform/TimerTest.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2006, 2008 Apple Inc. All rights reserved. 2 * Copyright (C) 2006, 2008 Apple Inc. All rights reserved.
3 * Copyright (C) 2009 Google Inc. All rights reserved. 3 * Copyright (C) 2009 Google Inc. All rights reserved.
4 * 4 *
5 * Redistribution and use in source and binary forms, with or without 5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions 6 * modification, are permitted provided that the following conditions
7 * are met: 7 * are met:
8 * 1. Redistributions of source code must retain the above copyright 8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright 10 * 2. Redistributions in binary form must reproduce the above copyright
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
98 m_nextFireTime = newTime; 98 m_nextFireTime = newTime;
99 if (m_cancellableTimerTask) 99 if (m_cancellableTimerTask)
100 m_cancellableTimerTask->cancel(); 100 m_cancellableTimerTask->cancel();
101 m_cancellableTimerTask = new CancellableTimerTask(this); 101 m_cancellableTimerTask = new CancellableTimerTask(this);
102 if (newTime != m_unalignedNextFireTime) { 102 if (newTime != m_unalignedNextFireTime) {
103 // If the timer is being aligned, use postTimerTaskAt() to schedule it 103 // If the timer is being aligned, use postTimerTaskAt() to schedule it
104 // so that the relative order of aligned timers is preserved. 104 // so that the relative order of aligned timers is preserved.
105 // TODO(skyostil): Move timer alignment into the scheduler. 105 // TODO(skyostil): Move timer alignment into the scheduler.
106 m_webScheduler->postTimerTaskAt(m_location, m_cancellableTimerTask, m_nextFireTime); 106 m_webScheduler->postTimerTaskAt(m_location, m_cancellableTimerTask, m_nextFireTime);
107 } else { 107 } else {
108 // Round the delay up to the nearest millisecond to be consistant wi th the 108 m_webScheduler->postTimerTask(m_location, m_cancellableTimerTask, ne wTime - now);
109 // previous behavior of BlinkPlatformImpl::setSharedTimerFireInterva l.
110 long long delayMs = static_cast<long long>(ceil((newTime - now) * 10 00.0));
111 if (delayMs < 0)
112 delayMs = 0;
113 m_webScheduler->postTimerTask(m_location, m_cancellableTimerTask, de layMs);
114 } 109 }
115 } 110 }
116 } 111 }
117 112
118 NO_LAZY_SWEEP_SANITIZE_ADDRESS 113 NO_LAZY_SWEEP_SANITIZE_ADDRESS
119 void TimerBase::runInternal() 114 void TimerBase::runInternal()
120 { 115 {
121 if (!canFire()) 116 if (!canFire())
122 return; 117 return;
123 118
124 TRACE_EVENT0("blink", "TimerBase::run"); 119 TRACE_EVENT0("blink", "TimerBase::run");
125 ASSERT_WITH_MESSAGE(m_thread == currentThread(), "Timer posted by %s %s was run on a different thread", m_location.functionName(), m_location.fileName()); 120 ASSERT_WITH_MESSAGE(m_thread == currentThread(), "Timer posted by %s %s was run on a different thread", m_location.functionName(), m_location.fileName());
126 TRACE_EVENT_SET_SAMPLING_STATE("blink", "BlinkInternal"); 121 TRACE_EVENT_SET_SAMPLING_STATE("blink", "BlinkInternal");
127 122
128 m_nextFireTime = 0; 123 m_nextFireTime = 0;
129 // Note: repeating timers drift, but this is preserving the functionality of the old timer heap. 124 if (m_repeatInterval) {
130 // See crbug.com/328700. 125 double now = monotonicallyIncreasingTime();
131 if (m_repeatInterval) 126 ASSERT(now >= m_unalignedNextFireTime);
132 setNextFireTime(monotonicallyIncreasingTime(), m_repeatInterval); 127 // This computation should be drift free, and it will cope if we miss a beat,
128 // which can easily happen if the thread is busy.
129 double intervalToNextFireTime = m_repeatInterval - fmod(now - m_unaligne dNextFireTime, m_repeatInterval);
130 setNextFireTime(now, intervalToNextFireTime);
131 }
133 fired(); 132 fired();
134 TRACE_EVENT_SET_SAMPLING_STATE("blink", "Sleeping"); 133 TRACE_EVENT_SET_SAMPLING_STATE("blink", "Sleeping");
135 } 134 }
136 135
137 void TimerBase::didChangeAlignmentInterval(double now) 136 void TimerBase::didChangeAlignmentInterval(double now)
138 { 137 {
139 setNextFireTime(now, m_unalignedNextFireTime - now); 138 setNextFireTime(now, m_unalignedNextFireTime - now);
140 } 139 }
141 140
142 double TimerBase::nextUnalignedFireInterval() const 141 double TimerBase::nextUnalignedFireInterval() const
143 { 142 {
144 ASSERT(isActive()); 143 ASSERT(isActive());
145 return std::max(m_unalignedNextFireTime - monotonicallyIncreasingTime(), 0.0 ); 144 return std::max(m_unalignedNextFireTime - monotonicallyIncreasingTime(), 0.0 );
146 } 145 }
147 146
148 bool TimerBase::Comparator::operator()(const TimerBase* a, const TimerBase* b) c onst 147 bool TimerBase::Comparator::operator()(const TimerBase* a, const TimerBase* b) c onst
149 { 148 {
150 return a->m_unalignedNextFireTime < b->m_unalignedNextFireTime; 149 return a->m_unalignedNextFireTime < b->m_unalignedNextFireTime;
151 } 150 }
152 151
153 } // namespace blink 152 } // namespace blink
OLDNEW
« no previous file with comments | « no previous file | Source/platform/TimerTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698