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

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

Issue 1441073006: Move throttling of background timers into the renderer scheduler (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Last few changes Sami requested 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
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 25 matching lines...) Expand all
36 #include "wtf/HashSet.h" 36 #include "wtf/HashSet.h"
37 #include <algorithm> 37 #include <algorithm>
38 #include <limits.h> 38 #include <limits.h>
39 #include <limits> 39 #include <limits>
40 #include <math.h> 40 #include <math.h>
41 41
42 namespace blink { 42 namespace blink {
43 43
44 TimerBase::TimerBase() 44 TimerBase::TimerBase()
45 : m_nextFireTime(0) 45 : m_nextFireTime(0)
46 , m_unalignedNextFireTime(0)
47 , m_repeatInterval(0) 46 , m_repeatInterval(0)
48 , m_cancellableTimerTask(nullptr) 47 , m_cancellableTimerTask(nullptr)
49 , m_webScheduler(Platform::current()->currentThread()->scheduler()) 48 , m_webScheduler(Platform::current()->currentThread()->scheduler())
50 #if ENABLE(ASSERT) 49 #if ENABLE(ASSERT)
51 , m_thread(currentThread()) 50 , m_thread(currentThread())
52 #endif 51 #endif
53 { 52 {
54 } 53 }
55 54
56 TimerBase::~TimerBase() 55 TimerBase::~TimerBase()
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
89 88
90 WebTaskRunner* TimerBase::timerTaskRunner() 89 WebTaskRunner* TimerBase::timerTaskRunner()
91 { 90 {
92 return m_webScheduler->timerTaskRunner(); 91 return m_webScheduler->timerTaskRunner();
93 } 92 }
94 93
95 void TimerBase::setNextFireTime(double now, double delay) 94 void TimerBase::setNextFireTime(double now, double delay)
96 { 95 {
97 ASSERT(m_thread == currentThread()); 96 ASSERT(m_thread == currentThread());
98 97
99 m_unalignedNextFireTime = now + delay; 98 double newTime = now + delay;
100 99
101 double newTime = alignedFireTime(m_unalignedNextFireTime);
102 if (m_nextFireTime != newTime) { 100 if (m_nextFireTime != newTime) {
103 m_nextFireTime = newTime; 101 m_nextFireTime = newTime;
104 if (m_cancellableTimerTask) 102 if (m_cancellableTimerTask)
105 m_cancellableTimerTask->cancel(); 103 m_cancellableTimerTask->cancel();
106 m_cancellableTimerTask = new CancellableTimerTask(this); 104 m_cancellableTimerTask = new CancellableTimerTask(this);
107 if (newTime != m_unalignedNextFireTime) { 105
108 // If the timer is being aligned, use postTimerTaskAt() to schedule it 106 double delayMs = 1000.0 * (newTime - now);
109 // so that the relative order of aligned timers is preserved. 107 timerTaskRunner()->postDelayedTask(m_location, m_cancellableTimerTask, d elayMs);
110 // TODO(skyostil): Move timer alignment into the scheduler.
111 m_webScheduler->postTimerTaskAt(m_location, m_cancellableTimerTask, m_nextFireTime);
112 } else {
113 double delayMs = 1000.0 * (newTime - now);
114 m_webScheduler->timerTaskRunner()->postDelayedTask(m_location, m_can cellableTimerTask, delayMs);
115 }
116 } 108 }
117 } 109 }
118 110
119 NO_LAZY_SWEEP_SANITIZE_ADDRESS 111 NO_LAZY_SWEEP_SANITIZE_ADDRESS
120 void TimerBase::runInternal() 112 void TimerBase::runInternal()
121 { 113 {
122 if (!canFire()) 114 if (!canFire())
123 return; 115 return;
124 116
125 TRACE_EVENT0("blink", "TimerBase::run"); 117 TRACE_EVENT0("blink", "TimerBase::run");
126 ASSERT_WITH_MESSAGE(m_thread == currentThread(), "Timer posted by %s %s was run on a different thread", m_location.functionName(), m_location.fileName()); 118 ASSERT_WITH_MESSAGE(m_thread == currentThread(), "Timer posted by %s %s was run on a different thread", m_location.functionName(), m_location.fileName());
127 TRACE_EVENT_SET_SAMPLING_STATE("blink", "BlinkInternal"); 119 TRACE_EVENT_SET_SAMPLING_STATE("blink", "BlinkInternal");
128 120
129 m_nextFireTime = 0;
130 if (m_repeatInterval) { 121 if (m_repeatInterval) {
131 double now = monotonicallyIncreasingTime(); 122 double now = monotonicallyIncreasingTime();
132 // This computation should be drift free, and it will cope if we miss a beat, 123 // This computation should be drift free, and it will cope if we miss a beat,
133 // which can easily happen if the thread is busy. It will also cope if we get 124 // 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 125 // called slightly before m_unalignedNextFireTime, which can happen due to lack
135 // of timer precision. 126 // of timer precision.
136 double intervalToNextFireTime = m_repeatInterval - fmod(now - m_unaligne dNextFireTime, m_repeatInterval); 127 double intervalToNextFireTime = m_repeatInterval - fmod(now - m_nextFire Time, m_repeatInterval);
137 setNextFireTime(monotonicallyIncreasingTime(), intervalToNextFireTime); 128 setNextFireTime(monotonicallyIncreasingTime(), intervalToNextFireTime);
129 } else {
130 m_nextFireTime = 0;
138 } 131 }
139 fired(); 132 fired();
140 TRACE_EVENT_SET_SAMPLING_STATE("blink", "Sleeping"); 133 TRACE_EVENT_SET_SAMPLING_STATE("blink", "Sleeping");
141 } 134 }
142 135
143 void TimerBase::didChangeAlignmentInterval(double now)
144 {
145 setNextFireTime(now, m_unalignedNextFireTime - now);
146 }
147
148 double TimerBase::nextUnalignedFireInterval() const
149 {
150 ASSERT(isActive());
151 return std::max(m_unalignedNextFireTime - monotonicallyIncreasingTime(), 0.0 );
152 }
153
154 bool TimerBase::Comparator::operator()(const TimerBase* a, const TimerBase* b) c onst 136 bool TimerBase::Comparator::operator()(const TimerBase* a, const TimerBase* b) c onst
155 { 137 {
156 return a->m_unalignedNextFireTime < b->m_unalignedNextFireTime; 138 return a->m_nextFireTime < b->m_nextFireTime;
157 } 139 }
158 140
159 } // namespace blink 141 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/platform/Timer.h ('k') | third_party/WebKit/Source/platform/TimerTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698