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_unalignedNextFireTime(0)
196
, m_repeatInterval(0)
199
, m_thread(currentThread())
204
TimerBase::~TimerBase()
210
void TimerBase::start(double nextFireInterval, double repeatInterval)
212
ASSERT(m_thread == currentThread());
214
m_repeatInterval = repeatInterval;
215
setNextFireTime(monotonicallyIncreasingTime() + nextFireInterval);
218
void TimerBase::stop()
220
ASSERT(m_thread == currentThread());
222
m_repeatInterval = 0;
225
ASSERT(m_nextFireTime == 0);
226
ASSERT(m_repeatInterval == 0);
230
double TimerBase::nextFireInterval() const
233
double current = monotonicallyIncreasingTime();
234
if (m_nextFireTime < current)
236
return m_nextFireTime - current;
239
inline void TimerBase::checkHeapIndex() const
241
ASSERT(!timerHeap().isEmpty());
242
ASSERT(m_heapIndex >= 0);
243
ASSERT(m_heapIndex < static_cast<int>(timerHeap().size()));
244
ASSERT(timerHeap()[m_heapIndex] == this);
247
inline void TimerBase::checkConsistency() const
249
// Timers should be in the heap if and only if they have a non-zero next fire time.
250
ASSERT(inHeap() == (m_nextFireTime != 0));
255
void TimerBase::heapDecreaseKey()
257
ASSERT(m_nextFireTime != 0);
259
TimerBase** heapData = timerHeap().data();
260
push_heap(TimerHeapIterator(heapData), TimerHeapIterator(heapData + m_heapIndex + 1), TimerHeapLessThanFunction());
264
inline void TimerBase::heapDelete()
266
ASSERT(m_nextFireTime == 0);
268
timerHeap().removeLast();
272
void TimerBase::heapDeleteMin()
274
ASSERT(m_nextFireTime == 0);
276
timerHeap().removeLast();
280
inline void TimerBase::heapIncreaseKey()
282
ASSERT(m_nextFireTime != 0);
287
inline void TimerBase::heapInsert()
290
timerHeap().append(this);
291
m_heapIndex = timerHeap().size() - 1;
295
inline void TimerBase::heapPop()
297
// Temporarily force this timer to have the minimum key so we can pop it.
298
double fireTime = m_nextFireTime;
299
m_nextFireTime = -numeric_limits<double>::infinity();
302
m_nextFireTime = fireTime;
305
void TimerBase::heapPopMin()
307
ASSERT(this == timerHeap().first());
309
Vector<TimerBase*>& heap = timerHeap();
310
TimerBase** heapData = heap.data();
311
pop_heap(TimerHeapIterator(heapData), TimerHeapIterator(heapData + heap.size()), TimerHeapLessThanFunction());
313
ASSERT(this == timerHeap().last());
316
void TimerBase::setNextFireTime(double newUnalignedTime)
318
ASSERT(m_thread == currentThread());
320
if (m_unalignedNextFireTime != newUnalignedTime)
321
m_unalignedNextFireTime = newUnalignedTime;
323
// Keep heap valid while changing the next-fire time.
324
double oldTime = m_nextFireTime;
325
double newTime = alignedFireTime(newUnalignedTime);
326
if (oldTime != newTime) {
327
m_nextFireTime = newTime;
328
static unsigned currentHeapInsertionOrder;
329
m_heapInsertionOrder = currentHeapInsertionOrder++;
331
bool wasFirstTimerInHeap = m_heapIndex == 0;
335
else if (newTime == 0)
337
else if (newTime < oldTime)
342
bool isFirstTimerInHeap = m_heapIndex == 0;
344
if (wasFirstTimerInHeap || isFirstTimerInHeap)
345
threadGlobalData().threadTimers().updateSharedTimer();
351
void TimerBase::fireTimersInNestedEventLoop()
353
// Redirect to ThreadTimers.
354
threadGlobalData().threadTimers().fireTimersInNestedEventLoop();
357
void TimerBase::didChangeAlignmentInterval()
359
setNextFireTime(m_unalignedNextFireTime);
362
double TimerBase::nextUnalignedFireInterval() const
365
return max(m_unalignedNextFireTime - monotonicallyIncreasingTime(), 0.0);
368
} // namespace WebCore