OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (C) 2006, 2008 Apple Inc. All rights reserved. |
| 3 * Copyright (C) 2009 Google Inc. All rights reserved. |
| 4 * |
| 5 * Redistribution and use in source and binary forms, with or without |
| 6 * modification, are permitted provided that the following conditions |
| 7 * are met: |
| 8 * 1. Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. |
| 10 * 2. Redistributions in binary form must reproduce the above copyright |
| 11 * notice, this list of conditions and the following disclaimer in the |
| 12 * documentation and/or other materials provided with the distribution. |
| 13 * |
| 14 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY |
| 15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR |
| 18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 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. |
| 25 */ |
| 26 |
| 27 #include "config.h" |
| 28 #include "platform/ThreadTimers.h" |
| 29 |
| 30 #include "platform/PlatformThreadData.h" |
| 31 #include "platform/SharedTimer.h" |
| 32 #include "platform/Timer.h" |
| 33 #include "platform/TraceEvent.h" |
| 34 #include "public/platform/Platform.h" |
| 35 #include "public/platform/WebScheduler.h" |
| 36 #include "wtf/AddressSanitizer.h" |
| 37 #include "wtf/CurrentTime.h" |
| 38 #include "wtf/MainThread.h" |
| 39 |
| 40 namespace blink { |
| 41 |
| 42 // Fire timers for this length of time, and then quit to let the run loop proces
s user input events. |
| 43 // 100ms is about a perceptable delay in UI, so use a half of that as a threshol
d. |
| 44 // This is to prevent UI freeze when there are too many timers or machine perfor
mance is low. |
| 45 static const double maxDurationOfFiringTimers = 0.050; |
| 46 |
| 47 // Timers are created, started and fired on the same thread, and each thread has
its own ThreadTimers |
| 48 // copy to keep the heap and a set of currently firing timers. |
| 49 |
| 50 static PassOwnPtr<MainThreadSharedTimer> mainThreadSharedTimer() |
| 51 { |
| 52 return adoptPtr(new MainThreadSharedTimer); |
| 53 } |
| 54 |
| 55 ThreadTimers::ThreadTimers() |
| 56 : m_sharedTimer(0) |
| 57 , m_firingTimers(false) |
| 58 , m_pendingSharedTimerFireTime(0) |
| 59 { |
| 60 if (isMainThread()) |
| 61 setSharedTimer(mainThreadSharedTimer()); |
| 62 } |
| 63 |
| 64 // A worker thread may initialize SharedTimer after some timers are created. |
| 65 // Also, SharedTimer can be replaced with 0 before all timers are destroyed. |
| 66 void ThreadTimers::setSharedTimer(PassOwnPtr<SharedTimer> sharedTimer) |
| 67 { |
| 68 if (m_sharedTimer) { |
| 69 m_sharedTimer->setFiredFunction(0); |
| 70 m_sharedTimer->stop(); |
| 71 m_pendingSharedTimerFireTime = 0; |
| 72 } |
| 73 |
| 74 m_sharedTimer = sharedTimer; |
| 75 |
| 76 if (m_sharedTimer) { |
| 77 m_sharedTimer->setFiredFunction(ThreadTimers::sharedTimerFired); |
| 78 updateSharedTimer(); |
| 79 } |
| 80 } |
| 81 |
| 82 NO_LAZY_SWEEP_SANITIZE_ADDRESS |
| 83 void ThreadTimers::updateSharedTimer() |
| 84 { |
| 85 if (!m_sharedTimer) |
| 86 return; |
| 87 |
| 88 if (m_firingTimers || m_timerHeap.isEmpty()) { |
| 89 m_pendingSharedTimerFireTime = 0; |
| 90 m_sharedTimer->stop(); |
| 91 } else { |
| 92 double nextFireTime = m_timerHeap.first()->m_nextFireTime; |
| 93 double currentMonotonicTime = monotonicallyIncreasingTime(); |
| 94 if (m_pendingSharedTimerFireTime) { |
| 95 // No need to restart the timer if both the pending fire time and th
e new fire time are in the past. |
| 96 if (m_pendingSharedTimerFireTime <= currentMonotonicTime && nextFire
Time <= currentMonotonicTime) |
| 97 return; |
| 98 } |
| 99 m_pendingSharedTimerFireTime = nextFireTime; |
| 100 m_sharedTimer->setFireInterval(std::max(nextFireTime - currentMonotonicT
ime, 0.0)); |
| 101 } |
| 102 } |
| 103 |
| 104 void ThreadTimers::sharedTimerFired() |
| 105 { |
| 106 TRACE_EVENT_SET_SAMPLING_STATE("blink", "BlinkInternal"); |
| 107 |
| 108 // Redirect to non-static method. |
| 109 PlatformThreadData::current().threadTimers().sharedTimerFiredInternal(); |
| 110 |
| 111 TRACE_EVENT_SET_SAMPLING_STATE("blink", "Sleeping"); |
| 112 } |
| 113 |
| 114 NO_LAZY_SWEEP_SANITIZE_ADDRESS |
| 115 void ThreadTimers::sharedTimerFiredInternal() |
| 116 { |
| 117 // Do a re-entrancy check. |
| 118 if (m_firingTimers) |
| 119 return; |
| 120 m_firingTimers = true; |
| 121 m_pendingSharedTimerFireTime = 0; |
| 122 |
| 123 double fireTime = monotonicallyIncreasingTime(); |
| 124 double timeToQuit = fireTime + maxDurationOfFiringTimers; |
| 125 |
| 126 while (!m_timerHeap.isEmpty() && m_timerHeap.first()->m_nextFireTime <= fire
Time) { |
| 127 TimerBase& timer = *m_timerHeap.first(); |
| 128 timer.m_nextFireTime = 0; |
| 129 timer.m_unalignedNextFireTime = 0; |
| 130 timer.heapDeleteMin(); |
| 131 |
| 132 double interval = timer.repeatInterval(); |
| 133 timer.setNextFireTime(interval ? fireTime + interval : 0); |
| 134 |
| 135 TRACE_EVENT2("blink", "ThreadTimers::sharedTimerFiredInternal", |
| 136 "src_file", timer.location().fileName(), |
| 137 "src_func", timer.location().functionName()); |
| 138 |
| 139 // Once the timer has been fired, it may be deleted, so do nothing else
with it after this point. |
| 140 timer.fired(); |
| 141 |
| 142 // Catch the case where the timer asked timers to fire in a nested event
loop, or we are over time limit. |
| 143 if (!m_firingTimers || timeToQuit < monotonicallyIncreasingTime() |
| 144 || (isMainThread() && Platform::current()->currentThread()->schedule
r()->shouldYieldForHighPriorityWork())) |
| 145 break; |
| 146 } |
| 147 |
| 148 m_firingTimers = false; |
| 149 |
| 150 updateSharedTimer(); |
| 151 } |
| 152 |
| 153 } // namespace blink |
OLD | NEW |