| OLD | NEW |
| 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 |
| 11 * notice, this list of conditions and the following disclaimer in the | 11 * notice, this list of conditions and the following disclaimer in the |
| 12 * documentation and/or other materials provided with the distribution. | 12 * documentation and/or other materials provided with the distribution. |
| 13 * | 13 * |
| 14 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY | 14 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY |
| 15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR | 17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR |
| 18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | 18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | 19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | 20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | 21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 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/PlatformThreadData.h" | 30 #include "platform/TraceEvent.h" |
| 31 #include "platform/ThreadTimers.h" | 31 #include "public/platform/Platform.h" |
| 32 #include "wtf/AddressSanitizer.h" |
| 32 #include "wtf/Atomics.h" | 33 #include "wtf/Atomics.h" |
| 33 #include "wtf/CurrentTime.h" | 34 #include "wtf/CurrentTime.h" |
| 34 #include "wtf/HashSet.h" | 35 #include "wtf/HashSet.h" |
| 35 #include <algorithm> | 36 #include <algorithm> |
| 36 #include <limits.h> | 37 #include <limits.h> |
| 37 #include <limits> | 38 #include <limits> |
| 38 #include <math.h> | 39 #include <math.h> |
| 39 | 40 |
| 40 namespace blink { | 41 namespace blink { |
| 41 | 42 |
| 42 class TimerHeapReference; | |
| 43 | |
| 44 // Timers are stored in a heap data structure, used to implement a priority queu
e. | |
| 45 // This allows us to efficiently determine which timer needs to fire the soonest
. | |
| 46 // Then we set a single shared system timer to fire at that time. | |
| 47 // | |
| 48 // When a timer's "next fire time" changes, we need to move it around in the pri
ority queue. | |
| 49 static Vector<TimerBase*>& threadGlobalTimerHeap() | |
| 50 { | |
| 51 return PlatformThreadData::current().threadTimers().timerHeap(); | |
| 52 } | |
| 53 // ---------------- | |
| 54 | |
| 55 class TimerHeapPointer { | |
| 56 public: | |
| 57 TimerHeapPointer(TimerBase** pointer) : m_pointer(pointer) { } | |
| 58 TimerHeapReference operator*() const; | |
| 59 TimerBase* operator->() const { return *m_pointer; } | |
| 60 private: | |
| 61 TimerBase** m_pointer; | |
| 62 }; | |
| 63 | |
| 64 class TimerHeapReference { | |
| 65 public: | |
| 66 TimerHeapReference(TimerBase*& reference) : m_reference(reference) { } | |
| 67 operator TimerBase*() const { return m_reference; } | |
| 68 TimerHeapPointer operator&() const { return &m_reference; } | |
| 69 TimerHeapReference& operator=(TimerBase*); | |
| 70 TimerHeapReference& operator=(TimerHeapReference); | |
| 71 private: | |
| 72 TimerBase*& m_reference; | |
| 73 }; | |
| 74 | |
| 75 inline TimerHeapReference TimerHeapPointer::operator*() const | |
| 76 { | |
| 77 return *m_pointer; | |
| 78 } | |
| 79 | |
| 80 NO_LAZY_SWEEP_SANITIZE_ADDRESS | |
| 81 inline TimerHeapReference& TimerHeapReference::operator=(TimerBase* timer) | |
| 82 { | |
| 83 m_reference = timer; | |
| 84 Vector<TimerBase*>& heap = timer->timerHeap(); | |
| 85 if (&m_reference >= heap.data() && &m_reference < heap.data() + heap.size()) | |
| 86 timer->m_heapIndex = &m_reference - heap.data(); | |
| 87 return *this; | |
| 88 } | |
| 89 | |
| 90 NO_LAZY_SWEEP_SANITIZE_ADDRESS | |
| 91 inline TimerHeapReference& TimerHeapReference::operator=(TimerHeapReference b) | |
| 92 { | |
| 93 TimerBase* timer = b; | |
| 94 return *this = timer; | |
| 95 } | |
| 96 | |
| 97 inline void swap(TimerHeapReference a, TimerHeapReference b) | |
| 98 { | |
| 99 TimerBase* timerA = a; | |
| 100 TimerBase* timerB = b; | |
| 101 | |
| 102 // Invoke the assignment operator, since that takes care of updating m_heapI
ndex. | |
| 103 a = timerB; | |
| 104 b = timerA; | |
| 105 } | |
| 106 | |
| 107 // ---------------- | |
| 108 | |
| 109 // Class to represent iterators in the heap when calling the standard library he
ap algorithms. | |
| 110 // Uses a custom pointer and reference type that update indices for pointers in
the heap. | |
| 111 class TimerHeapIterator : public std::iterator<std::random_access_iterator_tag,
TimerBase*, ptrdiff_t, TimerHeapPointer, TimerHeapReference> { | |
| 112 public: | |
| 113 explicit TimerHeapIterator(TimerBase** pointer) : m_pointer(pointer) { check
Consistency(); } | |
| 114 | |
| 115 TimerHeapIterator& operator++() { checkConsistency(); ++m_pointer; checkCons
istency(); return *this; } | |
| 116 TimerHeapIterator operator++(int) { checkConsistency(1); return TimerHeapIte
rator(m_pointer++); } | |
| 117 | |
| 118 TimerHeapIterator& operator--() { checkConsistency(); --m_pointer; checkCons
istency(); return *this; } | |
| 119 TimerHeapIterator operator--(int) { checkConsistency(-1); return TimerHeapIt
erator(m_pointer--); } | |
| 120 | |
| 121 TimerHeapIterator& operator+=(ptrdiff_t i) { checkConsistency(); m_pointer +
= i; checkConsistency(); return *this; } | |
| 122 TimerHeapIterator& operator-=(ptrdiff_t i) { checkConsistency(); m_pointer -
= i; checkConsistency(); return *this; } | |
| 123 | |
| 124 TimerHeapReference operator*() const { return TimerHeapReference(*m_pointer)
; } | |
| 125 TimerHeapReference operator[](ptrdiff_t i) const { return TimerHeapReference
(m_pointer[i]); } | |
| 126 TimerBase* operator->() const { return *m_pointer; } | |
| 127 | |
| 128 private: | |
| 129 NO_LAZY_SWEEP_SANITIZE_ADDRESS | |
| 130 void checkConsistency(ptrdiff_t offset = 0) const | |
| 131 { | |
| 132 ASSERT(m_pointer >= threadGlobalTimerHeap().data()); | |
| 133 ASSERT(m_pointer <= threadGlobalTimerHeap().data() + threadGlobalTimerHe
ap().size()); | |
| 134 ASSERT_UNUSED(offset, m_pointer + offset >= threadGlobalTimerHeap().data
()); | |
| 135 ASSERT_UNUSED(offset, m_pointer + offset <= threadGlobalTimerHeap().data
() + threadGlobalTimerHeap().size()); | |
| 136 } | |
| 137 | |
| 138 friend bool operator==(TimerHeapIterator, TimerHeapIterator); | |
| 139 friend bool operator!=(TimerHeapIterator, TimerHeapIterator); | |
| 140 friend bool operator<(TimerHeapIterator, TimerHeapIterator); | |
| 141 friend bool operator>(TimerHeapIterator, TimerHeapIterator); | |
| 142 friend bool operator<=(TimerHeapIterator, TimerHeapIterator); | |
| 143 friend bool operator>=(TimerHeapIterator, TimerHeapIterator); | |
| 144 | |
| 145 friend TimerHeapIterator operator+(TimerHeapIterator, size_t); | |
| 146 friend TimerHeapIterator operator+(size_t, TimerHeapIterator); | |
| 147 | |
| 148 friend TimerHeapIterator operator-(TimerHeapIterator, size_t); | |
| 149 friend ptrdiff_t operator-(TimerHeapIterator, TimerHeapIterator); | |
| 150 | |
| 151 TimerBase** m_pointer; | |
| 152 }; | |
| 153 | |
| 154 inline bool operator==(TimerHeapIterator a, TimerHeapIterator b) { return a.m_po
inter == b.m_pointer; } | |
| 155 inline bool operator!=(TimerHeapIterator a, TimerHeapIterator b) { return a.m_po
inter != b.m_pointer; } | |
| 156 inline bool operator<(TimerHeapIterator a, TimerHeapIterator b) { return a.m_poi
nter < b.m_pointer; } | |
| 157 inline bool operator>(TimerHeapIterator a, TimerHeapIterator b) { return a.m_poi
nter > b.m_pointer; } | |
| 158 inline bool operator<=(TimerHeapIterator a, TimerHeapIterator b) { return a.m_po
inter <= b.m_pointer; } | |
| 159 inline bool operator>=(TimerHeapIterator a, TimerHeapIterator b) { return a.m_po
inter >= b.m_pointer; } | |
| 160 | |
| 161 inline TimerHeapIterator operator+(TimerHeapIterator a, size_t b) { return Timer
HeapIterator(a.m_pointer + b); } | |
| 162 inline TimerHeapIterator operator+(size_t a, TimerHeapIterator b) { return Timer
HeapIterator(a + b.m_pointer); } | |
| 163 | |
| 164 inline TimerHeapIterator operator-(TimerHeapIterator a, size_t b) { return Timer
HeapIterator(a.m_pointer - b); } | |
| 165 inline ptrdiff_t operator-(TimerHeapIterator a, TimerHeapIterator b) { return a.
m_pointer - b.m_pointer; } | |
| 166 | |
| 167 // ---------------- | |
| 168 | |
| 169 class TimerHeapLessThanFunction { | |
| 170 public: | |
| 171 bool operator()(const TimerBase*, const TimerBase*) const; | |
| 172 }; | |
| 173 | |
| 174 NO_LAZY_SWEEP_SANITIZE_ADDRESS | |
| 175 inline bool TimerHeapLessThanFunction::operator()(const TimerBase* a, const Time
rBase* b) const | |
| 176 { | |
| 177 // The comparisons below are "backwards" because the heap puts the largest | |
| 178 // element first and we want the lowest time to be the first one in the heap
. | |
| 179 double aFireTime = a->m_nextFireTime; | |
| 180 double bFireTime = b->m_nextFireTime; | |
| 181 if (bFireTime != aFireTime) | |
| 182 return bFireTime < aFireTime; | |
| 183 | |
| 184 // We need to look at the difference of the insertion orders instead of comp
aring the two | |
| 185 // outright in case of overflow. | |
| 186 unsigned difference = a->m_heapInsertionOrder - b->m_heapInsertionOrder; | |
| 187 return difference < std::numeric_limits<unsigned>::max() / 2; | |
| 188 } | |
| 189 | |
| 190 // ---------------- | |
| 191 | |
| 192 TimerBase::TimerBase() | 43 TimerBase::TimerBase() |
| 193 : m_nextFireTime(0) | 44 : m_nextFireTime(0) |
| 194 , m_unalignedNextFireTime(0) | 45 , m_unalignedNextFireTime(0) |
| 195 , m_repeatInterval(0) | 46 , m_repeatInterval(0) |
| 196 , m_heapIndex(-1) | 47 , m_cancellableTaskFactory(WTF::bind(&TimerBase::run, this)) |
| 197 , m_cachedThreadGlobalTimerHeap(0) | 48 , m_webScheduler(Platform::current()->currentThread()->scheduler()) |
| 198 #if ENABLE(ASSERT) | 49 #if ENABLE(ASSERT) |
| 199 , m_thread(currentThread()) | 50 , m_thread(currentThread()) |
| 200 #endif | 51 #endif |
| 201 { | 52 { |
| 202 } | 53 } |
| 203 | 54 |
| 204 TimerBase::~TimerBase() | 55 TimerBase::~TimerBase() |
| 205 { | 56 { |
| 206 stop(); | 57 stop(); |
| 207 ASSERT(!inHeap()); | |
| 208 } | 58 } |
| 209 | 59 |
| 210 void TimerBase::start(double nextFireInterval, double repeatInterval, const WebT
raceLocation& caller) | 60 void TimerBase::start(double nextFireInterval, double repeatInterval, const WebT
raceLocation& caller) |
| 211 { | 61 { |
| 212 ASSERT(m_thread == currentThread()); | 62 ASSERT(m_thread == currentThread()); |
| 213 | 63 |
| 214 m_location = caller; | 64 m_location = caller; |
| 215 m_repeatInterval = repeatInterval; | 65 m_repeatInterval = repeatInterval; |
| 216 setNextFireTime(monotonicallyIncreasingTime() + nextFireInterval); | 66 setNextFireTime(monotonicallyIncreasingTime(), nextFireInterval); |
| 217 } | 67 } |
| 218 | 68 |
| 219 void TimerBase::stop() | 69 void TimerBase::stop() |
| 220 { | 70 { |
| 221 ASSERT(m_thread == currentThread()); | 71 ASSERT(m_thread == currentThread()); |
| 222 | 72 |
| 223 m_repeatInterval = 0; | 73 m_repeatInterval = 0; |
| 224 setNextFireTime(0); | 74 m_nextFireTime = 0; |
| 225 | 75 m_cancellableTaskFactory.cancel(); |
| 226 ASSERT(m_nextFireTime == 0); | |
| 227 ASSERT(m_repeatInterval == 0); | |
| 228 ASSERT(!inHeap()); | |
| 229 } | 76 } |
| 230 | 77 |
| 231 double TimerBase::nextFireInterval() const | 78 double TimerBase::nextFireInterval() const |
| 232 { | 79 { |
| 233 ASSERT(isActive()); | 80 ASSERT(isActive()); |
| 234 double current = monotonicallyIncreasingTime(); | 81 double current = monotonicallyIncreasingTime(); |
| 235 if (m_nextFireTime < current) | 82 if (m_nextFireTime < current) |
| 236 return 0; | 83 return 0; |
| 237 return m_nextFireTime - current; | 84 return m_nextFireTime - current; |
| 238 } | 85 } |
| 239 | 86 |
| 240 NO_LAZY_SWEEP_SANITIZE_ADDRESS | 87 NO_LAZY_SWEEP_SANITIZE_ADDRESS |
| 241 inline void TimerBase::checkHeapIndex() const | 88 void TimerBase::setNextFireTime(double now, double delay) |
| 242 { | |
| 243 ASSERT(timerHeap() == threadGlobalTimerHeap()); | |
| 244 ASSERT(!timerHeap().isEmpty()); | |
| 245 ASSERT(m_heapIndex >= 0); | |
| 246 ASSERT(m_heapIndex < static_cast<int>(timerHeap().size())); | |
| 247 ASSERT(timerHeap()[m_heapIndex] == this); | |
| 248 } | |
| 249 | |
| 250 NO_LAZY_SWEEP_SANITIZE_ADDRESS | |
| 251 inline void TimerBase::checkConsistency() const | |
| 252 { | |
| 253 // Timers should be in the heap if and only if they have a non-zero next fir
e time. | |
| 254 ASSERT(inHeap() == (m_nextFireTime != 0)); | |
| 255 if (inHeap()) | |
| 256 checkHeapIndex(); | |
| 257 } | |
| 258 | |
| 259 void TimerBase::heapDecreaseKey() | |
| 260 { | |
| 261 ASSERT(m_nextFireTime != 0); | |
| 262 checkHeapIndex(); | |
| 263 TimerBase** heapData = timerHeap().data(); | |
| 264 push_heap(TimerHeapIterator(heapData), TimerHeapIterator(heapData + m_heapIn
dex + 1), TimerHeapLessThanFunction()); | |
| 265 checkHeapIndex(); | |
| 266 } | |
| 267 | |
| 268 NO_LAZY_SWEEP_SANITIZE_ADDRESS | |
| 269 inline void TimerBase::heapDelete() | |
| 270 { | |
| 271 ASSERT(m_nextFireTime == 0); | |
| 272 heapPop(); | |
| 273 timerHeap().removeLast(); | |
| 274 m_heapIndex = -1; | |
| 275 } | |
| 276 | |
| 277 NO_LAZY_SWEEP_SANITIZE_ADDRESS | |
| 278 void TimerBase::heapDeleteMin() | |
| 279 { | |
| 280 ASSERT(m_nextFireTime == 0); | |
| 281 heapPopMin(); | |
| 282 timerHeap().removeLast(); | |
| 283 m_heapIndex = -1; | |
| 284 } | |
| 285 | |
| 286 inline void TimerBase::heapIncreaseKey() | |
| 287 { | |
| 288 ASSERT(m_nextFireTime != 0); | |
| 289 heapPop(); | |
| 290 heapDecreaseKey(); | |
| 291 } | |
| 292 | |
| 293 inline void TimerBase::heapInsert() | |
| 294 { | |
| 295 ASSERT(!inHeap()); | |
| 296 timerHeap().append(this); | |
| 297 m_heapIndex = timerHeap().size() - 1; | |
| 298 heapDecreaseKey(); | |
| 299 } | |
| 300 | |
| 301 NO_LAZY_SWEEP_SANITIZE_ADDRESS | |
| 302 inline void TimerBase::heapPop() | |
| 303 { | |
| 304 // Temporarily force this timer to have the minimum key so we can pop it. | |
| 305 double fireTime = m_nextFireTime; | |
| 306 m_nextFireTime = -std::numeric_limits<double>::infinity(); | |
| 307 heapDecreaseKey(); | |
| 308 heapPopMin(); | |
| 309 m_nextFireTime = fireTime; | |
| 310 } | |
| 311 | |
| 312 NO_LAZY_SWEEP_SANITIZE_ADDRESS | |
| 313 void TimerBase::heapPopMin() | |
| 314 { | |
| 315 ASSERT(this == timerHeap().first()); | |
| 316 checkHeapIndex(); | |
| 317 Vector<TimerBase*>& heap = timerHeap(); | |
| 318 TimerBase** heapData = heap.data(); | |
| 319 pop_heap(TimerHeapIterator(heapData), TimerHeapIterator(heapData + heap.size
()), TimerHeapLessThanFunction()); | |
| 320 checkHeapIndex(); | |
| 321 ASSERT(this == timerHeap().last()); | |
| 322 } | |
| 323 | |
| 324 static inline bool parentHeapPropertyHolds(const TimerBase* current, const Vecto
r<TimerBase*>& heap, unsigned currentIndex) | |
| 325 { | |
| 326 if (!currentIndex) | |
| 327 return true; | |
| 328 unsigned parentIndex = (currentIndex - 1) / 2; | |
| 329 TimerHeapLessThanFunction compareHeapPosition; | |
| 330 return compareHeapPosition(current, heap[parentIndex]); | |
| 331 } | |
| 332 | |
| 333 static inline bool childHeapPropertyHolds(const TimerBase* current, const Vector
<TimerBase*>& heap, unsigned childIndex) | |
| 334 { | |
| 335 if (childIndex >= heap.size()) | |
| 336 return true; | |
| 337 TimerHeapLessThanFunction compareHeapPosition; | |
| 338 return compareHeapPosition(heap[childIndex], current); | |
| 339 } | |
| 340 | |
| 341 bool TimerBase::hasValidHeapPosition() const | |
| 342 { | |
| 343 ASSERT(m_nextFireTime); | |
| 344 if (!inHeap()) | |
| 345 return false; | |
| 346 // Check if the heap property still holds with the new fire time. If it does
we don't need to do anything. | |
| 347 // This assumes that the STL heap is a standard binary heap. In an unlikely
event it is not, the assertions | |
| 348 // in updateHeapIfNeeded() will get hit. | |
| 349 const Vector<TimerBase*>& heap = timerHeap(); | |
| 350 if (!parentHeapPropertyHolds(this, heap, m_heapIndex)) | |
| 351 return false; | |
| 352 unsigned childIndex1 = 2 * m_heapIndex + 1; | |
| 353 unsigned childIndex2 = childIndex1 + 1; | |
| 354 return childHeapPropertyHolds(this, heap, childIndex1) && childHeapPropertyH
olds(this, heap, childIndex2); | |
| 355 } | |
| 356 | |
| 357 void TimerBase::updateHeapIfNeeded(double oldTime) | |
| 358 { | |
| 359 if (m_nextFireTime && hasValidHeapPosition()) | |
| 360 return; | |
| 361 #if ENABLE(ASSERT) | |
| 362 int oldHeapIndex = m_heapIndex; | |
| 363 #endif | |
| 364 if (!oldTime) | |
| 365 heapInsert(); | |
| 366 else if (!m_nextFireTime) | |
| 367 heapDelete(); | |
| 368 else if (m_nextFireTime < oldTime) | |
| 369 heapDecreaseKey(); | |
| 370 else | |
| 371 heapIncreaseKey(); | |
| 372 ASSERT(m_heapIndex != oldHeapIndex); | |
| 373 ASSERT(!inHeap() || hasValidHeapPosition()); | |
| 374 } | |
| 375 | |
| 376 NO_LAZY_SWEEP_SANITIZE_ADDRESS | |
| 377 void TimerBase::setNextFireTime(double newUnalignedTime) | |
| 378 { | 89 { |
| 379 ASSERT(m_thread == currentThread()); | 90 ASSERT(m_thread == currentThread()); |
| 380 | 91 |
| 381 if (m_unalignedNextFireTime != newUnalignedTime) | 92 m_unalignedNextFireTime = now + delay; |
| 382 m_unalignedNextFireTime = newUnalignedTime; | |
| 383 | 93 |
| 384 // Accessing thread global data is slow. Cache the heap pointer. | 94 double newTime = alignedFireTime(m_unalignedNextFireTime); |
| 385 if (!m_cachedThreadGlobalTimerHeap) | 95 if (m_nextFireTime != newTime) { |
| 386 m_cachedThreadGlobalTimerHeap = &threadGlobalTimerHeap(); | |
| 387 | |
| 388 // Keep heap valid while changing the next-fire time. | |
| 389 double oldTime = m_nextFireTime; | |
| 390 double newTime = alignedFireTime(newUnalignedTime); | |
| 391 if (oldTime != newTime) { | |
| 392 m_nextFireTime = newTime; | 96 m_nextFireTime = newTime; |
| 393 static unsigned currentHeapInsertionOrder; | 97 // Round the delay up to the nearest millisecond to be consistant with t
he |
| 394 m_heapInsertionOrder = atomicAdd(¤tHeapInsertionOrder, 1); | 98 // previous behavior of BlinkPlatformImpl::setSharedTimerFireInterval. |
| 395 | 99 long long delayMs = static_cast<long long>(ceil((newTime - now) * 1000.0
)); |
| 396 bool wasFirstTimerInHeap = m_heapIndex == 0; | 100 if (delayMs < 0) |
| 397 | 101 delayMs = 0; |
| 398 updateHeapIfNeeded(oldTime); | 102 m_webScheduler->postTimerTask(m_location, m_cancellableTaskFactory.cance
lAndCreate(), delayMs); |
| 399 | |
| 400 bool isFirstTimerInHeap = m_heapIndex == 0; | |
| 401 | |
| 402 if (wasFirstTimerInHeap || isFirstTimerInHeap) | |
| 403 PlatformThreadData::current().threadTimers().updateSharedTimer(); | |
| 404 } | 103 } |
| 405 | |
| 406 checkConsistency(); | |
| 407 } | 104 } |
| 408 | 105 |
| 409 void TimerBase::didChangeAlignmentInterval() | 106 void TimerBase::run() |
| 410 { | 107 { |
| 411 setNextFireTime(m_unalignedNextFireTime); | 108 TRACE_EVENT0("blink", "TimerBase::run"); |
| 109 ASSERT_WITH_MESSAGE(m_thread == currentThread(), "Timer posted by %s %s was
run on a different thread", m_location.functionName(), m_location.fileName()); |
| 110 TRACE_EVENT_SET_SAMPLING_STATE("blink", "BlinkInternal"); |
| 111 |
| 112 m_nextFireTime = 0; |
| 113 // Note: repeating timers drift, but this is preserving the functionality of
the old timer heap. |
| 114 // See crbug.com/328700. |
| 115 if (m_repeatInterval) |
| 116 setNextFireTime(monotonicallyIncreasingTime(), m_repeatInterval); |
| 117 fired(); |
| 118 TRACE_EVENT_SET_SAMPLING_STATE("blink", "Sleeping"); |
| 119 } |
| 120 |
| 121 void TimerBase::didChangeAlignmentInterval(double now) |
| 122 { |
| 123 setNextFireTime(now, m_unalignedNextFireTime - now); |
| 412 } | 124 } |
| 413 | 125 |
| 414 double TimerBase::nextUnalignedFireInterval() const | 126 double TimerBase::nextUnalignedFireInterval() const |
| 415 { | 127 { |
| 416 ASSERT(isActive()); | 128 ASSERT(isActive()); |
| 417 return std::max(m_unalignedNextFireTime - monotonicallyIncreasingTime(), 0.0
); | 129 return std::max(m_unalignedNextFireTime - monotonicallyIncreasingTime(), 0.0
); |
| 418 } | 130 } |
| 419 | 131 |
| 420 } // namespace blink | 132 } // namespace blink |
| OLD | NEW |