OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2006 Apple Computer, Inc. All rights reserved. | 2 * Copyright (C) 2006 Apple Computer, Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
6 * are met: | 6 * are met: |
7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
(...skipping 18 matching lines...) Expand all Loading... |
29 #include "platform/PlatformExport.h" | 29 #include "platform/PlatformExport.h" |
30 #include "platform/heap/Handle.h" | 30 #include "platform/heap/Handle.h" |
31 #include "public/platform/WebTaskRunner.h" | 31 #include "public/platform/WebTaskRunner.h" |
32 #include "public/platform/WebTraceLocation.h" | 32 #include "public/platform/WebTraceLocation.h" |
33 #include "wtf/AddressSanitizer.h" | 33 #include "wtf/AddressSanitizer.h" |
34 #include "wtf/Allocator.h" | 34 #include "wtf/Allocator.h" |
35 #include "wtf/CurrentTime.h" | 35 #include "wtf/CurrentTime.h" |
36 #include "wtf/Noncopyable.h" | 36 #include "wtf/Noncopyable.h" |
37 #include "wtf/Threading.h" | 37 #include "wtf/Threading.h" |
38 #include "wtf/Vector.h" | 38 #include "wtf/Vector.h" |
| 39 #include "wtf/WeakPtr.h" |
39 | 40 |
40 namespace blink { | 41 namespace blink { |
41 | 42 |
42 // Time intervals are all in seconds. | 43 // Time intervals are all in seconds. |
43 | 44 |
44 class PLATFORM_EXPORT TimerBase { | 45 class PLATFORM_EXPORT TimerBase { |
45 WTF_MAKE_NONCOPYABLE(TimerBase); | 46 WTF_MAKE_NONCOPYABLE(TimerBase); |
46 public: | 47 public: |
47 explicit TimerBase(WebTaskRunner*); | 48 explicit TimerBase(WebTaskRunner*); |
48 virtual ~TimerBase(); | 49 virtual ~TimerBase(); |
49 | 50 |
50 void start(double nextFireInterval, double repeatInterval, const WebTraceLoc
ation&); | 51 void start(double nextFireInterval, double repeatInterval, const WebTraceLoc
ation&); |
51 | 52 |
52 void startRepeating(double repeatInterval, const WebTraceLocation& caller) | 53 void startRepeating(double repeatInterval, const WebTraceLocation& caller) |
53 { | 54 { |
54 start(repeatInterval, repeatInterval, caller); | 55 start(repeatInterval, repeatInterval, caller); |
55 } | 56 } |
56 void startOneShot(double interval, const WebTraceLocation& caller) | 57 void startOneShot(double interval, const WebTraceLocation& caller) |
57 { | 58 { |
58 start(interval, 0, caller); | 59 start(interval, 0, caller); |
59 } | 60 } |
60 | 61 |
61 // Timer cancellation is supported but not free. Please be careful not to | 62 // Timer cancellation is fast enough that you shouldn't have to worry |
62 // cause a flood of timer cancellations. | 63 // about it unless you're canceling tens of thousands of tasks. |
63 void stop(); | 64 void stop(); |
64 bool isActive() const; | 65 bool isActive() const; |
65 const WebTraceLocation& location() const { return m_location; } | 66 const WebTraceLocation& location() const { return m_location; } |
66 | 67 |
67 double nextFireInterval() const; | 68 double nextFireInterval() const; |
68 double repeatInterval() const { return m_repeatInterval; } | 69 double repeatInterval() const { return m_repeatInterval; } |
69 | 70 |
70 void augmentRepeatInterval(double delta) { | 71 void augmentRepeatInterval(double delta) { |
71 double now = timerMonotonicallyIncreasingTime(); | 72 double now = timerMonotonicallyIncreasingTime(); |
72 setNextFireTime(now, std::max(m_nextFireTime - now + delta, 0.0)); | 73 setNextFireTime(now, std::max(m_nextFireTime - now + delta, 0.0)); |
(...skipping 15 matching lines...) Expand all Loading... |
88 | 89 |
89 NO_LAZY_SWEEP_SANITIZE_ADDRESS | 90 NO_LAZY_SWEEP_SANITIZE_ADDRESS |
90 virtual bool canFire() const { return true; } | 91 virtual bool canFire() const { return true; } |
91 | 92 |
92 double timerMonotonicallyIncreasingTime() const; | 93 double timerMonotonicallyIncreasingTime() const; |
93 | 94 |
94 void setNextFireTime(double now, double delay); | 95 void setNextFireTime(double now, double delay); |
95 | 96 |
96 void runInternal(); | 97 void runInternal(); |
97 | 98 |
98 class CancellableTimerTask final : public WebTaskRunner::Task { | |
99 WTF_MAKE_NONCOPYABLE(CancellableTimerTask); | |
100 public: | |
101 explicit CancellableTimerTask(TimerBase* timer) : m_timer(timer) { } | |
102 | |
103 NO_LAZY_SWEEP_SANITIZE_ADDRESS | |
104 ~CancellableTimerTask() override | |
105 { | |
106 if (m_timer) | |
107 m_timer->m_cancellableTimerTask = nullptr; | |
108 } | |
109 | |
110 NO_LAZY_SWEEP_SANITIZE_ADDRESS | |
111 void run() override | |
112 { | |
113 if (m_timer) { | |
114 m_timer->m_cancellableTimerTask = nullptr; | |
115 m_timer->runInternal(); | |
116 m_timer = nullptr; | |
117 } | |
118 } | |
119 | |
120 void cancel() | |
121 { | |
122 m_timer = nullptr; | |
123 } | |
124 | |
125 private: | |
126 TimerBase* m_timer; // NOT OWNED | |
127 }; | |
128 | |
129 double m_nextFireTime; // 0 if inactive | 99 double m_nextFireTime; // 0 if inactive |
130 double m_repeatInterval; // 0 if not repeating | 100 double m_repeatInterval; // 0 if not repeating |
131 WebTraceLocation m_location; | 101 WebTraceLocation m_location; |
132 CancellableTimerTask* m_cancellableTimerTask; // NOT OWNED | |
133 std::unique_ptr<WebTaskRunner> m_webTaskRunner; | 102 std::unique_ptr<WebTaskRunner> m_webTaskRunner; |
134 | 103 |
135 #if DCHECK_IS_ON() | 104 #if DCHECK_IS_ON() |
136 ThreadIdentifier m_thread; | 105 ThreadIdentifier m_thread; |
137 #endif | 106 #endif |
| 107 WTF::WeakPtrFactory<TimerBase> m_weakPtrFactory; |
138 | 108 |
139 friend class ThreadTimers; | 109 friend class ThreadTimers; |
140 friend class TimerHeapLessThanFunction; | 110 friend class TimerHeapLessThanFunction; |
141 friend class TimerHeapReference; | 111 friend class TimerHeapReference; |
142 }; | 112 }; |
143 | 113 |
144 template<typename T, bool = IsGarbageCollectedType<T>::value> | 114 template<typename T, bool = IsGarbageCollectedType<T>::value> |
145 class TimerIsObjectAliveTrait { | 115 class TimerIsObjectAliveTrait { |
146 public: | 116 public: |
147 static bool isHeapObjectAlive(T*) { return true; } | 117 static bool isHeapObjectAlive(T*) { return true; } |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
220 UnthrottledThreadTimer(TimerFiredClass* timerFiredClass, TimerFiredFunction
timerFiredFunction) | 190 UnthrottledThreadTimer(TimerFiredClass* timerFiredClass, TimerFiredFunction
timerFiredFunction) |
221 : TaskRunnerTimer<TimerFiredClass>(TimerBase::getUnthrottledTaskRunner()
, timerFiredClass, timerFiredFunction) | 191 : TaskRunnerTimer<TimerFiredClass>(TimerBase::getUnthrottledTaskRunner()
, timerFiredClass, timerFiredFunction) |
222 { | 192 { |
223 } | 193 } |
224 }; | 194 }; |
225 | 195 |
226 NO_LAZY_SWEEP_SANITIZE_ADDRESS | 196 NO_LAZY_SWEEP_SANITIZE_ADDRESS |
227 inline bool TimerBase::isActive() const | 197 inline bool TimerBase::isActive() const |
228 { | 198 { |
229 ASSERT(m_thread == currentThread()); | 199 ASSERT(m_thread == currentThread()); |
230 return m_cancellableTimerTask; | 200 return m_weakPtrFactory.hasWeakPtrs(); |
231 } | 201 } |
232 | 202 |
233 } // namespace blink | 203 } // namespace blink |
234 | 204 |
235 #endif // Timer_h | 205 #endif // Timer_h |
OLD | NEW |