• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // Timers are created, started and fired on the same thread, and each thread has its own ThreadTimers
38 // copy to keep the heap and a set of currently firing timers.
39 
mainThreadSharedTimer()40 static MainThreadSharedTimer* mainThreadSharedTimer()
41 {
42     static MainThreadSharedTimer* timer = new MainThreadSharedTimer;
43     return timer;
44 }
45 
ThreadTimers()46 ThreadTimers::ThreadTimers()
47     : m_sharedTimer(0)
48     , m_firingTimers(false)
49 {
50     if (isMainThread())
51         setSharedTimer(mainThreadSharedTimer());
52 }
53 
54 // A worker thread may initialize SharedTimer after some timers are created.
55 // Also, SharedTimer can be replaced with 0 before all timers are destroyed.
setSharedTimer(SharedTimer * sharedTimer)56 void ThreadTimers::setSharedTimer(SharedTimer* sharedTimer)
57 {
58     if (m_sharedTimer) {
59         m_sharedTimer->setFiredFunction(0);
60         m_sharedTimer->stop();
61     }
62 
63     m_sharedTimer = sharedTimer;
64 
65     if (sharedTimer) {
66         m_sharedTimer->setFiredFunction(ThreadTimers::sharedTimerFired);
67         updateSharedTimer();
68     }
69 }
70 
updateSharedTimer()71 void ThreadTimers::updateSharedTimer()
72 {
73     if (!m_sharedTimer)
74         return;
75 
76     if (m_firingTimers || m_timerHeap.isEmpty())
77         m_sharedTimer->stop();
78     else
79         m_sharedTimer->setFireTime(m_timerHeap.first()->m_nextFireTime);
80 }
81 
82 
collectFiringTimers(double fireTime,Vector<TimerBase * > & firingTimers)83 void ThreadTimers::collectFiringTimers(double fireTime, Vector<TimerBase*>& firingTimers)
84 {
85     while (!m_timerHeap.isEmpty() && m_timerHeap.first()->m_nextFireTime <= fireTime) {
86         TimerBase* timer = m_timerHeap.first();
87         firingTimers.append(timer);
88         m_timersReadyToFire.add(timer);
89         timer->m_nextFireTime = 0;
90         timer->heapDeleteMin();
91     }
92 }
93 
fireTimers(double fireTime,const Vector<TimerBase * > & firingTimers)94 void ThreadTimers::fireTimers(double fireTime, const Vector<TimerBase*>& firingTimers)
95 {
96     size_t size = firingTimers.size();
97     for (size_t i = 0; i != size; ++i) {
98         TimerBase* timer = firingTimers[i];
99 
100         // If not in the set, this timer has been deleted or re-scheduled in another timer's fired function.
101         // So either we don't want to fire it at all or we will fire it next time the shared timer goes off.
102         // It might even have been deleted; that's OK because we won't do anything else with the pointer.
103         if (!m_timersReadyToFire.contains(timer))
104             continue;
105 
106         // Setting the next fire time has a side effect of removing the timer from the firing timers set.
107         double interval = timer->repeatInterval();
108         timer->setNextFireTime(interval ? fireTime + interval : 0);
109 
110         // Once the timer has been fired, it may be deleted, so do nothing else with it after this point.
111         timer->fired();
112 
113         // Catch the case where the timer asked timers to fire in a nested event loop.
114         if (!m_firingTimers)
115             break;
116     }
117 }
118 
sharedTimerFired()119 void ThreadTimers::sharedTimerFired()
120 {
121     // Redirect to non-static method.
122     threadGlobalData().threadTimers().sharedTimerFiredInternal();
123 }
124 
sharedTimerFiredInternal()125 void ThreadTimers::sharedTimerFiredInternal()
126 {
127     // Do a re-entrancy check.
128     if (m_firingTimers)
129         return;
130     m_firingTimers = true;
131 
132     double fireTime = currentTime();
133     Vector<TimerBase*> firingTimers;
134 
135     // m_timersReadyToFire will initially contain the same set as firingTimers, but
136     // as timers fire some mat become re-scheduled or deleted. They get removed from
137     // m_timersReadyToFire so we can avoid firing them.
138     ASSERT(m_timersReadyToFire.isEmpty());
139 
140     collectFiringTimers(fireTime, firingTimers);
141     fireTimers(fireTime, firingTimers);
142 
143     m_timersReadyToFire.clear();
144     m_firingTimers = false;
145 
146     updateSharedTimer();
147 }
148 
fireTimersInNestedEventLoop()149 void ThreadTimers::fireTimersInNestedEventLoop()
150 {
151     // Reset the reentrancy guard so the timers can fire again.
152     m_firingTimers = false;
153     m_timersReadyToFire.clear();
154     updateSharedTimer();
155 }
156 
157 } // namespace WebCore
158 
159