| 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 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 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" | |
| 33 #include "wtf/AddressSanitizer.h" | 32 #include "wtf/AddressSanitizer.h" |
| 34 #include "wtf/Atomics.h" | 33 #include "wtf/Atomics.h" |
| 35 #include "wtf/CurrentTime.h" | 34 #include "wtf/CurrentTime.h" |
| 36 #include "wtf/HashSet.h" | 35 #include "wtf/HashSet.h" |
| 37 #include <algorithm> | 36 #include <algorithm> |
| 38 #include <limits.h> | 37 #include <limits.h> |
| 39 #include <limits> | 38 #include <limits> |
| 40 #include <math.h> | 39 #include <math.h> |
| 41 | 40 |
| 42 namespace blink { | 41 namespace blink { |
| 43 | 42 |
| 44 TimerBase::TimerBase() | 43 TimerBase::TimerBase() |
| 45 : m_nextFireTime(0) | 44 : m_nextFireTime(0) |
| 46 , m_unalignedNextFireTime(0) | 45 , m_unalignedNextFireTime(0) |
| 47 , m_repeatInterval(0) | 46 , m_repeatInterval(0) |
| 48 , m_cancellableTimerTask(nullptr) | 47 , m_cancellableTaskFactory(WTF::bind(&TimerBase::run, this)) |
| 49 , m_webScheduler(Platform::current()->currentThread()->scheduler()) | 48 , m_webScheduler(Platform::current()->currentThread()->scheduler()) |
| 50 #if ENABLE(ASSERT) | 49 #if ENABLE(ASSERT) |
| 51 , m_thread(currentThread()) | 50 , m_thread(currentThread()) |
| 52 #endif | 51 #endif |
| 53 { | 52 { |
| 54 } | 53 } |
| 55 | 54 |
| 56 TimerBase::~TimerBase() | 55 TimerBase::~TimerBase() |
| 57 { | 56 { |
| 58 stop(); | 57 stop(); |
| 59 } | 58 } |
| 60 | 59 |
| 61 void TimerBase::start(double nextFireInterval, double repeatInterval, const WebT
raceLocation& caller) | 60 void TimerBase::start(double nextFireInterval, double repeatInterval, const WebT
raceLocation& caller) |
| 62 { | 61 { |
| 63 ASSERT(m_thread == currentThread()); | 62 ASSERT(m_thread == currentThread()); |
| 64 | 63 |
| 65 m_location = caller; | 64 m_location = caller; |
| 66 m_repeatInterval = repeatInterval; | 65 m_repeatInterval = repeatInterval; |
| 67 setNextFireTime(monotonicallyIncreasingTime(), nextFireInterval); | 66 setNextFireTime(monotonicallyIncreasingTime(), nextFireInterval); |
| 68 } | 67 } |
| 69 | 68 |
| 70 void TimerBase::stop() | 69 void TimerBase::stop() |
| 71 { | 70 { |
| 72 ASSERT(m_thread == currentThread()); | 71 ASSERT(m_thread == currentThread()); |
| 73 | 72 |
| 74 m_repeatInterval = 0; | 73 m_repeatInterval = 0; |
| 75 m_nextFireTime = 0; | 74 m_nextFireTime = 0; |
| 76 if (m_cancellableTimerTask) | 75 m_cancellableTaskFactory.cancel(); |
| 77 m_cancellableTimerTask->cancel(); | |
| 78 m_cancellableTimerTask = nullptr; | |
| 79 } | 76 } |
| 80 | 77 |
| 81 double TimerBase::nextFireInterval() const | 78 double TimerBase::nextFireInterval() const |
| 82 { | 79 { |
| 83 ASSERT(isActive()); | 80 ASSERT(isActive()); |
| 84 double current = monotonicallyIncreasingTime(); | 81 double current = monotonicallyIncreasingTime(); |
| 85 if (m_nextFireTime < current) | 82 if (m_nextFireTime < current) |
| 86 return 0; | 83 return 0; |
| 87 return m_nextFireTime - current; | 84 return m_nextFireTime - current; |
| 88 } | 85 } |
| 89 | 86 |
| 90 void TimerBase::setNextFireTime(double now, double delay) | 87 void TimerBase::setNextFireTime(double now, double delay) |
| 91 { | 88 { |
| 92 ASSERT(m_thread == currentThread()); | 89 ASSERT(m_thread == currentThread()); |
| 93 | 90 |
| 94 m_unalignedNextFireTime = now + delay; | 91 m_unalignedNextFireTime = now + delay; |
| 95 | 92 |
| 96 double newTime = alignedFireTime(m_unalignedNextFireTime); | 93 double newTime = alignedFireTime(m_unalignedNextFireTime); |
| 97 if (m_nextFireTime != newTime) { | 94 if (m_nextFireTime != newTime) { |
| 98 m_nextFireTime = newTime; | 95 m_nextFireTime = newTime; |
| 99 // Round the delay up to the nearest millisecond to be consistant with t
he | 96 // Round the delay up to the nearest millisecond to be consistant with t
he |
| 100 // previous behavior of BlinkPlatformImpl::setSharedTimerFireInterval. | 97 // previous behavior of BlinkPlatformImpl::setSharedTimerFireInterval. |
| 101 long long delayMs = static_cast<long long>(ceil((newTime - now) * 1000.0
)); | 98 long long delayMs = static_cast<long long>(ceil((newTime - now) * 1000.0
)); |
| 102 if (delayMs < 0) | 99 if (delayMs < 0) |
| 103 delayMs = 0; | 100 delayMs = 0; |
| 104 if (m_cancellableTimerTask) | 101 m_webScheduler->postTimerTask(m_location, m_cancellableTaskFactory.cance
lAndCreate(), delayMs); |
| 105 m_cancellableTimerTask->cancel(); | |
| 106 m_cancellableTimerTask = new CancellableTimerTask(this); | |
| 107 m_webScheduler->postTimerTask(m_location, m_cancellableTimerTask, delayM
s); | |
| 108 } | 102 } |
| 109 } | 103 } |
| 110 | 104 |
| 111 NO_LAZY_SWEEP_SANITIZE_ADDRESS | 105 NO_LAZY_SWEEP_SANITIZE_ADDRESS |
| 112 void TimerBase::runInternal() | 106 void TimerBase::run() |
| 113 { | 107 { |
| 114 if (!canFire()) | 108 if (!canFire()) |
| 115 return; | 109 return; |
| 116 | 110 |
| 117 TRACE_EVENT0("blink", "TimerBase::run"); | 111 TRACE_EVENT0("blink", "TimerBase::run"); |
| 118 ASSERT_WITH_MESSAGE(m_thread == currentThread(), "Timer posted by %s %s was
run on a different thread", m_location.functionName(), m_location.fileName()); | 112 ASSERT_WITH_MESSAGE(m_thread == currentThread(), "Timer posted by %s %s was
run on a different thread", m_location.functionName(), m_location.fileName()); |
| 119 TRACE_EVENT_SET_SAMPLING_STATE("blink", "BlinkInternal"); | 113 TRACE_EVENT_SET_SAMPLING_STATE("blink", "BlinkInternal"); |
| 120 | 114 |
| 121 m_nextFireTime = 0; | 115 m_nextFireTime = 0; |
| 122 // Note: repeating timers drift, but this is preserving the functionality of
the old timer heap. | 116 // Note: repeating timers drift, but this is preserving the functionality of
the old timer heap. |
| 123 // See crbug.com/328700. | 117 // See crbug.com/328700. |
| 124 if (m_repeatInterval) | 118 if (m_repeatInterval) |
| 125 setNextFireTime(monotonicallyIncreasingTime(), m_repeatInterval); | 119 setNextFireTime(monotonicallyIncreasingTime(), m_repeatInterval); |
| 126 fired(); | 120 fired(); |
| 127 TRACE_EVENT_SET_SAMPLING_STATE("blink", "Sleeping"); | 121 TRACE_EVENT_SET_SAMPLING_STATE("blink", "Sleeping"); |
| 128 } | 122 } |
| 129 | 123 |
| 130 void TimerBase::didChangeAlignmentInterval(double now) | 124 void TimerBase::didChangeAlignmentInterval(double now) |
| 131 { | 125 { |
| 132 setNextFireTime(now, m_unalignedNextFireTime - now); | 126 setNextFireTime(now, m_unalignedNextFireTime - now); |
| 133 } | 127 } |
| 134 | 128 |
| 135 double TimerBase::nextUnalignedFireInterval() const | 129 double TimerBase::nextUnalignedFireInterval() const |
| 136 { | 130 { |
| 137 ASSERT(isActive()); | 131 ASSERT(isActive()); |
| 138 return std::max(m_unalignedNextFireTime - monotonicallyIncreasingTime(), 0.0
); | 132 return std::max(m_unalignedNextFireTime - monotonicallyIncreasingTime(), 0.0
); |
| 139 } | 133 } |
| 140 | 134 |
| 141 } // namespace blink | 135 } // namespace blink |
| OLD | NEW |