2 * Copyright (C) 2006 Apple Computer, Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include "platform/PlatformExport.h"
30 #include "wtf/Noncopyable.h"
31 #include "wtf/Threading.h"
32 #include "wtf/Vector.h"
36 // Time intervals are all in seconds.
38 class TimerHeapElement;
40 class PLATFORM_EXPORT TimerBase {
41 WTF_MAKE_NONCOPYABLE(TimerBase); WTF_MAKE_FAST_ALLOCATED;
46 void start(double nextFireInterval, double repeatInterval);
48 void startRepeating(double repeatInterval) { start(repeatInterval, repeatInterval); }
49 void startOneShot(double interval) { start(interval, 0); }
52 bool isActive() const;
54 double nextFireInterval() const;
55 double nextUnalignedFireInterval() const;
56 double repeatInterval() const { return m_repeatInterval; }
58 void augmentRepeatInterval(double delta) {
59 setNextFireTime(m_nextFireTime + delta);
60 m_repeatInterval += delta;
63 void didChangeAlignmentInterval();
65 static void fireTimersInNestedEventLoop();
68 virtual void fired() = 0;
70 virtual double alignedFireTime(double fireTime) const { return fireTime; }
72 void checkConsistency() const;
73 void checkHeapIndex() const;
75 void setNextFireTime(double);
77 bool inHeap() const { return m_heapIndex != -1; }
79 bool hasValidHeapPosition() const;
80 void updateHeapIfNeeded(double oldTime);
82 void heapDecreaseKey();
85 void heapIncreaseKey();
90 Vector<TimerBase*>& timerHeap() const { ASSERT(m_cachedThreadGlobalTimerHeap); return *m_cachedThreadGlobalTimerHeap; }
92 double m_nextFireTime; // 0 if inactive
93 double m_unalignedNextFireTime; // m_nextFireTime not considering alignment interval
94 double m_repeatInterval; // 0 if not repeating
95 int m_heapIndex; // -1 if not in heap
96 unsigned m_heapInsertionOrder; // Used to keep order among equal-fire-time timers
97 Vector<TimerBase*>* m_cachedThreadGlobalTimerHeap;
100 ThreadIdentifier m_thread;
103 friend class ThreadTimers;
104 friend class TimerHeapLessThanFunction;
105 friend class TimerHeapReference;
108 template <typename TimerFiredClass>
109 class Timer FINAL : public TimerBase {
111 typedef void (TimerFiredClass::*TimerFiredFunction)(Timer*);
113 Timer(TimerFiredClass* o, TimerFiredFunction f)
114 : m_object(o), m_function(f) { }
117 virtual void fired() OVERRIDE { (m_object->*m_function)(this); }
119 TimerFiredClass* m_object;
120 TimerFiredFunction m_function;
123 inline bool TimerBase::isActive() const
125 ASSERT(m_thread == currentThread());
126 return m_nextFireTime;
129 template <typename TimerFiredClass>
130 class DeferrableOneShotTimer FINAL : private TimerBase {
132 typedef void (TimerFiredClass::*TimerFiredFunction)(DeferrableOneShotTimer*);
134 DeferrableOneShotTimer(TimerFiredClass* o, TimerFiredFunction f, double delay)
138 , m_shouldRestartWhenTimerFires(false)
144 // Setting this boolean is much more efficient than calling startOneShot
145 // again, which might result in rescheduling the system timer which
146 // can be quite expensive.
149 m_shouldRestartWhenTimerFires = true;
152 startOneShot(m_delay);
155 using TimerBase::stop;
156 using TimerBase::isActive;
159 virtual void fired() OVERRIDE
161 if (m_shouldRestartWhenTimerFires) {
162 m_shouldRestartWhenTimerFires = false;
163 startOneShot(m_delay);
167 (m_object->*m_function)(this);
170 TimerFiredClass* m_object;
171 TimerFiredFunction m_function;
174 bool m_shouldRestartWhenTimerFires;