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 <wtf/Noncopyable.h>
30#include <wtf/Threading.h>
31#include <wtf/Vector.h>
32
33namespace WebCore {
34
35// Time intervals are all in seconds.
36
37class TimerHeapElement;
38
39class TimerBase {
40    WTF_MAKE_NONCOPYABLE(TimerBase); WTF_MAKE_FAST_ALLOCATED;
41public:
42    TimerBase();
43    virtual ~TimerBase();
44
45    void start(double nextFireInterval, double repeatInterval);
46
47    void startRepeating(double repeatInterval) { start(repeatInterval, repeatInterval); }
48    void startOneShot(double interval) { start(interval, 0); }
49
50    void stop();
51    bool isActive() const;
52
53    double nextFireInterval() const;
54    double nextUnalignedFireInterval() const;
55    double repeatInterval() const { return m_repeatInterval; }
56
57    void augmentFireInterval(double delta) { setNextFireTime(m_nextFireTime + delta); }
58    void augmentRepeatInterval(double delta) { augmentFireInterval(delta); m_repeatInterval += delta; }
59
60    void didChangeAlignmentInterval();
61
62    static void fireTimersInNestedEventLoop();
63
64private:
65    virtual void fired() = 0;
66
67    virtual double alignedFireTime(double fireTime) const { return fireTime; }
68
69    void checkConsistency() const;
70    void checkHeapIndex() const;
71
72    void setNextFireTime(double);
73
74    bool inHeap() const { return m_heapIndex != -1; }
75
76    bool hasValidHeapPosition() const;
77    void updateHeapIfNeeded(double oldTime);
78
79    void heapDecreaseKey();
80    void heapDelete();
81    void heapDeleteMin();
82    void heapIncreaseKey();
83    void heapInsert();
84    void heapPop();
85    void heapPopMin();
86
87    Vector<TimerBase*>& timerHeap() const { ASSERT(m_cachedThreadGlobalTimerHeap); return *m_cachedThreadGlobalTimerHeap; }
88
89    double m_nextFireTime; // 0 if inactive
90    double m_unalignedNextFireTime; // m_nextFireTime not considering alignment interval
91    double m_repeatInterval; // 0 if not repeating
92    int m_heapIndex; // -1 if not in heap
93    unsigned m_heapInsertionOrder; // Used to keep order among equal-fire-time timers
94    Vector<TimerBase*>* m_cachedThreadGlobalTimerHeap;
95
96#ifndef NDEBUG
97    ThreadIdentifier m_thread;
98#endif
99
100    friend class ThreadTimers;
101    friend class TimerHeapLessThanFunction;
102    friend class TimerHeapReference;
103};
104
105template <typename TimerFiredClass> class Timer : public TimerBase {
106public:
107    typedef void (TimerFiredClass::*TimerFiredFunction)(Timer*);
108
109    Timer(TimerFiredClass* o, TimerFiredFunction f)
110        : m_object(o), m_function(f) { }
111
112private:
113    virtual void fired() { (m_object->*m_function)(this); }
114
115    TimerFiredClass* m_object;
116    TimerFiredFunction m_function;
117};
118
119inline bool TimerBase::isActive() const
120{
121    ASSERT(m_thread == currentThread());
122    return m_nextFireTime;
123}
124
125template <typename TimerFiredClass> class DeferrableOneShotTimer : protected TimerBase {
126public:
127    typedef void (TimerFiredClass::*TimerFiredFunction)(DeferrableOneShotTimer*);
128
129    DeferrableOneShotTimer(TimerFiredClass* o, TimerFiredFunction f, double delay)
130        : m_object(o)
131        , m_function(f)
132        , m_delay(delay)
133        , m_shouldRestartWhenTimerFires(false)
134    {
135    }
136
137    void restart()
138    {
139        // Setting this boolean is much more efficient than calling startOneShot
140        // again, which might result in rescheduling the system timer which
141        // can be quite expensive.
142
143        if (isActive()) {
144            m_shouldRestartWhenTimerFires = true;
145            return;
146        }
147        startOneShot(m_delay);
148    }
149
150    using TimerBase::stop;
151    using TimerBase::isActive;
152private:
153    virtual void fired()
154    {
155        if (m_shouldRestartWhenTimerFires) {
156            m_shouldRestartWhenTimerFires = false;
157            startOneShot(m_delay);
158            return;
159        }
160
161        (m_object->*m_function)(this);
162    }
163
164    TimerFiredClass* m_object;
165    TimerFiredFunction m_function;
166
167    double m_delay;
168    bool m_shouldRestartWhenTimerFires;
169};
170
171}
172
173#endif
174