Maintain timer ordering when alignment is used
[chromium-blink-merge.git] / third_party / WebKit / Source / platform / Timer.h
blobec2621b0508cb0db7ca3e7addab133e2fc2ec257
1 /*
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
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.
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.
26 #ifndef Timer_h
27 #define Timer_h
29 #include "platform/PlatformExport.h"
30 #include "platform/heap/Handle.h"
31 #include "public/platform/WebTraceLocation.h"
32 #include "wtf/AddressSanitizer.h"
33 #include "wtf/Noncopyable.h"
34 #include "wtf/Threading.h"
35 #include "wtf/Vector.h"
37 namespace blink {
39 // Time intervals are all in seconds.
41 class PLATFORM_EXPORT TimerBase {
42 WTF_MAKE_NONCOPYABLE(TimerBase);
43 public:
44 TimerBase();
45 virtual ~TimerBase();
47 void start(double nextFireInterval, double repeatInterval, const WebTraceLocation&);
49 void startRepeating(double repeatInterval, const WebTraceLocation& caller)
51 start(repeatInterval, repeatInterval, caller);
53 void startOneShot(double interval, const WebTraceLocation& caller)
55 start(interval, 0, caller);
58 void stop();
59 bool isActive() const;
60 const WebTraceLocation& location() const { return m_location; }
62 double nextFireInterval() const;
63 double nextUnalignedFireInterval() const;
64 double repeatInterval() const { return m_repeatInterval; }
66 void augmentRepeatInterval(double delta) {
67 double now = monotonicallyIncreasingTime();
68 setNextFireTime(now, m_nextFireTime - now + delta);
69 m_repeatInterval += delta;
72 void didChangeAlignmentInterval(double now);
74 struct PLATFORM_EXPORT Comparator {
75 bool operator()(const TimerBase* a, const TimerBase* b) const;
78 private:
79 virtual void fired() = 0;
81 NO_LAZY_SWEEP_SANITIZE_ADDRESS
82 virtual bool canFire() const { return true; }
84 virtual double alignedFireTime(double fireTime) const { return fireTime; }
86 void setNextFireTime(double now, double delay);
88 void runInternal();
90 class CancellableTimerTask final : public WebThread::Task {
91 WTF_MAKE_NONCOPYABLE(CancellableTimerTask);
92 public:
93 explicit CancellableTimerTask(TimerBase* timer) : m_timer(timer) { }
95 ~CancellableTimerTask() override
97 if (m_timer)
98 m_timer->m_cancellableTimerTask = nullptr;
101 NO_LAZY_SWEEP_SANITIZE_ADDRESS
102 void run() override
104 if (m_timer) {
105 m_timer->m_cancellableTimerTask = nullptr;
106 m_timer->runInternal();
107 m_timer = nullptr;
111 void cancel()
113 m_timer = nullptr;
116 private:
117 TimerBase* m_timer; // NOT OWNED
120 double m_nextFireTime; // 0 if inactive
121 double m_unalignedNextFireTime; // m_nextFireTime not considering alignment interval
122 double m_repeatInterval; // 0 if not repeating
123 WebTraceLocation m_location;
124 CancellableTimerTask* m_cancellableTimerTask; // NOT OWNED
125 WebScheduler* m_webScheduler; // Not owned.
127 #if ENABLE(ASSERT)
128 ThreadIdentifier m_thread;
129 #endif
131 friend class ThreadTimers;
132 friend class TimerHeapLessThanFunction;
133 friend class TimerHeapReference;
136 template<typename T, bool = IsGarbageCollectedType<T>::value>
137 class TimerIsObjectAliveTrait {
138 public:
139 static bool isHeapObjectAlive(T*) { return true; }
142 template<typename T>
143 class TimerIsObjectAliveTrait<T, true> {
144 public:
145 static bool isHeapObjectAlive(T* objectPointer)
147 return !Heap::willObjectBeLazilySwept(objectPointer);
151 template <typename TimerFiredClass>
152 class Timer : public TimerBase {
153 public:
154 typedef void (TimerFiredClass::*TimerFiredFunction)(Timer*);
156 Timer(TimerFiredClass* o, TimerFiredFunction f)
157 : m_object(o), m_function(f)
161 protected:
162 void fired() override
164 (m_object->*m_function)(this);
167 NO_LAZY_SWEEP_SANITIZE_ADDRESS
168 bool canFire() const override
170 // Oilpan: if a timer fires while Oilpan heaps are being lazily
171 // swept, it is not safe to proceed if the object is about to
172 // be swept (and this timer will be stopped while doing so.)
173 return TimerIsObjectAliveTrait<TimerFiredClass>::isHeapObjectAlive(m_object);
176 private:
177 // FIXME: Oilpan: TimerBase should be moved to the heap and m_object should be traced.
178 // This raw pointer is safe as long as Timer<X> is held by the X itself (That's the case
179 // in the current code base).
180 GC_PLUGIN_IGNORE("363031")
181 TimerFiredClass* m_object;
182 TimerFiredFunction m_function;
185 NO_LAZY_SWEEP_SANITIZE_ADDRESS
186 inline bool TimerBase::isActive() const
188 ASSERT(m_thread == currentThread());
189 return m_cancellableTimerTask;
192 } // namespace blink
194 #endif // Timer_h