| 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 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 MessageLoop::TaskObserver::~TaskObserver() { | 110 MessageLoop::TaskObserver::~TaskObserver() { |
| 111 } | 111 } |
| 112 | 112 |
| 113 MessageLoop::DestructionObserver::~DestructionObserver() { | 113 MessageLoop::DestructionObserver::~DestructionObserver() { |
| 114 } | 114 } |
| 115 | 115 |
| 116 //------------------------------------------------------------------------------ | 116 //------------------------------------------------------------------------------ |
| 117 | 117 |
| 118 MessageLoop::MessageLoop(Type type) | 118 MessageLoop::MessageLoop(Type type) |
| 119 : type_(type), | 119 : type_(type), |
| 120 #if defined(OS_WIN) |
| 120 pending_high_res_tasks_(0), | 121 pending_high_res_tasks_(0), |
| 121 in_high_res_mode_(false), | 122 in_high_res_mode_(false), |
| 123 #endif |
| 122 nestable_tasks_allowed_(true), | 124 nestable_tasks_allowed_(true), |
| 123 #if defined(OS_WIN) | 125 #if defined(OS_WIN) |
| 124 os_modal_loop_(false), | 126 os_modal_loop_(false), |
| 125 #endif // OS_WIN | 127 #endif // OS_WIN |
| 126 message_histogram_(NULL), | 128 message_histogram_(NULL), |
| 127 run_loop_(NULL) { | 129 run_loop_(NULL) { |
| 128 Init(); | 130 Init(); |
| 129 | 131 |
| 130 pump_ = CreateMessagePumpForType(type).Pass(); | 132 pump_ = CreateMessagePumpForType(type).Pass(); |
| 131 } | 133 } |
| 132 | 134 |
| 133 MessageLoop::MessageLoop(scoped_ptr<MessagePump> pump) | 135 MessageLoop::MessageLoop(scoped_ptr<MessagePump> pump) |
| 134 : pump_(pump.Pass()), | 136 : pump_(pump.Pass()), |
| 135 type_(TYPE_CUSTOM), | 137 type_(TYPE_CUSTOM), |
| 138 #if defined(OS_WIN) |
| 136 pending_high_res_tasks_(0), | 139 pending_high_res_tasks_(0), |
| 137 in_high_res_mode_(false), | 140 in_high_res_mode_(false), |
| 141 #endif |
| 138 nestable_tasks_allowed_(true), | 142 nestable_tasks_allowed_(true), |
| 139 #if defined(OS_WIN) | 143 #if defined(OS_WIN) |
| 140 os_modal_loop_(false), | 144 os_modal_loop_(false), |
| 141 #endif // OS_WIN | 145 #endif // OS_WIN |
| 142 message_histogram_(NULL), | 146 message_histogram_(NULL), |
| 143 run_loop_(NULL) { | 147 run_loop_(NULL) { |
| 144 DCHECK(pump_.get()); | 148 DCHECK(pump_.get()); |
| 145 Init(); | 149 Init(); |
| 146 } | 150 } |
| 147 | 151 |
| (...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 415 PendingTask pending_task = deferred_non_nestable_work_queue_.front(); | 419 PendingTask pending_task = deferred_non_nestable_work_queue_.front(); |
| 416 deferred_non_nestable_work_queue_.pop(); | 420 deferred_non_nestable_work_queue_.pop(); |
| 417 | 421 |
| 418 RunTask(pending_task); | 422 RunTask(pending_task); |
| 419 return true; | 423 return true; |
| 420 } | 424 } |
| 421 | 425 |
| 422 void MessageLoop::RunTask(const PendingTask& pending_task) { | 426 void MessageLoop::RunTask(const PendingTask& pending_task) { |
| 423 DCHECK(nestable_tasks_allowed_); | 427 DCHECK(nestable_tasks_allowed_); |
| 424 | 428 |
| 429 #if defined(OS_WIN) |
| 425 if (pending_task.is_high_res) { | 430 if (pending_task.is_high_res) { |
| 426 pending_high_res_tasks_--; | 431 pending_high_res_tasks_--; |
| 427 CHECK(pending_high_res_tasks_ >= 0); | 432 CHECK_GE(pending_high_res_tasks_, 0); |
| 428 } | 433 } |
| 434 #endif |
| 435 |
| 429 // Execute the task and assume the worst: It is probably not reentrant. | 436 // Execute the task and assume the worst: It is probably not reentrant. |
| 430 nestable_tasks_allowed_ = false; | 437 nestable_tasks_allowed_ = false; |
| 431 | 438 |
| 432 HistogramEvent(kTaskRunEvent); | 439 HistogramEvent(kTaskRunEvent); |
| 433 | 440 |
| 434 FOR_EACH_OBSERVER(TaskObserver, task_observers_, | 441 FOR_EACH_OBSERVER(TaskObserver, task_observers_, |
| 435 WillProcessTask(pending_task)); | 442 WillProcessTask(pending_task)); |
| 436 task_annotator_.RunTask( | 443 task_annotator_.RunTask( |
| 437 "MessageLoop::PostTask", "MessageLoop::RunTask", pending_task); | 444 "MessageLoop::PostTask", "MessageLoop::RunTask", pending_task); |
| 438 FOR_EACH_OBSERVER(TaskObserver, task_observers_, | 445 FOR_EACH_OBSERVER(TaskObserver, task_observers_, |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 488 } | 495 } |
| 489 return did_work; | 496 return did_work; |
| 490 } | 497 } |
| 491 | 498 |
| 492 void MessageLoop::ReloadWorkQueue() { | 499 void MessageLoop::ReloadWorkQueue() { |
| 493 // We can improve performance of our loading tasks from the incoming queue to | 500 // We can improve performance of our loading tasks from the incoming queue to |
| 494 // |*work_queue| by waiting until the last minute (|*work_queue| is empty) to | 501 // |*work_queue| by waiting until the last minute (|*work_queue| is empty) to |
| 495 // load. That reduces the number of locks-per-task significantly when our | 502 // load. That reduces the number of locks-per-task significantly when our |
| 496 // queues get large. | 503 // queues get large. |
| 497 if (work_queue_.empty()) { | 504 if (work_queue_.empty()) { |
| 505 #if defined(OS_WIN) |
| 498 pending_high_res_tasks_ += | 506 pending_high_res_tasks_ += |
| 499 incoming_task_queue_->ReloadWorkQueue(&work_queue_); | 507 incoming_task_queue_->ReloadWorkQueue(&work_queue_); |
| 508 #else |
| 509 incoming_task_queue_->ReloadWorkQueue(&work_queue_); |
| 510 #endif |
| 500 } | 511 } |
| 501 } | 512 } |
| 502 | 513 |
| 503 void MessageLoop::ScheduleWork() { | 514 void MessageLoop::ScheduleWork() { |
| 504 pump_->ScheduleWork(); | 515 pump_->ScheduleWork(); |
| 505 } | 516 } |
| 506 | 517 |
| 507 //------------------------------------------------------------------------------ | 518 //------------------------------------------------------------------------------ |
| 508 // Method and data for histogramming events and actions taken by each instance | 519 // Method and data for histogramming events and actions taken by each instance |
| 509 // on each thread. | 520 // on each thread. |
| (...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 685 return ToPumpIO(pump_.get())->RegisterJobObject(job, handler); | 696 return ToPumpIO(pump_.get())->RegisterJobObject(job, handler); |
| 686 } | 697 } |
| 687 | 698 |
| 688 bool MessageLoopForIO::WaitForIOCompletion(DWORD timeout, IOHandler* filter) { | 699 bool MessageLoopForIO::WaitForIOCompletion(DWORD timeout, IOHandler* filter) { |
| 689 return ToPumpIO(pump_.get())->WaitForIOCompletion(timeout, filter); | 700 return ToPumpIO(pump_.get())->WaitForIOCompletion(timeout, filter); |
| 690 } | 701 } |
| 691 #elif defined(OS_POSIX) | 702 #elif defined(OS_POSIX) |
| 692 bool MessageLoopForIO::WatchFileDescriptor(int fd, | 703 bool MessageLoopForIO::WatchFileDescriptor(int fd, |
| 693 bool persistent, | 704 bool persistent, |
| 694 Mode mode, | 705 Mode mode, |
| 695 FileDescriptorWatcher *controller, | 706 FileDescriptorWatcher* controller, |
| 696 Watcher *delegate) { | 707 Watcher* delegate) { |
| 697 return ToPumpIO(pump_.get())->WatchFileDescriptor( | 708 return ToPumpIO(pump_.get())->WatchFileDescriptor( |
| 698 fd, | 709 fd, |
| 699 persistent, | 710 persistent, |
| 700 mode, | 711 mode, |
| 701 controller, | 712 controller, |
| 702 delegate); | 713 delegate); |
| 703 } | 714 } |
| 704 #endif | 715 #endif |
| 705 | 716 |
| 706 #endif // !defined(OS_NACL_SFI) | 717 #endif // !defined(OS_NACL_SFI) |
| 707 | 718 |
| 708 } // namespace base | 719 } // namespace base |
| OLD | NEW |