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 <wtf/Noncopyable.h>
30 #include <wtf/Vector.h>
34 // Time intervals are all in seconds.
36 class TimerHeapElement
;
38 class TimerBase
: Noncopyable
{
43 void start(double nextFireInterval
, double repeatInterval
);
45 void startRepeating(double repeatInterval
) { start(repeatInterval
, repeatInterval
); }
46 void startOneShot(double interval
) { start(interval
, 0); }
49 bool isActive() const;
51 double nextFireInterval() const;
52 double repeatInterval() const { return m_repeatInterval
; }
54 void augmentRepeatInterval(double delta
) { setNextFireTime(m_nextFireTime
+ delta
); m_repeatInterval
+= delta
; }
56 static void fireTimersInNestedEventLoop();
59 virtual void fired() = 0;
61 void checkConsistency() const;
62 void checkHeapIndex() const;
64 void setNextFireTime(double);
66 bool inHeap() const { return m_heapIndex
!= -1; }
68 void heapDecreaseKey();
71 void heapIncreaseKey();
76 static void collectFiringTimers(double fireTime
, Vector
<TimerBase
*>&);
77 static void fireTimers(double fireTime
, const Vector
<TimerBase
*>&);
78 static void sharedTimerFired();
80 double m_nextFireTime
; // 0 if inactive
81 double m_repeatInterval
; // 0 if not repeating
82 int m_heapIndex
; // -1 if not in heap
83 unsigned m_heapInsertionOrder
; // Used to keep order among equal-fire-time timers
85 friend void updateSharedTimer();
86 friend void setDeferringTimers(bool);
87 friend class TimerHeapElement
;
88 friend bool operator<(const TimerHeapElement
&, const TimerHeapElement
&);
91 template <typename TimerFiredClass
> class Timer
: public TimerBase
{
93 typedef void (TimerFiredClass::*TimerFiredFunction
)(Timer
*);
95 Timer(TimerFiredClass
* o
, TimerFiredFunction f
)
96 : m_object(o
), m_function(f
) { }
99 virtual void fired() { (m_object
->*m_function
)(this); }
101 TimerFiredClass
* m_object
;
102 TimerFiredFunction m_function
;
105 // Set to true to prevent any timers from firing.
106 // When set back to false, timers that were deferred will fire.
107 bool isDeferringTimers();
108 void setDeferringTimers(bool);