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" | |
40 | 39 |
41 namespace blink { | 40 namespace blink { |
42 | 41 |
43 // Time intervals are all in seconds. | 42 // Time intervals are all in seconds. |
44 | 43 |
45 class PLATFORM_EXPORT TimerBase { | 44 class PLATFORM_EXPORT TimerBase { |
46 WTF_MAKE_NONCOPYABLE(TimerBase); | 45 WTF_MAKE_NONCOPYABLE(TimerBase); |
47 public: | 46 public: |
48 explicit TimerBase(WebTaskRunner*); | 47 explicit TimerBase(WebTaskRunner*); |
49 virtual ~TimerBase(); | 48 virtual ~TimerBase(); |
50 | 49 |
51 void start(double nextFireInterval, double repeatInterval, const WebTraceLoc
ation&); | 50 void start(double nextFireInterval, double repeatInterval, const WebTraceLoc
ation&); |
52 | 51 |
53 void startRepeating(double repeatInterval, const WebTraceLocation& caller) | 52 void startRepeating(double repeatInterval, const WebTraceLocation& caller) |
54 { | 53 { |
55 start(repeatInterval, repeatInterval, caller); | 54 start(repeatInterval, repeatInterval, caller); |
56 } | 55 } |
57 void startOneShot(double interval, const WebTraceLocation& caller) | 56 void startOneShot(double interval, const WebTraceLocation& caller) |
58 { | 57 { |
59 start(interval, 0, caller); | 58 start(interval, 0, caller); |
60 } | 59 } |
61 | 60 |
62 // Timer cancellation is fast enough that you shouldn't have to worry | 61 // Timer cancellation is supported but not free. Please be careful not to |
63 // about it unless you're canceling tens of thousands of tasks. | 62 // cause a flood of timer cancellations. |
64 void stop(); | 63 void stop(); |
65 bool isActive() const; | 64 bool isActive() const; |
66 const WebTraceLocation& location() const { return m_location; } | 65 const WebTraceLocation& location() const { return m_location; } |
67 | 66 |
68 double nextFireInterval() const; | 67 double nextFireInterval() const; |
69 double repeatInterval() const { return m_repeatInterval; } | 68 double repeatInterval() const { return m_repeatInterval; } |
70 | 69 |
71 void augmentRepeatInterval(double delta) { | 70 void augmentRepeatInterval(double delta) { |
72 double now = timerMonotonicallyIncreasingTime(); | 71 double now = timerMonotonicallyIncreasingTime(); |
73 setNextFireTime(now, std::max(m_nextFireTime - now + delta, 0.0)); | 72 setNextFireTime(now, std::max(m_nextFireTime - now + delta, 0.0)); |
(...skipping 15 matching lines...) Expand all Loading... |
89 | 88 |
90 NO_LAZY_SWEEP_SANITIZE_ADDRESS | 89 NO_LAZY_SWEEP_SANITIZE_ADDRESS |
91 virtual bool canFire() const { return true; } | 90 virtual bool canFire() const { return true; } |
92 | 91 |
93 double timerMonotonicallyIncreasingTime() const; | 92 double timerMonotonicallyIncreasingTime() const; |
94 | 93 |
95 void setNextFireTime(double now, double delay); | 94 void setNextFireTime(double now, double delay); |
96 | 95 |
97 void runInternal(); | 96 void runInternal(); |
98 | 97 |
| 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 |
99 double m_nextFireTime; // 0 if inactive | 129 double m_nextFireTime; // 0 if inactive |
100 double m_repeatInterval; // 0 if not repeating | 130 double m_repeatInterval; // 0 if not repeating |
101 WebTraceLocation m_location; | 131 WebTraceLocation m_location; |
| 132 CancellableTimerTask* m_cancellableTimerTask; // NOT OWNED |
102 std::unique_ptr<WebTaskRunner> m_webTaskRunner; | 133 std::unique_ptr<WebTaskRunner> m_webTaskRunner; |
103 | 134 |
104 #if DCHECK_IS_ON() | 135 #if DCHECK_IS_ON() |
105 ThreadIdentifier m_thread; | 136 ThreadIdentifier m_thread; |
106 #endif | 137 #endif |
107 WTF::WeakPtrFactory<TimerBase> m_weakPtrFactory; | |
108 | 138 |
109 friend class ThreadTimers; | 139 friend class ThreadTimers; |
110 friend class TimerHeapLessThanFunction; | 140 friend class TimerHeapLessThanFunction; |
111 friend class TimerHeapReference; | 141 friend class TimerHeapReference; |
112 }; | 142 }; |
113 | 143 |
114 template<typename T, bool = IsGarbageCollectedType<T>::value> | 144 template<typename T, bool = IsGarbageCollectedType<T>::value> |
115 class TimerIsObjectAliveTrait { | 145 class TimerIsObjectAliveTrait { |
116 public: | 146 public: |
117 static bool isHeapObjectAlive(T*) { return true; } | 147 static bool isHeapObjectAlive(T*) { return true; } |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
190 UnthrottledThreadTimer(TimerFiredClass* timerFiredClass, TimerFiredFunction
timerFiredFunction) | 220 UnthrottledThreadTimer(TimerFiredClass* timerFiredClass, TimerFiredFunction
timerFiredFunction) |
191 : TaskRunnerTimer<TimerFiredClass>(TimerBase::getUnthrottledTaskRunner()
, timerFiredClass, timerFiredFunction) | 221 : TaskRunnerTimer<TimerFiredClass>(TimerBase::getUnthrottledTaskRunner()
, timerFiredClass, timerFiredFunction) |
192 { | 222 { |
193 } | 223 } |
194 }; | 224 }; |
195 | 225 |
196 NO_LAZY_SWEEP_SANITIZE_ADDRESS | 226 NO_LAZY_SWEEP_SANITIZE_ADDRESS |
197 inline bool TimerBase::isActive() const | 227 inline bool TimerBase::isActive() const |
198 { | 228 { |
199 ASSERT(m_thread == currentThread()); | 229 ASSERT(m_thread == currentThread()); |
200 return m_weakPtrFactory.hasWeakPtrs(); | 230 return m_cancellableTimerTask; |
201 } | 231 } |
202 | 232 |
203 } // namespace blink | 233 } // namespace blink |
204 | 234 |
205 #endif // Timer_h | 235 #endif // Timer_h |
OLD | NEW |