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

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

Issue 1477353002: Revert of Move throttling of background timers into the renderer scheduler (Closed) Base URL: https://chromium.googlesource.com/chromium/src.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
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)
46 , m_repeatInterval(0) 47 , m_repeatInterval(0)
47 , m_cancellableTimerTask(nullptr) 48 , m_cancellableTimerTask(nullptr)
48 , m_webScheduler(Platform::current()->currentThread()->scheduler()) 49 , m_webScheduler(Platform::current()->currentThread()->scheduler())
49 #if ENABLE(ASSERT) 50 #if ENABLE(ASSERT)
50 , m_thread(currentThread()) 51 , m_thread(currentThread())
51 #endif 52 #endif
52 { 53 {
53 } 54 }
54 55
55 TimerBase::~TimerBase() 56 TimerBase::~TimerBase()
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
88 89
89 WebTaskRunner* TimerBase::timerTaskRunner() 90 WebTaskRunner* TimerBase::timerTaskRunner()
90 { 91 {
91 return m_webScheduler->timerTaskRunner(); 92 return m_webScheduler->timerTaskRunner();
92 } 93 }
93 94
94 void TimerBase::setNextFireTime(double now, double delay) 95 void TimerBase::setNextFireTime(double now, double delay)
95 { 96 {
96 ASSERT(m_thread == currentThread()); 97 ASSERT(m_thread == currentThread());
97 98
98 double newTime = now + delay; 99 m_unalignedNextFireTime = now + delay;
99 100
101 double newTime = alignedFireTime(m_unalignedNextFireTime);
100 if (m_nextFireTime != newTime) { 102 if (m_nextFireTime != newTime) {
101 m_nextFireTime = newTime; 103 m_nextFireTime = newTime;
102 if (m_cancellableTimerTask) 104 if (m_cancellableTimerTask)
103 m_cancellableTimerTask->cancel(); 105 m_cancellableTimerTask->cancel();
104 m_cancellableTimerTask = new CancellableTimerTask(this); 106 m_cancellableTimerTask = new CancellableTimerTask(this);
105 107 if (newTime != m_unalignedNextFireTime) {
106 double delayMs = 1000.0 * (newTime - now); 108 // If the timer is being aligned, use postTimerTaskAt() to schedule it
107 timerTaskRunner()->postDelayedTask(m_location, m_cancellableTimerTask, d elayMs); 109 // so that the relative order of aligned timers is preserved.
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 }
108 } 116 }
109 } 117 }
110 118
111 NO_LAZY_SWEEP_SANITIZE_ADDRESS 119 NO_LAZY_SWEEP_SANITIZE_ADDRESS
112 void TimerBase::runInternal() 120 void TimerBase::runInternal()
113 { 121 {
114 if (!canFire()) 122 if (!canFire())
115 return; 123 return;
116 124
117 TRACE_EVENT0("blink", "TimerBase::run"); 125 TRACE_EVENT0("blink", "TimerBase::run");
118 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());
119 TRACE_EVENT_SET_SAMPLING_STATE("blink", "BlinkInternal"); 127 TRACE_EVENT_SET_SAMPLING_STATE("blink", "BlinkInternal");
120 128
129 m_nextFireTime = 0;
121 if (m_repeatInterval) { 130 if (m_repeatInterval) {
122 double now = monotonicallyIncreasingTime(); 131 double now = monotonicallyIncreasingTime();
123 // This computation should be drift free, and it will cope if we miss a beat, 132 // This computation should be drift free, and it will cope if we miss a beat,
124 // which can easily happen if the thread is busy. It will also cope if we get 133 // which can easily happen if the thread is busy. It will also cope if we get
125 // called slightly before m_unalignedNextFireTime, which can happen due to lack 134 // called slightly before m_unalignedNextFireTime, which can happen due to lack
126 // of timer precision. 135 // of timer precision.
127 double intervalToNextFireTime = m_repeatInterval - fmod(now - m_nextFire Time, m_repeatInterval); 136 double intervalToNextFireTime = m_repeatInterval - fmod(now - m_unaligne dNextFireTime, m_repeatInterval);
128 setNextFireTime(monotonicallyIncreasingTime(), intervalToNextFireTime); 137 setNextFireTime(monotonicallyIncreasingTime(), intervalToNextFireTime);
129 } else {
130 m_nextFireTime = 0;
131 } 138 }
132 fired(); 139 fired();
133 TRACE_EVENT_SET_SAMPLING_STATE("blink", "Sleeping"); 140 TRACE_EVENT_SET_SAMPLING_STATE("blink", "Sleeping");
134 } 141 }
135 142
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
136 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
137 { 155 {
138 return a->m_nextFireTime < b->m_nextFireTime; 156 return a->m_unalignedNextFireTime < b->m_unalignedNextFireTime;
139 } 157 }
140 158
141 } // namespace blink 159 } // 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