Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(571)

Side by Side Diff: base/message_loop/message_loop_proxy_impl.cc

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

Powered by Google App Engine
This is Rietveld 408576698