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