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 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
163 | 163 |
164 // Let interested parties have one last shot at accessing this. | 164 // Let interested parties have one last shot at accessing this. |
165 FOR_EACH_OBSERVER(DestructionObserver, destruction_observers_, | 165 FOR_EACH_OBSERVER(DestructionObserver, destruction_observers_, |
166 WillDestroyCurrentMessageLoop()); | 166 WillDestroyCurrentMessageLoop()); |
167 | 167 |
168 thread_task_runner_handle_.reset(); | 168 thread_task_runner_handle_.reset(); |
169 | 169 |
170 // Tell the incoming queue that we are dying. | 170 // Tell the incoming queue that we are dying. |
171 incoming_task_queue_->WillDestroyCurrentMessageLoop(); | 171 incoming_task_queue_->WillDestroyCurrentMessageLoop(); |
172 incoming_task_queue_ = NULL; | 172 incoming_task_queue_ = NULL; |
173 unbound_task_runner_ = NULL; | |
174 task_runner_ = NULL; | 173 task_runner_ = NULL; |
175 | 174 |
176 // OK, now make it so that no one can find us. | 175 // OK, now make it so that no one can find us. |
177 lazy_tls_ptr.Pointer()->Set(NULL); | 176 lazy_tls_ptr.Pointer()->Set(NULL); |
178 } | 177 } |
179 | 178 |
180 // static | 179 // static |
181 MessageLoop* MessageLoop::current() { | 180 MessageLoop* MessageLoop::current() { |
182 // TODO(darin): sadly, we cannot enable this yet since people call us even | 181 // TODO(darin): sadly, we cannot enable this yet since people call us even |
183 // when they have no intention of using us. | 182 // when they have no intention of using us. |
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
361 } | 360 } |
362 | 361 |
363 bool MessageLoop::IsIdleForTesting() { | 362 bool MessageLoop::IsIdleForTesting() { |
364 // 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 |
365 // queue. | 364 // queue. |
366 return incoming_task_queue_->IsIdleForTesting(); | 365 return incoming_task_queue_->IsIdleForTesting(); |
367 } | 366 } |
368 | 367 |
369 //------------------------------------------------------------------------------ | 368 //------------------------------------------------------------------------------ |
370 | 369 |
371 // static | |
372 scoped_ptr<MessageLoop> MessageLoop::CreateUnbound( | 370 scoped_ptr<MessageLoop> MessageLoop::CreateUnbound( |
373 Type type, MessagePumpFactoryCallback pump_factory) { | 371 Type type, MessagePumpFactoryCallback pump_factory) { |
374 return make_scoped_ptr(new MessageLoop(type, pump_factory)); | 372 return make_scoped_ptr(new MessageLoop(type, pump_factory)); |
375 } | 373 } |
376 | 374 |
377 MessageLoop::MessageLoop(Type type, MessagePumpFactoryCallback pump_factory) | 375 MessageLoop::MessageLoop(Type type, MessagePumpFactoryCallback pump_factory) |
378 : type_(type), | 376 : type_(type), |
379 #if defined(OS_WIN) | 377 #if defined(OS_WIN) |
380 pending_high_res_tasks_(0), | 378 pending_high_res_tasks_(0), |
381 in_high_res_mode_(false), | 379 in_high_res_mode_(false), |
382 #endif | 380 #endif |
383 nestable_tasks_allowed_(true), | 381 nestable_tasks_allowed_(true), |
384 #if defined(OS_WIN) | 382 #if defined(OS_WIN) |
385 os_modal_loop_(false), | 383 os_modal_loop_(false), |
386 #endif // OS_WIN | 384 #endif // OS_WIN |
387 pump_factory_(pump_factory), | 385 pump_factory_(pump_factory), |
388 message_histogram_(NULL), | 386 message_histogram_(NULL), |
389 run_loop_(NULL), | 387 run_loop_(NULL), |
390 incoming_task_queue_(new internal::IncomingTaskQueue(this)), | 388 incoming_task_queue_(new internal::IncomingTaskQueue(this)), |
391 unbound_task_runner_( | 389 task_runner_(new internal::MessageLoopTaskRunner(incoming_task_queue_)) { |
392 new internal::MessageLoopTaskRunner(incoming_task_queue_)), | |
393 task_runner_(unbound_task_runner_) { | |
394 // If type is TYPE_CUSTOM non-null pump_factory must be given. | 390 // If type is TYPE_CUSTOM non-null pump_factory must be given. |
395 DCHECK_EQ(type_ == TYPE_CUSTOM, !pump_factory_.is_null()); | 391 DCHECK_EQ(type_ == TYPE_CUSTOM, !pump_factory_.is_null()); |
396 } | 392 } |
397 | 393 |
398 void MessageLoop::BindToCurrentThread() { | 394 void MessageLoop::BindToCurrentThread() { |
399 DCHECK(!pump_); | 395 DCHECK(!pump_); |
400 if (!pump_factory_.is_null()) | 396 if (!pump_factory_.is_null()) |
401 pump_ = pump_factory_.Run(); | 397 pump_ = pump_factory_.Run(); |
402 else | 398 else |
403 pump_ = CreateMessagePumpForType(type_); | 399 pump_ = CreateMessagePumpForType(type_); |
404 | 400 |
405 DCHECK(!current()) << "should only have one message loop per thread"; | 401 DCHECK(!current()) << "should only have one message loop per thread"; |
406 lazy_tls_ptr.Pointer()->Set(this); | 402 lazy_tls_ptr.Pointer()->Set(this); |
407 | 403 |
408 incoming_task_queue_->StartScheduling(); | 404 incoming_task_queue_->StartScheduling(); |
409 unbound_task_runner_->BindToCurrentThread(); | 405 task_runner_->BindToCurrentThread(); |
410 SetTaskRunner(unbound_task_runner_.Pass()); | |
411 } | |
412 | |
413 void MessageLoop::SetTaskRunner( | |
414 scoped_refptr<SingleThreadTaskRunner> task_runner) { | |
415 DCHECK_EQ(this, current()); | |
416 DCHECK(task_runner->BelongsToCurrentThread()); | |
417 DCHECK(!unbound_task_runner_); | |
418 task_runner_ = task_runner.Pass(); | |
419 // Clear the previous thread task runner first because only one can exist at | |
420 // a time. | |
421 thread_task_runner_handle_.reset(); | |
422 thread_task_runner_handle_.reset(new ThreadTaskRunnerHandle(task_runner_)); | 406 thread_task_runner_handle_.reset(new ThreadTaskRunnerHandle(task_runner_)); |
423 } | 407 } |
424 | 408 |
425 void MessageLoop::RunHandler() { | 409 void MessageLoop::RunHandler() { |
426 DCHECK_EQ(this, current()); | 410 DCHECK_EQ(this, current()); |
427 | 411 |
428 StartHistogrammer(); | 412 StartHistogrammer(); |
429 | 413 |
430 #if defined(OS_WIN) | 414 #if defined(OS_WIN) |
431 if (run_loop_->dispatcher_ && type() == TYPE_UI) { | 415 if (run_loop_->dispatcher_ && type() == TYPE_UI) { |
(...skipping 307 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
739 persistent, | 723 persistent, |
740 mode, | 724 mode, |
741 controller, | 725 controller, |
742 delegate); | 726 delegate); |
743 } | 727 } |
744 #endif | 728 #endif |
745 | 729 |
746 #endif // !defined(OS_NACL_SFI) | 730 #endif // !defined(OS_NACL_SFI) |
747 | 731 |
748 } // namespace base | 732 } // namespace base |
OLD | NEW |