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

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

Issue 956333002: Refactor TimeBase to post tasks. Workers to use real Idle tasks. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Rebased Created 5 years, 8 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
(Empty)
1 /*
2 * Copyright (C) 2006, 2008 Apple Inc. All rights reserved.
3 * Copyright (C) 2009 Google Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 #include "config.h"
28 #include "platform/ThreadTimers.h"
29
30 #include "platform/PlatformThreadData.h"
31 #include "platform/SharedTimer.h"
32 #include "platform/Timer.h"
33 #include "platform/TraceEvent.h"
34 #include "platform/scheduler/Scheduler.h"
35 #include "wtf/CurrentTime.h"
36 #include "wtf/MainThread.h"
37
38 namespace blink {
39
40 // Fire timers for this length of time, and then quit to let the run loop proces s user input events.
41 // 100ms is about a perceptable delay in UI, so use a half of that as a threshol d.
42 // This is to prevent UI freeze when there are too many timers or machine perfor mance is low.
43 static const double maxDurationOfFiringTimers = 0.050;
44
45 // Timers are created, started and fired on the same thread, and each thread has its own ThreadTimers
46 // copy to keep the heap and a set of currently firing timers.
47
48 static PassOwnPtr<MainThreadSharedTimer> mainThreadSharedTimer()
49 {
50 return adoptPtr(new MainThreadSharedTimer);
51 }
52
53 ThreadTimers::ThreadTimers()
54 : m_sharedTimer(0)
55 , m_firingTimers(false)
56 , m_pendingSharedTimerFireTime(0)
57 {
58 if (isMainThread())
59 setSharedTimer(mainThreadSharedTimer());
60 }
61
62 // A worker thread may initialize SharedTimer after some timers are created.
63 // Also, SharedTimer can be replaced with 0 before all timers are destroyed.
64 void ThreadTimers::setSharedTimer(PassOwnPtr<SharedTimer> sharedTimer)
65 {
66 if (m_sharedTimer) {
67 m_sharedTimer->setFiredFunction(0);
68 m_sharedTimer->stop();
69 m_pendingSharedTimerFireTime = 0;
70 }
71
72 m_sharedTimer = sharedTimer;
73
74 if (m_sharedTimer) {
75 m_sharedTimer->setFiredFunction(ThreadTimers::sharedTimerFired);
76 updateSharedTimer();
77 }
78 }
79
80 void ThreadTimers::updateSharedTimer()
81 {
82 if (!m_sharedTimer)
83 return;
84
85 if (m_firingTimers || m_timerHeap.isEmpty()) {
86 m_pendingSharedTimerFireTime = 0;
87 m_sharedTimer->stop();
88 } else {
89 double nextFireTime = m_timerHeap.first()->m_nextFireTime;
90 double currentMonotonicTime = monotonicallyIncreasingTime();
91 if (m_pendingSharedTimerFireTime) {
92 // No need to restart the timer if both the pending fire time and th e new fire time are in the past.
93 if (m_pendingSharedTimerFireTime <= currentMonotonicTime && nextFire Time <= currentMonotonicTime)
94 return;
95 }
96 m_pendingSharedTimerFireTime = nextFireTime;
97 m_sharedTimer->setFireInterval(std::max(nextFireTime - currentMonotonicT ime, 0.0));
98 }
99 }
100
101 void ThreadTimers::sharedTimerFired()
102 {
103 TRACE_EVENT_SET_SAMPLING_STATE("blink", "BlinkInternal");
Sami 2015/04/09 10:52:29 I assume this is for sampled trace events? Can we
alex clarke (OOO till 29th) 2015/04/10 15:29:34 Done.
104
105 // Redirect to non-static method.
106 PlatformThreadData::current().threadTimers().sharedTimerFiredInternal();
107
108 TRACE_EVENT_SET_SAMPLING_STATE("blink", "Sleeping");
109 }
110
111 void ThreadTimers::sharedTimerFiredInternal()
112 {
113 // Do a re-entrancy check.
114 if (m_firingTimers)
115 return;
116 m_firingTimers = true;
117 m_pendingSharedTimerFireTime = 0;
118
119 double fireTime = monotonicallyIncreasingTime();
120 double timeToQuit = fireTime + maxDurationOfFiringTimers;
121
122 while (!m_timerHeap.isEmpty() && m_timerHeap.first()->m_nextFireTime <= fire Time) {
123 TimerBase& timer = *m_timerHeap.first();
124 timer.m_nextFireTime = 0;
125 timer.m_unalignedNextFireTime = 0;
126 timer.heapDeleteMin();
127
128 double interval = timer.repeatInterval();
129 timer.setNextFireTime(interval ? fireTime + interval : 0);
130
131 TRACE_EVENT2("blink", "ThreadTimers::sharedTimerFiredInternal",
132 "src_file", timer.location().fileName(),
133 "src_func", timer.location().functionName());
134
135 // Once the timer has been fired, it may be deleted, so do nothing else with it after this point.
136 timer.fired();
137
138 // Catch the case where the timer asked timers to fire in a nested event loop, or we are over time limit.
139 if (!m_firingTimers || timeToQuit < monotonicallyIncreasingTime() || (is MainThread() && Scheduler::shared()->shouldYieldForHighPriorityWork()))
140 break;
141 }
142
143 m_firingTimers = false;
144
145 updateSharedTimer();
146 }
147
148 void ThreadTimers::fireTimersInNestedEventLoop()
149 {
150 // Reset the reentrancy guard so the timers can fire again.
151 m_firingTimers = false;
152 updateSharedTimer();
153 }
154
155 } // namespace blink
156
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698