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