OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/message_loop/message_loop_proxy_impl.h" | 5 #include "base/message_loop/message_loop_proxy_impl.h" |
6 | 6 |
7 #include "base/debug/trace_event.h" | |
7 #include "base/location.h" | 8 #include "base/location.h" |
8 #include "base/threading/thread_restrictions.h" | 9 #include "base/threading/thread_restrictions.h" |
9 | 10 |
10 namespace base { | 11 namespace base { |
11 | 12 |
12 MessageLoopProxyImpl::~MessageLoopProxyImpl() { | 13 MessageLoopProxyImpl::~MessageLoopProxyImpl() { |
13 } | 14 } |
14 | 15 |
15 bool MessageLoopProxyImpl::PostDelayedTask( | 16 bool MessageLoopProxyImpl::PostDelayedTask( |
16 const tracked_objects::Location& from_here, | 17 const tracked_objects::Location& from_here, |
17 const base::Closure& task, | 18 const base::Closure& task, |
18 base::TimeDelta delay) { | 19 base::TimeDelta delay) { |
19 return PostTaskHelper(from_here, task, delay, true); | 20 DCHECK(!task.is_null()) << from_here.ToString(); |
21 return AddToIncomingQueue(from_here, task, delay, true); | |
20 } | 22 } |
21 | 23 |
22 bool MessageLoopProxyImpl::PostNonNestableDelayedTask( | 24 bool MessageLoopProxyImpl::PostNonNestableDelayedTask( |
23 const tracked_objects::Location& from_here, | 25 const tracked_objects::Location& from_here, |
24 const base::Closure& task, | 26 const base::Closure& task, |
25 base::TimeDelta delay) { | 27 base::TimeDelta delay) { |
26 return PostTaskHelper(from_here, task, delay, false); | 28 DCHECK(!task.is_null()) << from_here.ToString(); |
29 return AddToIncomingQueue(from_here, task, delay, false); | |
27 } | 30 } |
28 | 31 |
29 bool MessageLoopProxyImpl::RunsTasksOnCurrentThread() const { | 32 bool MessageLoopProxyImpl::RunsTasksOnCurrentThread() const { |
30 // We shouldn't use MessageLoop::current() since it uses LazyInstance which | 33 // We shouldn't use MessageLoop::current() since it uses LazyInstance which |
31 // may be deleted by ~AtExitManager when a WorkerPool thread calls this | 34 // may be deleted by ~AtExitManager when a WorkerPool thread calls this |
32 // function. | 35 // function. |
33 // http://crbug.com/63678 | 36 // http://crbug.com/63678 |
34 base::ThreadRestrictions::ScopedAllowSingleton allow_singleton; | 37 base::ThreadRestrictions::ScopedAllowSingleton allow_singleton; |
35 AutoLock lock(message_loop_lock_); | 38 AutoLock lock(incoming_queue_lock_); |
36 return (target_message_loop_ && | 39 return (message_loop_ && (MessageLoop::current() == message_loop_)); |
37 (MessageLoop::current() == target_message_loop_)); | |
38 } | 40 } |
39 | 41 |
40 // MessageLoop::DestructionObserver implementation | 42 // MessageLoop::DestructionObserver implementation |
41 void MessageLoopProxyImpl::WillDestroyCurrentMessageLoop() { | 43 void MessageLoopProxyImpl::WillDestroyCurrentMessageLoop() { |
42 AutoLock lock(message_loop_lock_); | 44 #if defined(OS_WIN) |
43 target_message_loop_ = NULL; | 45 // If we left the high-resolution timer activated, deactivate it now. |
46 // Doing this is not-critical, it is mainly to make sure we track | |
47 // the high resolution timer activations properly in our unit tests. | |
48 if (!high_resolution_timer_expiration_.is_null()) { | |
49 Time::ActivateHighResolutionTimer(false); | |
50 high_resolution_timer_expiration_ = TimeTicks(); | |
51 } | |
52 #endif | |
53 | |
54 AutoLock lock(incoming_queue_lock_); | |
55 message_loop_ = NULL; | |
44 } | 56 } |
45 | 57 |
46 void MessageLoopProxyImpl::OnDestruct() const { | 58 void MessageLoopProxyImpl::OnDestruct() const { |
47 // We shouldn't use MessageLoop::current() since it uses LazyInstance which | 59 // We shouldn't use MessageLoop::current() since it uses LazyInstance which |
48 // may be deleted by ~AtExitManager when a WorkerPool thread calls this | 60 // may be deleted by ~AtExitManager when a WorkerPool thread calls this |
49 // function. | 61 // function. |
50 // http://crbug.com/63678 | 62 // http://crbug.com/63678 |
51 base::ThreadRestrictions::ScopedAllowSingleton allow_singleton; | 63 base::ThreadRestrictions::ScopedAllowSingleton allow_singleton; |
52 bool delete_later = false; | 64 bool delete_later = false; |
53 { | 65 { |
54 AutoLock lock(message_loop_lock_); | 66 AutoLock lock(incoming_queue_lock_); |
55 if (target_message_loop_ && | 67 if (message_loop_ && (MessageLoop::current() != message_loop_)) { |
rvargas (doing something else)
2013/06/28 22:59:44
This should be always false. (we should only reach
alexeypa (please no reviews)
2013/07/03 18:56:48
Done. I deleted OnDestruct and added DCHECK to the
| |
56 (MessageLoop::current() != target_message_loop_)) { | 68 message_loop_->DeleteSoon(FROM_HERE, this); |
57 target_message_loop_->DeleteSoon(FROM_HERE, this); | |
58 delete_later = true; | 69 delete_later = true; |
59 } | 70 } |
60 } | 71 } |
61 if (!delete_later) | 72 if (!delete_later) |
62 delete this; | 73 delete this; |
63 } | 74 } |
64 | 75 |
65 MessageLoopProxyImpl::MessageLoopProxyImpl() | 76 MessageLoopProxyImpl::MessageLoopProxyImpl() |
66 : target_message_loop_(MessageLoop::current()) { | 77 : message_loop_(MessageLoop::current()), |
78 next_sequence_num_(0) { | |
67 } | 79 } |
68 | 80 |
69 bool MessageLoopProxyImpl::PostTaskHelper( | 81 bool MessageLoopProxyImpl::AddToIncomingQueue( |
70 const tracked_objects::Location& from_here, const base::Closure& task, | 82 const tracked_objects::Location& from_here, |
71 base::TimeDelta delay, bool nestable) { | 83 const Closure& task, |
72 AutoLock lock(message_loop_lock_); | 84 TimeDelta delay, |
73 if (target_message_loop_) { | 85 bool nestable) { |
74 if (nestable) { | 86 PendingTask pending_task( |
75 target_message_loop_->PostDelayedTask(from_here, task, delay); | 87 from_here, task, CalculateDelayedRuntime(delay), nestable); |
76 } else { | 88 |
77 target_message_loop_->PostNonNestableDelayedTask(from_here, task, delay); | 89 AutoLock locked(incoming_queue_lock_); |
90 return PostPendingTask(&pending_task); | |
91 } | |
92 | |
93 TimeTicks MessageLoopProxyImpl::CalculateDelayedRuntime(TimeDelta delay) { | |
94 TimeTicks delayed_run_time; | |
95 if (delay > TimeDelta()) { | |
96 delayed_run_time = TimeTicks::Now() + delay; | |
97 | |
98 #if defined(OS_WIN) | |
99 if (high_resolution_timer_expiration_.is_null()) { | |
100 // Windows timers are granular to 15.6ms. If we only set high-res | |
101 // timers for those under 15.6ms, then a 18ms timer ticks at ~32ms, | |
102 // which as a percentage is pretty inaccurate. So enable high | |
103 // res timers for any timer which is within 2x of the granularity. | |
104 // This is a tradeoff between accuracy and power management. | |
105 bool needs_high_res_timers = delay.InMilliseconds() < | |
106 (2 * Time::kMinLowResolutionThresholdMs); | |
107 if (needs_high_res_timers) { | |
108 if (Time::ActivateHighResolutionTimer(true)) { | |
109 high_resolution_timer_expiration_ = TimeTicks::Now() + | |
rvargas (doing something else)
2013/06/28 22:59:44
This looks like racing to me (same thing on the ol
alexeypa (please no reviews)
2013/07/03 18:56:48
Done.
| |
110 TimeDelta::FromMilliseconds( | |
111 MessageLoop::kHighResolutionTimerModeLeaseTimeMs); | |
112 } | |
113 } | |
78 } | 114 } |
79 return true; | 115 #endif |
116 } else { | |
117 DCHECK_EQ(delay.InMilliseconds(), 0) << "delay should not be negative"; | |
80 } | 118 } |
81 return false; | 119 |
120 #if defined(OS_WIN) | |
121 if (!high_resolution_timer_expiration_.is_null()) { | |
122 if (TimeTicks::Now() > high_resolution_timer_expiration_) { | |
123 Time::ActivateHighResolutionTimer(false); | |
124 high_resolution_timer_expiration_ = TimeTicks(); | |
125 } | |
126 } | |
127 #endif | |
128 | |
129 return delayed_run_time; | |
130 } | |
131 | |
132 bool MessageLoopProxyImpl::IsHishResolutionTimersEnabledForTest() { | |
133 #if defined(OS_WIN) | |
134 return !high_resolution_timer_expiration_.is_null(); | |
135 #else | |
136 return true; | |
137 #endif | |
138 } | |
139 | |
140 bool MessageLoopProxyImpl::IsIdleForTest() { | |
141 AutoLock lock(incoming_queue_lock_); | |
142 return incoming_queue_.empty(); | |
143 } | |
144 | |
145 bool MessageLoopProxyImpl::TryAddToIncomingQueue( | |
146 const tracked_objects::Location& from_here, | |
147 const Closure& task) { | |
148 PendingTask pending_task( | |
149 from_here, task, CalculateDelayedRuntime(TimeDelta()), true); | |
150 if (!incoming_queue_lock_.Try()) { | |
151 pending_task.task.Reset(); | |
152 return false; | |
153 } | |
154 | |
155 AutoLock locked(incoming_queue_lock_, AutoLock::AlreadyAcquired()); | |
156 return PostPendingTask(&pending_task); | |
157 } | |
158 | |
159 bool MessageLoopProxyImpl::PostPendingTask(PendingTask* pending_task) { | |
160 // Warning: Don't try to short-circuit, and handle this thread's tasks more | |
161 // directly, as it could starve handling of foreign threads. Put every task | |
162 // into this queue. | |
163 | |
164 // This should only be called while the lock is taken. | |
165 incoming_queue_lock_.AssertAcquired(); | |
166 | |
167 // Initialize the sequence number. The sequence number is used for delayed | |
168 // tasks (to faciliate FIFO sorting when two tasks have the same | |
169 // delayed_run_time value) and for identifying the task in about:tracing. | |
170 pending_task->sequence_num = next_sequence_num_++; | |
171 | |
172 TRACE_EVENT_FLOW_BEGIN0("task", "MessageLoop::PostTask", | |
173 TRACE_ID_MANGLE(message_loop_->GetTaskTraceID(*pending_task))); | |
174 | |
175 bool was_empty = incoming_queue_.empty(); | |
176 incoming_queue_.push(*pending_task); | |
177 pending_task->task.Reset(); | |
178 | |
179 // Wake up the pump. | |
180 if (was_empty && message_loop_) | |
181 message_loop_->StartPump(); | |
182 | |
183 return !!message_loop_; | |
rvargas (doing something else)
2013/06/28 22:59:44
early return before adding the task to the queue.
alexeypa (please no reviews)
2013/07/03 18:56:48
Done.
| |
184 } | |
185 | |
186 void MessageLoopProxyImpl::ReloadWorkQueue(TaskQueue* work_queue) { | |
187 // We can improve performance of our loading tasks from |incoming_queue_| to | |
188 // |*work_queue| by waiting until the last minute (|*work_queue| is empty) to | |
189 // load. That reduces the number of locks-per-task significantly when our | |
190 // queues get large. | |
191 if (!work_queue->empty()) | |
rvargas (doing something else)
2013/06/28 22:59:44
nit: we should probably do this check at the calle
alexeypa (please no reviews)
2013/07/03 18:56:48
Done.
| |
192 return; // Wait till we *really* need to lock and load. | |
193 | |
194 // Acquire all we can from the inter-thread queue with one lock acquisition. | |
195 AutoLock lock(incoming_queue_lock_); | |
196 if (!incoming_queue_.empty()) | |
197 incoming_queue_.Swap(work_queue); // Constant time | |
198 | |
199 DCHECK(incoming_queue_.empty()); | |
82 } | 200 } |
83 | 201 |
84 scoped_refptr<MessageLoopProxy> | 202 scoped_refptr<MessageLoopProxy> |
85 MessageLoopProxy::current() { | 203 MessageLoopProxy::current() { |
86 MessageLoop* cur_loop = MessageLoop::current(); | 204 MessageLoop* cur_loop = MessageLoop::current(); |
87 if (!cur_loop) | 205 if (!cur_loop) |
88 return NULL; | 206 return NULL; |
89 return cur_loop->message_loop_proxy(); | 207 return cur_loop->message_loop_proxy(); |
90 } | 208 } |
91 | 209 |
92 } // namespace base | 210 } // namespace base |
OLD | NEW |