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/WebTraceLocation.h"
32 #include "wtf/AddressSanitizer.h"
33 #include "wtf/Noncopyable.h"
34 #include "wtf/Threading.h"
35 #include "wtf/Vector.h"
39 // Time intervals are all in seconds.
41 class PLATFORM_EXPORT TimerBase
{
42 WTF_MAKE_NONCOPYABLE(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
);
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;
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
);
90 class CancellableTimerTask final
: public WebThread::Task
{
91 WTF_MAKE_NONCOPYABLE(CancellableTimerTask
);
93 explicit CancellableTimerTask(TimerBase
* timer
) : m_timer(timer
) { }
95 ~CancellableTimerTask() override
98 m_timer
->m_cancellableTimerTask
= nullptr;
101 NO_LAZY_SWEEP_SANITIZE_ADDRESS
105 m_timer
->m_cancellableTimerTask
= nullptr;
106 m_timer
->runInternal();
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.
128 ThreadIdentifier m_thread
;
131 friend class ThreadTimers
;
132 friend class TimerHeapLessThanFunction
;
133 friend class TimerHeapReference
;
136 template<typename T
, bool = IsGarbageCollectedType
<T
>::value
>
137 class TimerIsObjectAliveTrait
{
139 static bool isHeapObjectAlive(T
*) { return true; }
143 class TimerIsObjectAliveTrait
<T
, true> {
145 static bool isHeapObjectAlive(T
* objectPointer
)
147 return !Heap::willObjectBeLazilySwept(objectPointer
);
151 template <typename TimerFiredClass
>
152 class Timer
: public TimerBase
{
154 typedef void (TimerFiredClass::*TimerFiredFunction
)(Timer
*);
156 Timer(TimerFiredClass
* o
, TimerFiredFunction f
)
157 : m_object(o
), m_function(f
)
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
);
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
;