Chromium Code Reviews| 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 25 matching lines...) Expand all Loading... | |
| 36 #include <algorithm> | 36 #include <algorithm> |
| 37 #include <limits.h> | 37 #include <limits.h> |
| 38 #include <limits> | 38 #include <limits> |
| 39 #include <math.h> | 39 #include <math.h> |
| 40 | 40 |
| 41 namespace blink { | 41 namespace blink { |
| 42 | 42 |
| 43 TimerBase::TimerBase(WebTaskRunner* webTaskRunner) | 43 TimerBase::TimerBase(WebTaskRunner* webTaskRunner) |
| 44 : m_nextFireTime(0) | 44 : m_nextFireTime(0) |
| 45 , m_repeatInterval(0) | 45 , m_repeatInterval(0) |
| 46 , m_cancellableTimerTask(nullptr) | |
| 47 , m_webTaskRunner(webTaskRunner->clone()) | 46 , m_webTaskRunner(webTaskRunner->clone()) |
| 48 #if DCHECK_IS_ON() | 47 #if DCHECK_IS_ON() |
| 49 , m_thread(currentThread()) | 48 , m_thread(currentThread()) |
| 50 #endif | 49 #endif |
| 50 , m_weakPtrFactory(this) | |
| 51 { | 51 { |
| 52 ASSERT(m_webTaskRunner); | 52 ASSERT(m_webTaskRunner); |
| 53 } | 53 } |
| 54 | 54 |
| 55 TimerBase::~TimerBase() | 55 TimerBase::~TimerBase() |
| 56 { | 56 { |
| 57 stop(); | 57 stop(); |
| 58 } | 58 } |
| 59 | 59 |
| 60 void TimerBase::start(double nextFireInterval, double repeatInterval, const WebT raceLocation& caller) | 60 void TimerBase::start(double nextFireInterval, double repeatInterval, const WebT raceLocation& caller) |
| 61 { | 61 { |
| 62 ASSERT(m_thread == currentThread()); | 62 ASSERT(m_thread == currentThread()); |
| 63 | 63 |
| 64 m_location = caller; | 64 m_location = caller; |
| 65 m_repeatInterval = repeatInterval; | 65 m_repeatInterval = repeatInterval; |
| 66 setNextFireTime(timerMonotonicallyIncreasingTime(), nextFireInterval); | 66 setNextFireTime(timerMonotonicallyIncreasingTime(), nextFireInterval); |
| 67 } | 67 } |
| 68 | 68 |
| 69 void TimerBase::stop() | 69 void TimerBase::stop() |
| 70 { | 70 { |
| 71 ASSERT(m_thread == currentThread()); | 71 ASSERT(m_thread == currentThread()); |
| 72 | 72 |
| 73 m_repeatInterval = 0; | 73 m_repeatInterval = 0; |
| 74 m_nextFireTime = 0; | 74 m_nextFireTime = 0; |
| 75 if (m_cancellableTimerTask) | 75 m_weakPtrFactory.revokeAll(); |
| 76 m_cancellableTimerTask->cancel(); | |
| 77 m_cancellableTimerTask = nullptr; | |
| 78 } | 76 } |
| 79 | 77 |
| 80 double TimerBase::nextFireInterval() const | 78 double TimerBase::nextFireInterval() const |
| 81 { | 79 { |
| 82 ASSERT(isActive()); | 80 ASSERT(isActive()); |
| 83 double current = timerMonotonicallyIncreasingTime(); | 81 double current = timerMonotonicallyIncreasingTime(); |
| 84 if (m_nextFireTime < current) | 82 if (m_nextFireTime < current) |
| 85 return 0; | 83 return 0; |
| 86 return m_nextFireTime - current; | 84 return m_nextFireTime - current; |
| 87 } | 85 } |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 104 } | 102 } |
| 105 | 103 |
| 106 void TimerBase::setNextFireTime(double now, double delay) | 104 void TimerBase::setNextFireTime(double now, double delay) |
| 107 { | 105 { |
| 108 ASSERT(m_thread == currentThread()); | 106 ASSERT(m_thread == currentThread()); |
| 109 | 107 |
| 110 double newTime = now + delay; | 108 double newTime = now + delay; |
| 111 | 109 |
| 112 if (m_nextFireTime != newTime) { | 110 if (m_nextFireTime != newTime) { |
| 113 m_nextFireTime = newTime; | 111 m_nextFireTime = newTime; |
| 114 if (m_cancellableTimerTask) | 112 |
| 115 m_cancellableTimerTask->cancel(); | 113 // Cancel any previously posted task. |
| 116 m_cancellableTimerTask = new CancellableTimerTask(this); | 114 m_weakPtrFactory.revokeAll(); |
| 117 | 115 |
| 118 double delayMs = 1000.0 * (newTime - now); | 116 double delayMs = 1000.0 * (newTime - now); |
| 119 timerTaskRunner()->postDelayedTask(m_location, m_cancellableTimerTask, d elayMs); | 117 timerTaskRunner()->postDelayedTask(m_location, base::Bind(&TimerBase::ru nInternal, m_weakPtrFactory.createWeakPtr()), delayMs); |
| 120 } | 118 } |
| 121 } | 119 } |
| 122 | 120 |
| 123 NO_LAZY_SWEEP_SANITIZE_ADDRESS | 121 NO_LAZY_SWEEP_SANITIZE_ADDRESS |
| 124 void TimerBase::runInternal() | 122 void TimerBase::runInternal() |
| 125 { | 123 { |
| 126 if (!canFire()) | 124 if (!canFire()) |
| 127 return; | 125 return; |
| 128 | 126 |
| 127 m_weakPtrFactory.revokeAll(); | |
|
haraken
2016/09/09 02:34:07
Maybe do we want to move this to above line 124?
alex clarke (OOO till 29th)
2016/09/09 09:40:58
We do need to call it here to match the previous b
alex clarke (OOO till 29th)
2016/09/12 11:07:18
It looks moving this above caused the bot failures
| |
| 128 | |
| 129 TRACE_EVENT0("blink", "TimerBase::run"); | 129 TRACE_EVENT0("blink", "TimerBase::run"); |
| 130 #if DCHECK_IS_ON() | 130 #if DCHECK_IS_ON() |
| 131 DCHECK_EQ(m_thread, currentThread()) << "Timer posted by " << m_location.fun ction_name() << " " << m_location.file_name() << " was run on a different thread "; | 131 DCHECK_EQ(m_thread, currentThread()) << "Timer posted by " << m_location.fun ction_name() << " " << m_location.file_name() << " was run on a different thread "; |
| 132 #endif | 132 #endif |
| 133 TRACE_EVENT_SET_SAMPLING_STATE("blink", "BlinkInternal"); | 133 TRACE_EVENT_SET_SAMPLING_STATE("blink", "BlinkInternal"); |
| 134 | 134 |
| 135 if (m_repeatInterval) { | 135 if (m_repeatInterval) { |
| 136 double now = timerMonotonicallyIncreasingTime(); | 136 double now = timerMonotonicallyIncreasingTime(); |
| 137 // This computation should be drift free, and it will cope if we miss a beat, | 137 // This computation should be drift free, and it will cope if we miss a beat, |
| 138 // which can easily happen if the thread is busy. It will also cope if we get | 138 // which can easily happen if the thread is busy. It will also cope if we get |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 152 return a->m_nextFireTime < b->m_nextFireTime; | 152 return a->m_nextFireTime < b->m_nextFireTime; |
| 153 } | 153 } |
| 154 | 154 |
| 155 // static | 155 // static |
| 156 double TimerBase::timerMonotonicallyIncreasingTime() const | 156 double TimerBase::timerMonotonicallyIncreasingTime() const |
| 157 { | 157 { |
| 158 return timerTaskRunner()->monotonicallyIncreasingVirtualTimeSeconds(); | 158 return timerTaskRunner()->monotonicallyIncreasingVirtualTimeSeconds(); |
| 159 } | 159 } |
| 160 | 160 |
| 161 } // namespace blink | 161 } // namespace blink |
| OLD | NEW |