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 "platform/PlatformExport.h"
30 #include "platform/heap/Handle.h"
31 #include "public/platform/WebTaskRunner.h"
32 #include "public/platform/WebTraceLocation.h"
33 #include "wtf/AddressSanitizer.h"
34 #include "wtf/Noncopyable.h"
35 #include "wtf/Threading.h"
36 #include "wtf/Vector.h"
40 // Time intervals are all in seconds.
42 class PLATFORM_EXPORT TimerBase
{
43 WTF_MAKE_NONCOPYABLE(TimerBase
);
48 void start(double nextFireInterval
, double repeatInterval
, const WebTraceLocation
&);
50 void startRepeating(double repeatInterval
, const WebTraceLocation
& caller
)
52 start(repeatInterval
, repeatInterval
, caller
);
54 void startOneShot(double interval
, const WebTraceLocation
& caller
)
56 start(interval
, 0, caller
);
60 bool isActive() const;
61 const WebTraceLocation
& location() const { return m_location
; }
63 double nextFireInterval() const;
64 double nextUnalignedFireInterval() const;
65 double repeatInterval() const { return m_repeatInterval
; }
67 void augmentRepeatInterval(double delta
) {
68 double now
= monotonicallyIncreasingTime();
69 setNextFireTime(now
, m_nextFireTime
- now
+ delta
);
70 m_repeatInterval
+= delta
;
73 void didChangeAlignmentInterval(double now
);
75 struct PLATFORM_EXPORT Comparator
{
76 bool operator()(const TimerBase
* a
, const TimerBase
* b
) const;
80 virtual void fired() = 0;
82 NO_LAZY_SWEEP_SANITIZE_ADDRESS
83 virtual bool canFire() const { return true; }
85 virtual double alignedFireTime(double fireTime
) const { return fireTime
; }
87 void setNextFireTime(double now
, double delay
);
91 class CancellableTimerTask final
: public WebTaskRunner::Task
{
92 WTF_MAKE_NONCOPYABLE(CancellableTimerTask
);
94 explicit CancellableTimerTask(TimerBase
* timer
) : m_timer(timer
) { }
96 ~CancellableTimerTask() override
99 m_timer
->m_cancellableTimerTask
= nullptr;
102 NO_LAZY_SWEEP_SANITIZE_ADDRESS
106 m_timer
->m_cancellableTimerTask
= nullptr;
107 m_timer
->runInternal();
118 TimerBase
* m_timer
; // NOT OWNED
121 double m_nextFireTime
; // 0 if inactive
122 double m_unalignedNextFireTime
; // m_nextFireTime not considering alignment interval
123 double m_repeatInterval
; // 0 if not repeating
124 WebTraceLocation m_location
;
125 CancellableTimerTask
* m_cancellableTimerTask
; // NOT OWNED
126 WebScheduler
* m_webScheduler
; // Not owned.
129 ThreadIdentifier m_thread
;
132 friend class ThreadTimers
;
133 friend class TimerHeapLessThanFunction
;
134 friend class TimerHeapReference
;
137 template<typename T
, bool = IsGarbageCollectedType
<T
>::value
>
138 class TimerIsObjectAliveTrait
{
140 static bool isHeapObjectAlive(T
*) { return true; }
144 class TimerIsObjectAliveTrait
<T
, true> {
146 static bool isHeapObjectAlive(T
* objectPointer
)
148 return !Heap::willObjectBeLazilySwept(objectPointer
);
152 template <typename TimerFiredClass
>
153 class Timer
: public TimerBase
{
155 typedef void (TimerFiredClass::*TimerFiredFunction
)(Timer
*);
157 Timer(TimerFiredClass
* o
, TimerFiredFunction f
)
158 : m_object(o
), m_function(f
)
163 void fired() override
165 (m_object
->*m_function
)(this);
168 NO_LAZY_SWEEP_SANITIZE_ADDRESS
169 bool canFire() const override
171 // Oilpan: if a timer fires while Oilpan heaps are being lazily
172 // swept, it is not safe to proceed if the object is about to
173 // be swept (and this timer will be stopped while doing so.)
174 return TimerIsObjectAliveTrait
<TimerFiredClass
>::isHeapObjectAlive(m_object
);
178 // FIXME: Oilpan: TimerBase should be moved to the heap and m_object should be traced.
179 // This raw pointer is safe as long as Timer<X> is held by the X itself (That's the case
180 // in the current code base).
181 GC_PLUGIN_IGNORE("363031")
182 TimerFiredClass
* m_object
;
183 TimerFiredFunction m_function
;
186 NO_LAZY_SWEEP_SANITIZE_ADDRESS
187 inline bool TimerBase::isActive() const
189 ASSERT(m_thread
== currentThread());
190 return m_cancellableTimerTask
;