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 "ThreadTimers.h"
29 
30 #include "SharedTimer.h"
31 #include "ThreadGlobalData.h"
32 #include "Timer.h"
33 #include <wtf/CurrentTime.h>
34 
35 namespace WebCore {
36 
37 // Fire timers for this length of time, and then quit to let the run loop process user input events.
38 // 100ms is about a perceptable delay in UI, so use a half of that as a threshold.
39 // This is to prevent UI freeze when there are too many timers or machine performance is low.
40 static const double maxDurationOfFiringTimers = 0.050;
41 
42 // Timers are created, started and fired on the same thread, and each thread has its own ThreadTimers
43 // copy to keep the heap and a set of currently firing timers.
44 
mainThreadSharedTimer()45 static MainThreadSharedTimer* mainThreadSharedTimer()
46 {
47     static MainThreadSharedTimer* timer = new MainThreadSharedTimer;
48     return timer;
49 }
50 
ThreadTimers()51 ThreadTimers::ThreadTimers()
52     : m_sharedTimer(0)
53     , m_firingTimers(false)
54 {
55     if (isMainThread())
56         setSharedTimer(mainThreadSharedTimer());
57 }
58 
59 // A worker thread may initialize SharedTimer after some timers are created.
60 // Also, SharedTimer can be replaced with 0 before all timers are destroyed.
setSharedTimer(SharedTimer * sharedTimer)61 void ThreadTimers::setSharedTimer(SharedTimer* sharedTimer)
62 {
63     if (m_sharedTimer) {
64         m_sharedTimer->setFiredFunction(0);
65         m_sharedTimer->stop();
66     }
67 
68     m_sharedTimer = sharedTimer;
69 
70     if (sharedTimer) {
71         m_sharedTimer->setFiredFunction(ThreadTimers::sharedTimerFired);
72         updateSharedTimer();
73     }
74 }
75 
updateSharedTimer()76 void ThreadTimers::updateSharedTimer()
77 {
78     if (!m_sharedTimer)
79         return;
80 
81     if (m_firingTimers || m_timerHeap.isEmpty())
82         m_sharedTimer->stop();
83     else
84         m_sharedTimer->setFireTime(m_timerHeap.first()->m_nextFireTime);
85 }
86 
sharedTimerFired()87 void ThreadTimers::sharedTimerFired()
88 {
89     // Redirect to non-static method.
90     threadGlobalData().threadTimers().sharedTimerFiredInternal();
91 }
92 
sharedTimerFiredInternal()93 void ThreadTimers::sharedTimerFiredInternal()
94 {
95     // Do a re-entrancy check.
96     if (m_firingTimers)
97         return;
98     m_firingTimers = true;
99 
100     double fireTime = currentTime();
101     double timeToQuit = fireTime + maxDurationOfFiringTimers;
102 
103     while (!m_timerHeap.isEmpty() && m_timerHeap.first()->m_nextFireTime <= fireTime) {
104         TimerBase* timer = m_timerHeap.first();
105         timer->m_nextFireTime = 0;
106         timer->heapDeleteMin();
107 
108         double interval = timer->repeatInterval();
109         timer->setNextFireTime(interval ? fireTime + interval : 0);
110 
111         // Once the timer has been fired, it may be deleted, so do nothing else with it after this point.
112         timer->fired();
113 
114         // Catch the case where the timer asked timers to fire in a nested event loop, or we are over time limit.
115         if (!m_firingTimers || timeToQuit < currentTime())
116             break;
117     }
118 
119     m_firingTimers = false;
120 
121     updateSharedTimer();
122 }
123 
fireTimersInNestedEventLoop()124 void ThreadTimers::fireTimersInNestedEventLoop()
125 {
126     // Reset the reentrancy guard so the timers can fire again.
127     m_firingTimers = false;
128     updateSharedTimer();
129 }
130 
131 } // namespace WebCore
132 
133