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

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: rebased 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"
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 MessageLoop::ProxyImpl::ProxyImpl()
14 : message_loop_(MessageLoop::current()),
15 next_sequence_num_(0) {
13 } 16 }
14 17
15 bool MessageLoopProxyImpl::PostDelayedTask( 18 bool MessageLoop::ProxyImpl::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 MessageLoop::ProxyImpl::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 MessageLoop::ProxyImpl::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 44 bool MessageLoop::ProxyImpl::AddToIncomingQueue(
41 void MessageLoopProxyImpl::WillDestroyCurrentMessageLoop() { 45 const tracked_objects::Location& from_here,
42 AutoLock lock(message_loop_lock_); 46 const Closure& task,
43 target_message_loop_ = NULL; 47 TimeDelta delay,
48 bool nestable) {
49 AutoLock locked(incoming_queue_lock_);
50 PendingTask pending_task(
51 from_here, task, CalculateDelayedRuntime(delay), nestable);
52 return PostPendingTask(&pending_task);
44 } 53 }
45 54
46 void MessageLoopProxyImpl::OnDestruct() const { 55 bool MessageLoop::ProxyImpl::IsHishResolutionTimersEnabledForTest() {
47 // We shouldn't use MessageLoop::current() since it uses LazyInstance which 56 #if defined(OS_WIN)
48 // may be deleted by ~AtExitManager when a WorkerPool thread calls this 57 return !high_resolution_timer_expiration_.is_null();
49 // function. 58 #else
50 // http://crbug.com/63678 59 return true;
51 base::ThreadRestrictions::ScopedAllowSingleton allow_singleton; 60 #endif
52 bool delete_later = false; 61 }
53 { 62
54 AutoLock lock(message_loop_lock_); 63 bool MessageLoop::ProxyImpl::IsIdleForTest() {
55 if (target_message_loop_ && 64 AutoLock lock(incoming_queue_lock_);
56 (MessageLoop::current() != target_message_loop_)) { 65 return incoming_queue_.empty();
57 target_message_loop_->DeleteSoon(FROM_HERE, this); 66 }
58 delete_later = true; 67
68 void MessageLoop::ProxyImpl::ReloadWorkQueue(TaskQueue* work_queue) {
69 // Make sure no tasks are lost.
70 DCHECK(work_queue->empty());
71
72 // Acquire all we can from the inter-thread queue with one lock acquisition.
73 AutoLock lock(incoming_queue_lock_);
74 if (!incoming_queue_.empty())
75 incoming_queue_.Swap(work_queue); // Constant time
76
77 DCHECK(incoming_queue_.empty());
78 }
79
80 bool MessageLoop::ProxyImpl::TryAddToIncomingQueue(
81 const tracked_objects::Location& from_here,
82 const Closure& task) {
83 if (!incoming_queue_lock_.Try()) {
84 // Reset |task|.
85 Closure local_task = task;
86 return false;
87 }
88
89 AutoLock locked(incoming_queue_lock_, AutoLock::AlreadyAcquired());
90 PendingTask pending_task(
91 from_here, task, CalculateDelayedRuntime(TimeDelta()), true);
92 return PostPendingTask(&pending_task);
93 }
94
95 void MessageLoop::ProxyImpl::WillDestroyCurrentMessageLoop() {
96 #if defined(OS_WIN)
97 // If we left the high-resolution timer activated, deactivate it now.
98 // Doing this is not-critical, it is mainly to make sure we track
99 // the high resolution timer activations properly in our unit tests.
100 if (!high_resolution_timer_expiration_.is_null()) {
101 Time::ActivateHighResolutionTimer(false);
102 high_resolution_timer_expiration_ = TimeTicks();
103 }
104 #endif
105
106 AutoLock lock(incoming_queue_lock_);
107 message_loop_ = NULL;
108 }
109
110 MessageLoop::ProxyImpl::~ProxyImpl() {
111 // Verify that WillDestroyCurrentMessageLoop() has been called.
112 DCHECK(!message_loop_);
113 }
114
115 TimeTicks MessageLoop::ProxyImpl::CalculateDelayedRuntime(TimeDelta delay) {
116 TimeTicks delayed_run_time;
117 if (delay > TimeDelta()) {
118 delayed_run_time = TimeTicks::Now() + delay;
119
120 #if defined(OS_WIN)
121 if (high_resolution_timer_expiration_.is_null()) {
122 // Windows timers are granular to 15.6ms. If we only set high-res
123 // timers for those under 15.6ms, then a 18ms timer ticks at ~32ms,
124 // which as a percentage is pretty inaccurate. So enable high
125 // res timers for any timer which is within 2x of the granularity.
126 // This is a tradeoff between accuracy and power management.
127 bool needs_high_res_timers = delay.InMilliseconds() <
128 (2 * Time::kMinLowResolutionThresholdMs);
129 if (needs_high_res_timers) {
130 if (Time::ActivateHighResolutionTimer(true)) {
131 high_resolution_timer_expiration_ = TimeTicks::Now() +
132 TimeDelta::FromMilliseconds(
133 MessageLoop::kHighResolutionTimerModeLeaseTimeMs);
134 }
135 }
136 }
137 #endif
138 } else {
139 DCHECK_EQ(delay.InMilliseconds(), 0) << "delay should not be negative";
140 }
141
142 #if defined(OS_WIN)
143 if (!high_resolution_timer_expiration_.is_null()) {
144 if (TimeTicks::Now() > high_resolution_timer_expiration_) {
145 Time::ActivateHighResolutionTimer(false);
146 high_resolution_timer_expiration_ = TimeTicks();
59 } 147 }
60 } 148 }
61 if (!delete_later) 149 #endif
62 delete this; 150
151 return delayed_run_time;
63 } 152 }
64 153
65 MessageLoopProxyImpl::MessageLoopProxyImpl() 154 bool MessageLoop::ProxyImpl::PostPendingTask(PendingTask* pending_task) {
66 : target_message_loop_(MessageLoop::current()) { 155 // Warning: Don't try to short-circuit, and handle this thread's tasks more
67 } 156 // directly, as it could starve handling of foreign threads. Put every task
157 // into this queue.
68 158
69 bool MessageLoopProxyImpl::PostTaskHelper( 159 // This should only be called while the lock is taken.
70 const tracked_objects::Location& from_here, const base::Closure& task, 160 incoming_queue_lock_.AssertAcquired();
71 base::TimeDelta delay, bool nestable) { 161
72 AutoLock lock(message_loop_lock_); 162 if (!message_loop_) {
73 if (target_message_loop_) { 163 pending_task->task.Reset();
74 if (nestable) { 164 return false;
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 } 165 }
81 return false; 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)
181 message_loop_->WakeUpPump();
182
183 return true;
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
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698