2 * Copyright (C) 2006, 2008 Apple Inc. All rights reserved.
3 * Copyright (C) 2009 Google Inc. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
30 #include "SharedTimer.h"
31 #include "ThreadGlobalData.h"
32 #include "ThreadTimers.h"
36 #include <wtf/CurrentTime.h>
37 #include <wtf/HashSet.h>
38 #include <wtf/Vector.h>
44 class TimerHeapReference;
46 // Timers are stored in a heap data structure, used to implement a priority queue.
47 // This allows us to efficiently determine which timer needs to fire the soonest.
48 // Then we set a single shared system timer to fire at that time.
50 // When a timer's "next fire time" changes, we need to move it around in the priority queue.
52 // Simple accessors to thread-specific data.
53 static Vector<TimerBase*>& timerHeap()
55 return threadGlobalData().threadTimers().timerHeap();
60 class TimerHeapPointer {
62 TimerHeapPointer(TimerBase** pointer) : m_pointer(pointer) { }
63 TimerHeapReference operator*() const;
64 TimerBase* operator->() const { return *m_pointer; }
66 TimerBase** m_pointer;
69 class TimerHeapReference {
71 TimerHeapReference(TimerBase*& reference) : m_reference(reference) { }
72 operator TimerBase*() const { return m_reference; }
73 TimerHeapPointer operator&() const { return &m_reference; }
74 TimerHeapReference& operator=(TimerBase*);
75 TimerHeapReference& operator=(TimerHeapReference);
77 TimerBase*& m_reference;
80 inline TimerHeapReference TimerHeapPointer::operator*() const
85 inline TimerHeapReference& TimerHeapReference::operator=(TimerBase* timer)
88 Vector<TimerBase*>& heap = timerHeap();
89 if (&m_reference >= heap.data() && &m_reference < heap.data() + heap.size())
90 timer->m_heapIndex = &m_reference - heap.data();
94 inline TimerHeapReference& TimerHeapReference::operator=(TimerHeapReference b)
100 inline void swap(TimerHeapReference a, TimerHeapReference b)
102 TimerBase* timerA = a;
103 TimerBase* timerB = b;
105 // Invoke the assignment operator, since that takes care of updating m_heapIndex.
112 // Class to represent iterators in the heap when calling the standard library heap algorithms.
113 // Uses a custom pointer and reference type that update indices for pointers in the heap.
114 class TimerHeapIterator : public iterator<random_access_iterator_tag, TimerBase*, ptrdiff_t, TimerHeapPointer, TimerHeapReference> {
116 explicit TimerHeapIterator(TimerBase** pointer) : m_pointer(pointer) { checkConsistency(); }
118 TimerHeapIterator& operator++() { checkConsistency(); ++m_pointer; checkConsistency(); return *this; }
119 TimerHeapIterator operator++(int) { checkConsistency(1); return TimerHeapIterator(m_pointer++); }
121 TimerHeapIterator& operator--() { checkConsistency(); --m_pointer; checkConsistency(); return *this; }
122 TimerHeapIterator operator--(int) { checkConsistency(-1); return TimerHeapIterator(m_pointer--); }
124 TimerHeapIterator& operator+=(ptrdiff_t i) { checkConsistency(); m_pointer += i; checkConsistency(); return *this; }
125 TimerHeapIterator& operator-=(ptrdiff_t i) { checkConsistency(); m_pointer -= i; checkConsistency(); return *this; }
127 TimerHeapReference operator*() const { return TimerHeapReference(*m_pointer); }
128 TimerHeapReference operator[](ptrdiff_t i) const { return TimerHeapReference(m_pointer[i]); }
129 TimerBase* operator->() const { return *m_pointer; }
132 void checkConsistency(ptrdiff_t offset = 0) const
134 ASSERT(m_pointer >= timerHeap().data());
135 ASSERT(m_pointer <= timerHeap().data() + timerHeap().size());
136 ASSERT_UNUSED(offset, m_pointer + offset >= timerHeap().data());
137 ASSERT_UNUSED(offset, m_pointer + offset <= timerHeap().data() + timerHeap().size());
140 friend bool operator==(TimerHeapIterator, TimerHeapIterator);
141 friend bool operator!=(TimerHeapIterator, TimerHeapIterator);
142 friend bool operator<(TimerHeapIterator, TimerHeapIterator);
143 friend bool operator>(TimerHeapIterator, TimerHeapIterator);
144 friend bool operator<=(TimerHeapIterator, TimerHeapIterator);
145 friend bool operator>=(TimerHeapIterator, TimerHeapIterator);
147 friend TimerHeapIterator operator+(TimerHeapIterator, size_t);
148 friend TimerHeapIterator operator+(size_t, TimerHeapIterator);
150 friend TimerHeapIterator operator-(TimerHeapIterator, size_t);
151 friend ptrdiff_t operator-(TimerHeapIterator, TimerHeapIterator);
153 TimerBase** m_pointer;
156 inline bool operator==(TimerHeapIterator a, TimerHeapIterator b) { return a.m_pointer == b.m_pointer; }
157 inline bool operator!=(TimerHeapIterator a, TimerHeapIterator b) { return a.m_pointer != b.m_pointer; }
158 inline bool operator<(TimerHeapIterator a, TimerHeapIterator b) { return a.m_pointer < b.m_pointer; }
159 inline bool operator>(TimerHeapIterator a, TimerHeapIterator b) { return a.m_pointer > b.m_pointer; }
160 inline bool operator<=(TimerHeapIterator a, TimerHeapIterator b) { return a.m_pointer <= b.m_pointer; }
161 inline bool operator>=(TimerHeapIterator a, TimerHeapIterator b) { return a.m_pointer >= b.m_pointer; }
163 inline TimerHeapIterator operator+(TimerHeapIterator a, size_t b) { return TimerHeapIterator(a.m_pointer + b); }
164 inline TimerHeapIterator operator+(size_t a, TimerHeapIterator b) { return TimerHeapIterator(a + b.m_pointer); }
166 inline TimerHeapIterator operator-(TimerHeapIterator a, size_t b) { return TimerHeapIterator(a.m_pointer - b); }
167 inline ptrdiff_t operator-(TimerHeapIterator a, TimerHeapIterator b) { return a.m_pointer - b.m_pointer; }
171 class TimerHeapLessThanFunction {
173 bool operator()(TimerBase*, TimerBase*) const;
176 inline bool TimerHeapLessThanFunction::operator()(TimerBase* a, TimerBase* b) const
178 // The comparisons below are "backwards" because the heap puts the largest
179 // element first and we want the lowest time to be the first one in the heap.
180 double aFireTime = a->m_nextFireTime;
181 double bFireTime = b->m_nextFireTime;
182 if (bFireTime != aFireTime)
183 return bFireTime < aFireTime;
185 // We need to look at the difference of the insertion orders instead of comparing the two
186 // outright in case of overflow.
187 unsigned difference = a->m_heapInsertionOrder - b->m_heapInsertionOrder;
188 return difference < numeric_limits<unsigned>::max() / 2;
193 TimerBase::TimerBase()
195 , m_repeatInterval(0)
198 , m_thread(currentThread())
203 TimerBase::~TimerBase()
209 void TimerBase::start(double nextFireInterval, double repeatInterval)
211 ASSERT(m_thread == currentThread());
213 m_repeatInterval = repeatInterval;
214 setNextFireTime(monotonicallyIncreasingTime() + nextFireInterval);
217 void TimerBase::stop()
219 ASSERT(m_thread == currentThread());
221 m_repeatInterval = 0;
224 ASSERT(m_nextFireTime == 0);
225 ASSERT(m_repeatInterval == 0);
229 double TimerBase::nextFireInterval() const
232 double current = monotonicallyIncreasingTime();
233 if (m_nextFireTime < current)
235 return m_nextFireTime - current;
238 inline void TimerBase::checkHeapIndex() const
240 ASSERT(!timerHeap().isEmpty());
241 ASSERT(m_heapIndex >= 0);
242 ASSERT(m_heapIndex < static_cast<int>(timerHeap().size()));
243 ASSERT(timerHeap()[m_heapIndex] == this);
246 inline void TimerBase::checkConsistency() const
248 // Timers should be in the heap if and only if they have a non-zero next fire time.
249 ASSERT(inHeap() == (m_nextFireTime != 0));
254 void TimerBase::heapDecreaseKey()
256 ASSERT(m_nextFireTime != 0);
258 TimerBase** heapData = timerHeap().data();
259 push_heap(TimerHeapIterator(heapData), TimerHeapIterator(heapData + m_heapIndex + 1), TimerHeapLessThanFunction());
263 inline void TimerBase::heapDelete()
265 ASSERT(m_nextFireTime == 0);
267 timerHeap().removeLast();
271 void TimerBase::heapDeleteMin()
273 ASSERT(m_nextFireTime == 0);
275 timerHeap().removeLast();
279 inline void TimerBase::heapIncreaseKey()
281 ASSERT(m_nextFireTime != 0);
286 inline void TimerBase::heapInsert()
289 timerHeap().append(this);
290 m_heapIndex = timerHeap().size() - 1;
294 inline void TimerBase::heapPop()
296 // Temporarily force this timer to have the minimum key so we can pop it.
297 double fireTime = m_nextFireTime;
298 m_nextFireTime = -numeric_limits<double>::infinity();
301 m_nextFireTime = fireTime;
304 void TimerBase::heapPopMin()
306 ASSERT(this == timerHeap().first());
308 Vector<TimerBase*>& heap = timerHeap();
309 TimerBase** heapData = heap.data();
310 pop_heap(TimerHeapIterator(heapData), TimerHeapIterator(heapData + heap.size()), TimerHeapLessThanFunction());
312 ASSERT(this == timerHeap().last());
315 void TimerBase::setNextFireTime(double newTime)
317 ASSERT(m_thread == currentThread());
319 // Keep heap valid while changing the next-fire time.
320 double oldTime = m_nextFireTime;
321 if (oldTime != newTime) {
322 m_nextFireTime = newTime;
323 static unsigned currentHeapInsertionOrder;
324 m_heapInsertionOrder = currentHeapInsertionOrder++;
326 bool wasFirstTimerInHeap = m_heapIndex == 0;
330 else if (newTime == 0)
332 else if (newTime < oldTime)
337 bool isFirstTimerInHeap = m_heapIndex == 0;
339 if (wasFirstTimerInHeap || isFirstTimerInHeap)
340 threadGlobalData().threadTimers().updateSharedTimer();
346 void TimerBase::fireTimersInNestedEventLoop()
348 // Redirect to ThreadTimers.
349 threadGlobalData().threadTimers().fireTimersInNestedEventLoop();
352 } // namespace WebCore