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

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

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 | « Source/platform/Timer.h ('k') | no next file » | 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, 2008 Apple Inc. All rights reserved. 2 * Copyright (C) 2006, 2008 Apple Inc. All rights reserved.
3 * Copyright (C) 2009 Google Inc. All rights reserved. 3 * Copyright (C) 2009 Google Inc. All rights reserved.
4 * 4 *
5 * Redistribution and use in source and binary forms, with or without 5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions 6 * modification, are permitted provided that the following conditions
7 * are met: 7 * are met:
8 * 1. Redistributions of source code must retain the above copyright 8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright 10 * 2. Redistributions in binary form must reproduce the above copyright
(...skipping 11 matching lines...) Expand all
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */ 25 */
26 26
27 #include "config.h" 27 #include "config.h"
28 #include "platform/Timer.h" 28 #include "platform/Timer.h"
29 29
30 #include "platform/TraceEvent.h" 30 #include "platform/TraceEvent.h"
31 #include "public/platform/Platform.h" 31 #include "public/platform/Platform.h"
32 #include "public/platform/WebScheduler.h"
32 #include "wtf/AddressSanitizer.h" 33 #include "wtf/AddressSanitizer.h"
33 #include "wtf/Atomics.h" 34 #include "wtf/Atomics.h"
34 #include "wtf/CurrentTime.h" 35 #include "wtf/CurrentTime.h"
35 #include "wtf/HashSet.h" 36 #include "wtf/HashSet.h"
36 #include <algorithm> 37 #include <algorithm>
37 #include <limits.h> 38 #include <limits.h>
38 #include <limits> 39 #include <limits>
39 #include <math.h> 40 #include <math.h>
40 41
41 namespace blink { 42 namespace blink {
42 43
43 TimerBase::TimerBase() 44 TimerBase::TimerBase()
44 : m_nextFireTime(0) 45 : m_nextFireTime(0)
45 , m_unalignedNextFireTime(0) 46 , m_unalignedNextFireTime(0)
46 , m_repeatInterval(0) 47 , m_repeatInterval(0)
47 , m_cancellableTaskFactory(WTF::bind(&TimerBase::run, this)) 48 , m_cancellableTimerTask(nullptr)
48 , m_webScheduler(Platform::current()->currentThread()->scheduler()) 49 , m_webScheduler(Platform::current()->currentThread()->scheduler())
49 #if ENABLE(ASSERT) 50 #if ENABLE(ASSERT)
50 , m_thread(currentThread()) 51 , m_thread(currentThread())
51 #endif 52 #endif
52 { 53 {
53 } 54 }
54 55
55 TimerBase::~TimerBase() 56 TimerBase::~TimerBase()
56 { 57 {
57 stop(); 58 stop();
58 } 59 }
59 60
60 void TimerBase::start(double nextFireInterval, double repeatInterval, const WebT raceLocation& caller) 61 void TimerBase::start(double nextFireInterval, double repeatInterval, const WebT raceLocation& caller)
61 { 62 {
62 ASSERT(m_thread == currentThread()); 63 ASSERT(m_thread == currentThread());
63 64
64 m_location = caller; 65 m_location = caller;
65 m_repeatInterval = repeatInterval; 66 m_repeatInterval = repeatInterval;
66 setNextFireTime(monotonicallyIncreasingTime(), nextFireInterval); 67 setNextFireTime(monotonicallyIncreasingTime(), nextFireInterval);
67 } 68 }
68 69
69 void TimerBase::stop() 70 void TimerBase::stop()
70 { 71 {
71 ASSERT(m_thread == currentThread()); 72 ASSERT(m_thread == currentThread());
72 73
73 m_repeatInterval = 0; 74 m_repeatInterval = 0;
74 m_nextFireTime = 0; 75 m_nextFireTime = 0;
75 m_cancellableTaskFactory.cancel(); 76 if (m_cancellableTimerTask)
77 m_cancellableTimerTask->cancel();
78 m_cancellableTimerTask = nullptr;
76 } 79 }
77 80
78 double TimerBase::nextFireInterval() const 81 double TimerBase::nextFireInterval() const
79 { 82 {
80 ASSERT(isActive()); 83 ASSERT(isActive());
81 double current = monotonicallyIncreasingTime(); 84 double current = monotonicallyIncreasingTime();
82 if (m_nextFireTime < current) 85 if (m_nextFireTime < current)
83 return 0; 86 return 0;
84 return m_nextFireTime - current; 87 return m_nextFireTime - current;
85 } 88 }
86 89
87 void TimerBase::setNextFireTime(double now, double delay) 90 void TimerBase::setNextFireTime(double now, double delay)
88 { 91 {
89 ASSERT(m_thread == currentThread()); 92 ASSERT(m_thread == currentThread());
90 93
91 m_unalignedNextFireTime = now + delay; 94 m_unalignedNextFireTime = now + delay;
92 95
93 double newTime = alignedFireTime(m_unalignedNextFireTime); 96 double newTime = alignedFireTime(m_unalignedNextFireTime);
94 if (m_nextFireTime != newTime) { 97 if (m_nextFireTime != newTime) {
95 m_nextFireTime = newTime; 98 m_nextFireTime = newTime;
96 // Round the delay up to the nearest millisecond to be consistant with t he 99 // Round the delay up to the nearest millisecond to be consistant with t he
97 // previous behavior of BlinkPlatformImpl::setSharedTimerFireInterval. 100 // previous behavior of BlinkPlatformImpl::setSharedTimerFireInterval.
98 long long delayMs = static_cast<long long>(ceil((newTime - now) * 1000.0 )); 101 long long delayMs = static_cast<long long>(ceil((newTime - now) * 1000.0 ));
99 if (delayMs < 0) 102 if (delayMs < 0)
100 delayMs = 0; 103 delayMs = 0;
101 m_webScheduler->postTimerTask(m_location, m_cancellableTaskFactory.cance lAndCreate(), delayMs); 104 if (m_cancellableTimerTask)
105 m_cancellableTimerTask->cancel();
106 m_cancellableTimerTask = new CancellableTimerTask(this);
107 m_webScheduler->postTimerTask(m_location, m_cancellableTimerTask, delayM s);
102 } 108 }
103 } 109 }
104 110
105 NO_LAZY_SWEEP_SANITIZE_ADDRESS 111 NO_LAZY_SWEEP_SANITIZE_ADDRESS
106 void TimerBase::run() 112 void TimerBase::runInternal()
107 { 113 {
108 if (!canFire()) 114 if (!canFire())
109 return; 115 return;
110 116
111 TRACE_EVENT0("blink", "TimerBase::run"); 117 TRACE_EVENT0("blink", "TimerBase::run");
112 ASSERT_WITH_MESSAGE(m_thread == currentThread(), "Timer posted by %s %s was run on a different thread", m_location.functionName(), m_location.fileName()); 118 ASSERT_WITH_MESSAGE(m_thread == currentThread(), "Timer posted by %s %s was run on a different thread", m_location.functionName(), m_location.fileName());
113 TRACE_EVENT_SET_SAMPLING_STATE("blink", "BlinkInternal"); 119 TRACE_EVENT_SET_SAMPLING_STATE("blink", "BlinkInternal");
114 120
115 m_nextFireTime = 0; 121 m_nextFireTime = 0;
116 // Note: repeating timers drift, but this is preserving the functionality of the old timer heap. 122 // Note: repeating timers drift, but this is preserving the functionality of the old timer heap.
117 // See crbug.com/328700. 123 // See crbug.com/328700.
118 if (m_repeatInterval) 124 if (m_repeatInterval)
119 setNextFireTime(monotonicallyIncreasingTime(), m_repeatInterval); 125 setNextFireTime(monotonicallyIncreasingTime(), m_repeatInterval);
120 fired(); 126 fired();
121 TRACE_EVENT_SET_SAMPLING_STATE("blink", "Sleeping"); 127 TRACE_EVENT_SET_SAMPLING_STATE("blink", "Sleeping");
122 } 128 }
123 129
124 void TimerBase::didChangeAlignmentInterval(double now) 130 void TimerBase::didChangeAlignmentInterval(double now)
125 { 131 {
126 setNextFireTime(now, m_unalignedNextFireTime - now); 132 setNextFireTime(now, m_unalignedNextFireTime - now);
127 } 133 }
128 134
129 double TimerBase::nextUnalignedFireInterval() const 135 double TimerBase::nextUnalignedFireInterval() const
130 { 136 {
131 ASSERT(isActive()); 137 ASSERT(isActive());
132 return std::max(m_unalignedNextFireTime - monotonicallyIncreasingTime(), 0.0 ); 138 return std::max(m_unalignedNextFireTime - monotonicallyIncreasingTime(), 0.0 );
133 } 139 }
134 140
135 } // namespace blink 141 } // namespace blink
OLDNEW
« no previous file with comments | « Source/platform/Timer.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698