Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(408)

Side by Side Diff: Source/platform/Timer.h

Issue 1185643002: Lazily constructed bespoke cancellable timer task. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Fix UAF bug that occured on worker shutdown, where ~CancellableTimerTask wasn't clearing backref Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | Source/platform/Timer.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 10 matching lines...) Expand all
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 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. 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */ 24 */
25 25
26 #ifndef Timer_h 26 #ifndef Timer_h
27 #define Timer_h 27 #define Timer_h
28 28
29 #include "platform/PlatformExport.h" 29 #include "platform/PlatformExport.h"
30 #include "platform/heap/Handle.h" 30 #include "platform/heap/Handle.h"
31 #include "platform/scheduler/CancellableTaskFactory.h"
32 #include "public/platform/WebTraceLocation.h" 31 #include "public/platform/WebTraceLocation.h"
33 #include "wtf/AddressSanitizer.h" 32 #include "wtf/AddressSanitizer.h"
34 #include "wtf/Noncopyable.h" 33 #include "wtf/Noncopyable.h"
35 #include "wtf/Threading.h" 34 #include "wtf/Threading.h"
36 #include "wtf/Vector.h" 35 #include "wtf/Vector.h"
37 36
38 namespace blink { 37 namespace blink {
39 38
40 // Time intervals are all in seconds. 39 // Time intervals are all in seconds.
41 40
(...skipping 23 matching lines...) Expand all
65 double repeatInterval() const { return m_repeatInterval; } 64 double repeatInterval() const { return m_repeatInterval; }
66 65
67 void augmentRepeatInterval(double delta) { 66 void augmentRepeatInterval(double delta) {
68 double now = monotonicallyIncreasingTime(); 67 double now = monotonicallyIncreasingTime();
69 setNextFireTime(now, m_nextFireTime - now + delta); 68 setNextFireTime(now, m_nextFireTime - now + delta);
70 m_repeatInterval += delta; 69 m_repeatInterval += delta;
71 } 70 }
72 71
73 void didChangeAlignmentInterval(double now); 72 void didChangeAlignmentInterval(double now);
74 73
75 #if defined(ADDRESS_SANITIZER)
76 protected:
77 CancellableTaskFactory& cancellableTaskFactory() { return m_cancellableTaskF actory; }
78 #endif
79
80 private: 74 private:
81 virtual void fired() = 0; 75 virtual void fired() = 0;
82 76
83 NO_LAZY_SWEEP_SANITIZE_ADDRESS 77 NO_LAZY_SWEEP_SANITIZE_ADDRESS
84 virtual bool canFire() const { return true; } 78 virtual bool canFire() const { return true; }
85 79
86 virtual double alignedFireTime(double fireTime) const { return fireTime; } 80 virtual double alignedFireTime(double fireTime) const { return fireTime; }
87 81
88 void setNextFireTime(double now, double delay); 82 void setNextFireTime(double now, double delay);
89 83
90 void run(); 84 void runInternal();
85
86 class CancellableTimerTask final : public WebThread::Task {
87 WTF_MAKE_NONCOPYABLE(CancellableTimerTask);
88 public:
89 explicit CancellableTimerTask(TimerBase* timer) : m_timer(timer) { }
90
91 ~CancellableTimerTask() override
92 {
93 if (m_timer)
94 m_timer->m_cancellableTimerTask = nullptr;
95 }
96
97 NO_LAZY_SWEEP_SANITIZE_ADDRESS
98 void run() override
99 {
100 if (m_timer) {
101 m_timer->m_cancellableTimerTask = nullptr;
102 m_timer->runInternal();
103 m_timer = nullptr;
104 }
105 }
106
107 void cancel()
108 {
109 m_timer = nullptr;
110 }
111
112 private:
113 TimerBase* m_timer; // NOT OWNED
114 };
91 115
92 double m_nextFireTime; // 0 if inactive 116 double m_nextFireTime; // 0 if inactive
93 double m_unalignedNextFireTime; // m_nextFireTime not considering alignment interval 117 double m_unalignedNextFireTime; // m_nextFireTime not considering alignment interval
94 double m_repeatInterval; // 0 if not repeating 118 double m_repeatInterval; // 0 if not repeating
95 WebTraceLocation m_location; 119 WebTraceLocation m_location;
96 CancellableTaskFactory m_cancellableTaskFactory; 120 CancellableTimerTask* m_cancellableTimerTask; // NOT OWNED
97 WebScheduler* m_webScheduler; // Not owned. 121 WebScheduler* m_webScheduler; // Not owned.
98 122
99 #if ENABLE(ASSERT) 123 #if ENABLE(ASSERT)
100 ThreadIdentifier m_thread; 124 ThreadIdentifier m_thread;
101 #endif 125 #endif
102 126
103 friend class ThreadTimers; 127 friend class ThreadTimers;
104 friend class TimerHeapLessThanFunction; 128 friend class TimerHeapLessThanFunction;
105 friend class TimerHeapReference; 129 friend class TimerHeapReference;
106 }; 130 };
(...skipping 14 matching lines...) Expand all
121 }; 145 };
122 146
123 template <typename TimerFiredClass> 147 template <typename TimerFiredClass>
124 class Timer : public TimerBase { 148 class Timer : public TimerBase {
125 public: 149 public:
126 typedef void (TimerFiredClass::*TimerFiredFunction)(Timer*); 150 typedef void (TimerFiredClass::*TimerFiredFunction)(Timer*);
127 151
128 Timer(TimerFiredClass* o, TimerFiredFunction f) 152 Timer(TimerFiredClass* o, TimerFiredFunction f)
129 : m_object(o), m_function(f) 153 : m_object(o), m_function(f)
130 { 154 {
131 #if ENABLE(LAZY_SWEEPING) && defined(ADDRESS_SANITIZER)
132 if (IsGarbageCollectedType<TimerFiredClass>::value)
133 cancellableTaskFactory().setUnpoisonBeforeUpdate();
134 #endif
135 } 155 }
136 156
137 protected: 157 protected:
138 virtual void fired() override 158 virtual void fired() override
139 { 159 {
140 (m_object->*m_function)(this); 160 (m_object->*m_function)(this);
141 } 161 }
142 162
143 NO_LAZY_SWEEP_SANITIZE_ADDRESS 163 NO_LAZY_SWEEP_SANITIZE_ADDRESS
144 virtual bool canFire() const override 164 virtual bool canFire() const override
(...skipping 10 matching lines...) Expand all
155 // in the current code base). 175 // in the current code base).
156 GC_PLUGIN_IGNORE("363031") 176 GC_PLUGIN_IGNORE("363031")
157 TimerFiredClass* m_object; 177 TimerFiredClass* m_object;
158 TimerFiredFunction m_function; 178 TimerFiredFunction m_function;
159 }; 179 };
160 180
161 NO_LAZY_SWEEP_SANITIZE_ADDRESS 181 NO_LAZY_SWEEP_SANITIZE_ADDRESS
162 inline bool TimerBase::isActive() const 182 inline bool TimerBase::isActive() const
163 { 183 {
164 ASSERT(m_thread == currentThread()); 184 ASSERT(m_thread == currentThread());
165 return m_cancellableTaskFactory.isPending(); 185 return m_cancellableTimerTask;
166 } 186 }
167 187
168 } // namespace blink 188 } // namespace blink
169 189
170 #endif // Timer_h 190 #endif // Timer_h
OLDNEW
« no previous file with comments | « no previous file | Source/platform/Timer.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698