1 /* 2 * Copyright (C) 2006 Apple Computer, Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 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. 12 * 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. 24 */ 25 26 #ifndef Timer_h 27 #define Timer_h 28 29 #include "platform/PlatformExport.h" 30 #include "wtf/Noncopyable.h" 31 #include "wtf/Threading.h" 32 #include "wtf/Vector.h" 33 34 namespace WebCore { 35 36 // Time intervals are all in seconds. 37 38 class TimerHeapElement; 39 40 class PLATFORM_EXPORT TimerBase { 41 WTF_MAKE_NONCOPYABLE(TimerBase); WTF_MAKE_FAST_ALLOCATED; 42 public: 43 TimerBase(); 44 virtual ~TimerBase(); 45 46 void start(double nextFireInterval, double repeatInterval); 47 48 void startRepeating(double repeatInterval) { start(repeatInterval, repeatInterval); } 49 void startOneShot(double interval) { start(interval, 0); } 50 51 void stop(); 52 bool isActive() const; 53 54 double nextFireInterval() const; 55 double nextUnalignedFireInterval() const; 56 double repeatInterval() const { return m_repeatInterval; } 57 58 void augmentRepeatInterval(double delta) { 59 setNextFireTime(m_nextFireTime + delta); 60 m_repeatInterval += delta; 61 } 62 63 void didChangeAlignmentInterval(); 64 65 static void fireTimersInNestedEventLoop(); 66 67 private: 68 virtual void fired() = 0; 69 70 virtual double alignedFireTime(double fireTime) const { return fireTime; } 71 72 void checkConsistency() const; 73 void checkHeapIndex() const; 74 75 void setNextFireTime(double); 76 77 bool inHeap() const { return m_heapIndex != -1; } 78 79 bool hasValidHeapPosition() const; 80 void updateHeapIfNeeded(double oldTime); 81 82 void heapDecreaseKey(); 83 void heapDelete(); 84 void heapDeleteMin(); 85 void heapIncreaseKey(); 86 void heapInsert(); 87 void heapPop(); 88 void heapPopMin(); 89 90 Vector<TimerBase*>& timerHeap() const { ASSERT(m_cachedThreadGlobalTimerHeap); return *m_cachedThreadGlobalTimerHeap; } 91 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; 98 99 #ifndef NDEBUG 100 ThreadIdentifier m_thread; 101 #endif 102 103 friend class ThreadTimers; 104 friend class TimerHeapLessThanFunction; 105 friend class TimerHeapReference; 106 }; 107 108 template <typename TimerFiredClass> 109 class Timer : public TimerBase { 110 public: 111 typedef void (TimerFiredClass::*TimerFiredFunction)(Timer*); 112 113 Timer(TimerFiredClass* o, TimerFiredFunction f) 114 : m_object(o), m_function(f) { } 115 116 private: 117 virtual void fired() { (m_object->*m_function)(this); } 118 119 TimerFiredClass* m_object; 120 TimerFiredFunction m_function; 121 }; 122 123 inline bool TimerBase::isActive() const 124 { 125 ASSERT(m_thread == currentThread()); 126 return m_nextFireTime; 127 } 128 129 template <typename TimerFiredClass> 130 class DeferrableOneShotTimer : private TimerBase { 131 public: 132 typedef void (TimerFiredClass::*TimerFiredFunction)(DeferrableOneShotTimer*); 133 134 DeferrableOneShotTimer(TimerFiredClass* o, TimerFiredFunction f, double delay) 135 : m_object(o) 136 , m_function(f) 137 , m_delay(delay) 138 , m_shouldRestartWhenTimerFires(false) 139 { 140 } 141 142 void restart() 143 { 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. 147 148 if (isActive()) { 149 m_shouldRestartWhenTimerFires = true; 150 return; 151 } 152 startOneShot(m_delay); 153 } 154 155 using TimerBase::stop; 156 using TimerBase::isActive; 157 158 private: 159 virtual void fired() 160 { 161 if (m_shouldRestartWhenTimerFires) { 162 m_shouldRestartWhenTimerFires = false; 163 startOneShot(m_delay); 164 return; 165 } 166 167 (m_object->*m_function)(this); 168 } 169 170 TimerFiredClass* m_object; 171 TimerFiredFunction m_function; 172 173 double m_delay; 174 bool m_shouldRestartWhenTimerFires; 175 }; 176 177 } 178 179 #endif 180