OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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.h" | 5 #include "base/message_loop/message_loop.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 #elif defined(OS_POSIX) | 93 #elif defined(OS_POSIX) |
94 typedef MessagePumpLibevent MessagePumpForIO; | 94 typedef MessagePumpLibevent MessagePumpForIO; |
95 #endif | 95 #endif |
96 | 96 |
97 #if !defined(OS_NACL_SFI) | 97 #if !defined(OS_NACL_SFI) |
98 MessagePumpForIO* ToPumpIO(MessagePump* pump) { | 98 MessagePumpForIO* ToPumpIO(MessagePump* pump) { |
99 return static_cast<MessagePumpForIO*>(pump); | 99 return static_cast<MessagePumpForIO*>(pump); |
100 } | 100 } |
101 #endif // !defined(OS_NACL_SFI) | 101 #endif // !defined(OS_NACL_SFI) |
102 | 102 |
103 scoped_ptr<MessagePump> ReturnPump(scoped_ptr<MessagePump> pump) { | |
104 return pump; | |
105 } | |
106 | |
107 } // namespace | 103 } // namespace |
108 | 104 |
109 //------------------------------------------------------------------------------ | 105 //------------------------------------------------------------------------------ |
110 | 106 |
111 MessageLoop::TaskObserver::TaskObserver() { | 107 MessageLoop::TaskObserver::TaskObserver() { |
112 } | 108 } |
113 | 109 |
114 MessageLoop::TaskObserver::~TaskObserver() { | 110 MessageLoop::TaskObserver::~TaskObserver() { |
115 } | 111 } |
116 | 112 |
117 MessageLoop::DestructionObserver::~DestructionObserver() { | 113 MessageLoop::DestructionObserver::~DestructionObserver() { |
118 } | 114 } |
119 | 115 |
120 //------------------------------------------------------------------------------ | 116 //------------------------------------------------------------------------------ |
121 | 117 |
122 MessageLoop::MessageLoop(Type type) | 118 MessageLoop::MessageLoop(Type type) |
123 : MessageLoop(type, MessagePumpFactoryCallback()) { | 119 : type_(type), |
124 BindToCurrentThread(); | 120 #if defined(OS_WIN) |
| 121 pending_high_res_tasks_(0), |
| 122 in_high_res_mode_(false), |
| 123 #endif |
| 124 nestable_tasks_allowed_(true), |
| 125 #if defined(OS_WIN) |
| 126 os_modal_loop_(false), |
| 127 #endif // OS_WIN |
| 128 message_histogram_(NULL), |
| 129 run_loop_(NULL) { |
| 130 Init(); |
| 131 |
| 132 pump_ = CreateMessagePumpForType(type).Pass(); |
125 } | 133 } |
126 | 134 |
127 MessageLoop::MessageLoop(scoped_ptr<MessagePump> pump) | 135 MessageLoop::MessageLoop(scoped_ptr<MessagePump> pump) |
128 : MessageLoop(TYPE_CUSTOM, Bind(&ReturnPump, Passed(&pump))) { | 136 : pump_(pump.Pass()), |
129 BindToCurrentThread(); | 137 type_(TYPE_CUSTOM), |
| 138 #if defined(OS_WIN) |
| 139 pending_high_res_tasks_(0), |
| 140 in_high_res_mode_(false), |
| 141 #endif |
| 142 nestable_tasks_allowed_(true), |
| 143 #if defined(OS_WIN) |
| 144 os_modal_loop_(false), |
| 145 #endif // OS_WIN |
| 146 message_histogram_(NULL), |
| 147 run_loop_(NULL) { |
| 148 DCHECK(pump_.get()); |
| 149 Init(); |
130 } | 150 } |
131 | 151 |
132 MessageLoop::~MessageLoop() { | 152 MessageLoop::~MessageLoop() { |
133 // current() could be NULL if this message loop is destructed before it is | 153 DCHECK_EQ(this, current()); |
134 // bound to a thread. | |
135 DCHECK(current() == this || !current()); | |
136 | 154 |
137 // iOS just attaches to the loop, it doesn't Run it. | 155 // iOS just attaches to the loop, it doesn't Run it. |
138 // TODO(stuartmorgan): Consider wiring up a Detach(). | 156 // TODO(stuartmorgan): Consider wiring up a Detach(). |
139 #if !defined(OS_IOS) | 157 #if !defined(OS_IOS) |
140 DCHECK(!run_loop_); | 158 DCHECK(!run_loop_); |
141 #endif | 159 #endif |
142 | 160 |
143 #if defined(OS_WIN) | 161 #if defined(OS_WIN) |
144 if (in_high_res_mode_) | 162 if (in_high_res_mode_) |
145 Time::ActivateHighResolutionTimer(false); | 163 Time::ActivateHighResolutionTimer(false); |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
274 } | 292 } |
275 | 293 |
276 void MessageLoop::PostNonNestableDelayedTask( | 294 void MessageLoop::PostNonNestableDelayedTask( |
277 const tracked_objects::Location& from_here, | 295 const tracked_objects::Location& from_here, |
278 const Closure& task, | 296 const Closure& task, |
279 TimeDelta delay) { | 297 TimeDelta delay) { |
280 message_loop_proxy_->PostNonNestableDelayedTask(from_here, task, delay); | 298 message_loop_proxy_->PostNonNestableDelayedTask(from_here, task, delay); |
281 } | 299 } |
282 | 300 |
283 void MessageLoop::Run() { | 301 void MessageLoop::Run() { |
284 DCHECK(pump_); | |
285 RunLoop run_loop; | 302 RunLoop run_loop; |
286 run_loop.Run(); | 303 run_loop.Run(); |
287 } | 304 } |
288 | 305 |
289 void MessageLoop::RunUntilIdle() { | 306 void MessageLoop::RunUntilIdle() { |
290 DCHECK(pump_); | |
291 RunLoop run_loop; | 307 RunLoop run_loop; |
292 run_loop.RunUntilIdle(); | 308 run_loop.RunUntilIdle(); |
293 } | 309 } |
294 | 310 |
295 void MessageLoop::QuitWhenIdle() { | 311 void MessageLoop::QuitWhenIdle() { |
296 DCHECK_EQ(this, current()); | 312 DCHECK_EQ(this, current()); |
297 if (run_loop_) { | 313 if (run_loop_) { |
298 run_loop_->quit_when_idle_received_ = true; | 314 run_loop_->quit_when_idle_received_ = true; |
299 } else { | 315 } else { |
300 NOTREACHED() << "Must be inside Run to call Quit"; | 316 NOTREACHED() << "Must be inside Run to call Quit"; |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
360 } | 376 } |
361 | 377 |
362 bool MessageLoop::IsIdleForTesting() { | 378 bool MessageLoop::IsIdleForTesting() { |
363 // We only check the imcoming queue|, since we don't want to lock the work | 379 // We only check the imcoming queue|, since we don't want to lock the work |
364 // queue. | 380 // queue. |
365 return incoming_task_queue_->IsIdleForTesting(); | 381 return incoming_task_queue_->IsIdleForTesting(); |
366 } | 382 } |
367 | 383 |
368 //------------------------------------------------------------------------------ | 384 //------------------------------------------------------------------------------ |
369 | 385 |
370 scoped_ptr<MessageLoop> MessageLoop::CreateUnbound( | 386 void MessageLoop::Init() { |
371 Type type, MessagePumpFactoryCallback pump_factory) { | |
372 return make_scoped_ptr(new MessageLoop(type, pump_factory)); | |
373 } | |
374 | |
375 MessageLoop::MessageLoop(Type type, MessagePumpFactoryCallback pump_factory) | |
376 : type_(type), | |
377 #if defined(OS_WIN) | |
378 pending_high_res_tasks_(0), | |
379 in_high_res_mode_(false), | |
380 #endif | |
381 nestable_tasks_allowed_(true), | |
382 #if defined(OS_WIN) | |
383 os_modal_loop_(false), | |
384 #endif // OS_WIN | |
385 pump_factory_(pump_factory), | |
386 message_histogram_(NULL), | |
387 run_loop_(NULL), | |
388 incoming_task_queue_(new internal::IncomingTaskQueue(this)), | |
389 message_loop_proxy_( | |
390 new internal::MessageLoopProxyImpl(incoming_task_queue_)) { | |
391 // If type is TYPE_CUSTOM non-null pump_factory must be given. | |
392 DCHECK_EQ(type_ == TYPE_CUSTOM, !pump_factory_.is_null()); | |
393 } | |
394 | |
395 void MessageLoop::BindToCurrentThread() { | |
396 DCHECK(!pump_); | |
397 if (!pump_factory_.is_null()) | |
398 pump_ = pump_factory_.Run(); | |
399 else | |
400 pump_ = CreateMessagePumpForType(type_); | |
401 | |
402 DCHECK(!current()) << "should only have one message loop per thread"; | 387 DCHECK(!current()) << "should only have one message loop per thread"; |
403 lazy_tls_ptr.Pointer()->Set(this); | 388 lazy_tls_ptr.Pointer()->Set(this); |
404 | 389 |
405 incoming_task_queue_->StartScheduling(); | 390 incoming_task_queue_ = new internal::IncomingTaskQueue(this); |
406 message_loop_proxy_->BindToCurrentThread(); | 391 message_loop_proxy_ = |
| 392 new internal::MessageLoopProxyImpl(incoming_task_queue_); |
407 thread_task_runner_handle_.reset( | 393 thread_task_runner_handle_.reset( |
408 new ThreadTaskRunnerHandle(message_loop_proxy_)); | 394 new ThreadTaskRunnerHandle(message_loop_proxy_)); |
409 } | 395 } |
410 | 396 |
411 void MessageLoop::RunHandler() { | 397 void MessageLoop::RunHandler() { |
412 DCHECK_EQ(this, current()); | 398 DCHECK_EQ(this, current()); |
413 | 399 |
414 StartHistogrammer(); | 400 StartHistogrammer(); |
415 | 401 |
416 #if defined(OS_WIN) | 402 #if defined(OS_WIN) |
(...skipping 308 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
725 persistent, | 711 persistent, |
726 mode, | 712 mode, |
727 controller, | 713 controller, |
728 delegate); | 714 delegate); |
729 } | 715 } |
730 #endif | 716 #endif |
731 | 717 |
732 #endif // !defined(OS_NACL_SFI) | 718 #endif // !defined(OS_NACL_SFI) |
733 | 719 |
734 } // namespace base | 720 } // namespace base |
OLD | NEW |