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