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

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

Issue 1373503002: Fix the drift in repeating timers (try #2) (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rename some variables and add a todo Created 5 years, 2 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
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 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
103 m_nextFireTime = newTime; 103 m_nextFireTime = newTime;
104 if (m_cancellableTimerTask) 104 if (m_cancellableTimerTask)
105 m_cancellableTimerTask->cancel(); 105 m_cancellableTimerTask->cancel();
106 m_cancellableTimerTask = new CancellableTimerTask(this); 106 m_cancellableTimerTask = new CancellableTimerTask(this);
107 if (newTime != m_unalignedNextFireTime) { 107 if (newTime != m_unalignedNextFireTime) {
108 // If the timer is being aligned, use postTimerTaskAt() to schedule it 108 // If the timer is being aligned, use postTimerTaskAt() to schedule it
109 // so that the relative order of aligned timers is preserved. 109 // so that the relative order of aligned timers is preserved.
110 // TODO(skyostil): Move timer alignment into the scheduler. 110 // TODO(skyostil): Move timer alignment into the scheduler.
111 m_webScheduler->postTimerTaskAt(m_location, m_cancellableTimerTask, m_nextFireTime); 111 m_webScheduler->postTimerTaskAt(m_location, m_cancellableTimerTask, m_nextFireTime);
112 } else { 112 } else {
113 // Round the delay up to the nearest millisecond to be consistant wi th the 113 double delayMs = 1000.0 * (newTime - now);
114 // previous behavior of BlinkPlatformImpl::setSharedTimerFireInterva l. 114 m_webScheduler->timerTaskRunner()->postDelayedTask(m_location, m_can cellableTimerTask, delayMs);
115 long long delayMs = static_cast<long long>(ceil((newTime - now) * 10 00.0));
116 if (delayMs < 0)
117 delayMs = 0;
118 timerTaskRunner()->postDelayedTask(m_location, m_cancellableTimerTas k, delayMs);
119 } 115 }
120 } 116 }
121 } 117 }
122 118
123 NO_LAZY_SWEEP_SANITIZE_ADDRESS 119 NO_LAZY_SWEEP_SANITIZE_ADDRESS
124 void TimerBase::runInternal() 120 void TimerBase::runInternal()
125 { 121 {
126 if (!canFire()) 122 if (!canFire())
127 return; 123 return;
128 124
129 TRACE_EVENT0("blink", "TimerBase::run"); 125 TRACE_EVENT0("blink", "TimerBase::run");
130 ASSERT_WITH_MESSAGE(m_thread == currentThread(), "Timer posted by %s %s was run on a different thread", m_location.functionName(), m_location.fileName()); 126 ASSERT_WITH_MESSAGE(m_thread == currentThread(), "Timer posted by %s %s was run on a different thread", m_location.functionName(), m_location.fileName());
131 TRACE_EVENT_SET_SAMPLING_STATE("blink", "BlinkInternal"); 127 TRACE_EVENT_SET_SAMPLING_STATE("blink", "BlinkInternal");
132 128
133 m_nextFireTime = 0; 129 m_nextFireTime = 0;
134 // Note: repeating timers drift, but this is preserving the functionality of the old timer heap. 130 if (m_repeatInterval) {
135 // See crbug.com/328700. 131 double now = monotonicallyIncreasingTime();
136 if (m_repeatInterval) 132 // This computation should be drift free, and it will cope if we miss a beat,
137 setNextFireTime(monotonicallyIncreasingTime(), m_repeatInterval); 133 // which can easily happen if the thread is busy. It will also cope if we get
134 // called slightly before m_unalignedNextFireTime, which can happen due to lack
135 // of timer precision.
136 double intervalToNextFireTime = m_repeatInterval - fmod(now - m_unaligne dNextFireTime, m_repeatInterval);
137 setNextFireTime(monotonicallyIncreasingTime(), intervalToNextFireTime);
138 }
138 fired(); 139 fired();
139 TRACE_EVENT_SET_SAMPLING_STATE("blink", "Sleeping"); 140 TRACE_EVENT_SET_SAMPLING_STATE("blink", "Sleeping");
140 } 141 }
141 142
142 void TimerBase::didChangeAlignmentInterval(double now) 143 void TimerBase::didChangeAlignmentInterval(double now)
143 { 144 {
144 setNextFireTime(now, m_unalignedNextFireTime - now); 145 setNextFireTime(now, m_unalignedNextFireTime - now);
145 } 146 }
146 147
147 double TimerBase::nextUnalignedFireInterval() const 148 double TimerBase::nextUnalignedFireInterval() const
148 { 149 {
149 ASSERT(isActive()); 150 ASSERT(isActive());
150 return std::max(m_unalignedNextFireTime - monotonicallyIncreasingTime(), 0.0 ); 151 return std::max(m_unalignedNextFireTime - monotonicallyIncreasingTime(), 0.0 );
151 } 152 }
152 153
153 bool TimerBase::Comparator::operator()(const TimerBase* a, const TimerBase* b) c onst 154 bool TimerBase::Comparator::operator()(const TimerBase* a, const TimerBase* b) c onst
154 { 155 {
155 return a->m_unalignedNextFireTime < b->m_unalignedNextFireTime; 156 return a->m_unalignedNextFireTime < b->m_unalignedNextFireTime;
156 } 157 }
157 158
158 } // namespace blink 159 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/core/dom/ScriptRunnerTest.cpp ('k') | third_party/WebKit/Source/platform/TimerTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698