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 22 matching lines...) Expand all Loading... |
33 #include "wtf/Atomics.h" | 33 #include "wtf/Atomics.h" |
34 #include "wtf/CurrentTime.h" | 34 #include "wtf/CurrentTime.h" |
35 #include "wtf/HashSet.h" | 35 #include "wtf/HashSet.h" |
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() : TimerBase(Platform::current()->currentThread()->schedul
er()->timerTaskRunner()) { } | 43 TimerBase::TimerBase() |
| 44 : m_nextFireTime(0) |
| 45 , m_repeatInterval(0) |
| 46 , m_cancellableTimerTask(nullptr) |
| 47 , m_thread(Platform::current()->currentThread()) |
| 48 , m_webTaskRunner(m_thread->scheduler()->timerTaskRunner()) |
| 49 { |
| 50 } |
44 | 51 |
45 TimerBase::TimerBase(WebTaskRunner* webTaskRunner) | 52 TimerBase::TimerBase(WebTaskRunner* webTaskRunner) |
46 : m_nextFireTime(0) | 53 : m_nextFireTime(0) |
47 , m_repeatInterval(0) | 54 , m_repeatInterval(0) |
48 , m_cancellableTimerTask(nullptr) | 55 , m_cancellableTimerTask(nullptr) |
| 56 , m_thread(Platform::current()->currentThread()) |
49 , m_webTaskRunner(webTaskRunner) | 57 , m_webTaskRunner(webTaskRunner) |
50 #if ENABLE(ASSERT) | |
51 , m_thread(currentThread()) | |
52 #endif | |
53 { | 58 { |
54 ASSERT(m_webTaskRunner); | 59 ASSERT(m_webTaskRunner); |
55 } | 60 } |
56 | 61 |
57 TimerBase::~TimerBase() | 62 TimerBase::~TimerBase() |
58 { | 63 { |
59 stop(); | 64 stop(); |
60 } | 65 } |
61 | 66 |
62 void TimerBase::start(double nextFireInterval, double repeatInterval, const WebT
raceLocation& caller) | 67 void TimerBase::start(double nextFireInterval, double repeatInterval, const WebT
raceLocation& caller) |
63 { | 68 { |
64 ASSERT(m_thread == currentThread()); | 69 ASSERT(m_thread->isCurrentThread()); |
65 | 70 |
66 m_location = caller; | 71 m_location = caller; |
67 m_repeatInterval = repeatInterval; | 72 m_repeatInterval = repeatInterval; |
68 setNextFireTime(monotonicallyIncreasingTime(), nextFireInterval); | 73 setNextFireTime(timerMonotonicallyIncreasingTime(), nextFireInterval); |
69 } | 74 } |
70 | 75 |
71 void TimerBase::stop() | 76 void TimerBase::stop() |
72 { | 77 { |
73 ASSERT(m_thread == currentThread()); | 78 ASSERT(m_thread->isCurrentThread()); |
74 | 79 |
75 m_repeatInterval = 0; | 80 m_repeatInterval = 0; |
76 m_nextFireTime = 0; | 81 m_nextFireTime = 0; |
77 if (m_cancellableTimerTask) | 82 if (m_cancellableTimerTask) |
78 m_cancellableTimerTask->cancel(); | 83 m_cancellableTimerTask->cancel(); |
79 m_cancellableTimerTask = nullptr; | 84 m_cancellableTimerTask = nullptr; |
80 } | 85 } |
81 | 86 |
82 double TimerBase::nextFireInterval() const | 87 double TimerBase::nextFireInterval() const |
83 { | 88 { |
84 ASSERT(isActive()); | 89 ASSERT(isActive()); |
85 double current = monotonicallyIncreasingTime(); | 90 double current = timerMonotonicallyIncreasingTime(); |
86 if (m_nextFireTime < current) | 91 if (m_nextFireTime < current) |
87 return 0; | 92 return 0; |
88 return m_nextFireTime - current; | 93 return m_nextFireTime - current; |
89 } | 94 } |
90 | 95 |
91 WebTaskRunner* TimerBase::timerTaskRunner() | 96 WebTaskRunner* TimerBase::timerTaskRunner() |
92 { | 97 { |
93 return m_webTaskRunner; | 98 return m_webTaskRunner; |
94 } | 99 } |
95 | 100 |
96 void TimerBase::setNextFireTime(double now, double delay) | 101 void TimerBase::setNextFireTime(double now, double delay) |
97 { | 102 { |
98 ASSERT(m_thread == currentThread()); | 103 ASSERT(m_thread->isCurrentThread()); |
99 | 104 |
100 double newTime = now + delay; | 105 double newTime = now + delay; |
101 | 106 |
102 if (m_nextFireTime != newTime) { | 107 if (m_nextFireTime != newTime) { |
103 m_nextFireTime = newTime; | 108 m_nextFireTime = newTime; |
104 if (m_cancellableTimerTask) | 109 if (m_cancellableTimerTask) |
105 m_cancellableTimerTask->cancel(); | 110 m_cancellableTimerTask->cancel(); |
106 m_cancellableTimerTask = new CancellableTimerTask(this); | 111 m_cancellableTimerTask = new CancellableTimerTask(this); |
107 | 112 |
108 double delayMs = 1000.0 * (newTime - now); | 113 double delayMs = 1000.0 * (newTime - now); |
109 timerTaskRunner()->postDelayedTask(m_location, m_cancellableTimerTask, d
elayMs); | 114 timerTaskRunner()->postDelayedTask(m_location, m_cancellableTimerTask, d
elayMs); |
110 } | 115 } |
111 } | 116 } |
112 | 117 |
113 NO_LAZY_SWEEP_SANITIZE_ADDRESS | 118 NO_LAZY_SWEEP_SANITIZE_ADDRESS |
114 void TimerBase::runInternal() | 119 void TimerBase::runInternal() |
115 { | 120 { |
116 if (!canFire()) | 121 if (!canFire()) |
117 return; | 122 return; |
118 | 123 |
119 TRACE_EVENT0("blink", "TimerBase::run"); | 124 TRACE_EVENT0("blink", "TimerBase::run"); |
120 ASSERT_WITH_MESSAGE(m_thread == currentThread(), "Timer posted by %s %s was
run on a different thread", m_location.functionName(), m_location.fileName()); | 125 ASSERT_WITH_MESSAGE(m_thread->isCurrentThread(), "Timer posted by %s %s was
run on a different thread", m_location.functionName(), m_location.fileName()); |
121 TRACE_EVENT_SET_SAMPLING_STATE("blink", "BlinkInternal"); | 126 TRACE_EVENT_SET_SAMPLING_STATE("blink", "BlinkInternal"); |
122 | 127 |
123 if (m_repeatInterval) { | 128 if (m_repeatInterval) { |
124 double now = monotonicallyIncreasingTime(); | 129 double now = timerMonotonicallyIncreasingTime(); |
125 // This computation should be drift free, and it will cope if we miss a
beat, | 130 // This computation should be drift free, and it will cope if we miss a
beat, |
126 // which can easily happen if the thread is busy. It will also cope if
we get | 131 // which can easily happen if the thread is busy. It will also cope if
we get |
127 // called slightly before m_unalignedNextFireTime, which can happen due
to lack | 132 // called slightly before m_unalignedNextFireTime, which can happen due
to lack |
128 // of timer precision. | 133 // of timer precision. |
129 double intervalToNextFireTime = m_repeatInterval - fmod(now - m_nextFire
Time, m_repeatInterval); | 134 double intervalToNextFireTime = m_repeatInterval - fmod(now - m_nextFire
Time, m_repeatInterval); |
130 setNextFireTime(monotonicallyIncreasingTime(), intervalToNextFireTime); | 135 setNextFireTime(timerMonotonicallyIncreasingTime(), intervalToNextFireTi
me); |
131 } else { | 136 } else { |
132 m_nextFireTime = 0; | 137 m_nextFireTime = 0; |
133 } | 138 } |
134 fired(); | 139 fired(); |
135 TRACE_EVENT_SET_SAMPLING_STATE("blink", "Sleeping"); | 140 TRACE_EVENT_SET_SAMPLING_STATE("blink", "Sleeping"); |
136 } | 141 } |
137 | 142 |
138 bool TimerBase::Comparator::operator()(const TimerBase* a, const TimerBase* b) c
onst | 143 bool TimerBase::Comparator::operator()(const TimerBase* a, const TimerBase* b) c
onst |
139 { | 144 { |
140 return a->m_nextFireTime < b->m_nextFireTime; | 145 return a->m_nextFireTime < b->m_nextFireTime; |
141 } | 146 } |
142 | 147 |
143 // static | 148 // static |
144 WebTaskRunner* TimerBase::UnthrottledWebTaskRunner() | 149 WebTaskRunner* TimerBase::UnthrottledWebTaskRunner() |
145 { | 150 { |
146 return Platform::current()->currentThread()->taskRunner(); | 151 return Platform::current()->currentThread()->taskRunner(); |
147 } | 152 } |
148 | 153 |
149 } // namespace blink | 154 } // namespace blink |
OLD | NEW |