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 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
483 // code is replicating legacy behavior, and should not be considered | 490 // code is replicating legacy behavior, and should not be considered |
484 // absolutely "correct" behavior. See TODO above about deleting all tasks | 491 // absolutely "correct" behavior. See TODO above about deleting all tasks |
485 // when it's safe. | 492 // when it's safe. |
486 while (!delayed_work_queue_.empty()) { | 493 while (!delayed_work_queue_.empty()) { |
487 delayed_work_queue_.pop(); | 494 delayed_work_queue_.pop(); |
488 } | 495 } |
489 return did_work; | 496 return did_work; |
490 } | 497 } |
491 | 498 |
492 void MessageLoop::ReloadWorkQueue() { | 499 void MessageLoop::ReloadWorkQueue() { |
| 500 #if defined(OS_WIN) |
493 // We can improve performance of our loading tasks from the incoming queue to | 501 // 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 | 502 // |*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 | 503 // load. That reduces the number of locks-per-task significantly when our |
496 // queues get large. | 504 // queues get large. |
497 if (work_queue_.empty()) { | 505 if (work_queue_.empty()) { |
498 pending_high_res_tasks_ += | 506 pending_high_res_tasks_ += |
499 incoming_task_queue_->ReloadWorkQueue(&work_queue_); | 507 incoming_task_queue_->ReloadWorkQueue(&work_queue_); |
500 } | 508 } |
| 509 #endif |
501 } | 510 } |
502 | 511 |
503 void MessageLoop::ScheduleWork() { | 512 void MessageLoop::ScheduleWork() { |
504 pump_->ScheduleWork(); | 513 pump_->ScheduleWork(); |
505 } | 514 } |
506 | 515 |
507 //------------------------------------------------------------------------------ | 516 //------------------------------------------------------------------------------ |
508 // Method and data for histogramming events and actions taken by each instance | 517 // Method and data for histogramming events and actions taken by each instance |
509 // on each thread. | 518 // on each thread. |
510 | 519 |
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
685 return ToPumpIO(pump_.get())->RegisterJobObject(job, handler); | 694 return ToPumpIO(pump_.get())->RegisterJobObject(job, handler); |
686 } | 695 } |
687 | 696 |
688 bool MessageLoopForIO::WaitForIOCompletion(DWORD timeout, IOHandler* filter) { | 697 bool MessageLoopForIO::WaitForIOCompletion(DWORD timeout, IOHandler* filter) { |
689 return ToPumpIO(pump_.get())->WaitForIOCompletion(timeout, filter); | 698 return ToPumpIO(pump_.get())->WaitForIOCompletion(timeout, filter); |
690 } | 699 } |
691 #elif defined(OS_POSIX) | 700 #elif defined(OS_POSIX) |
692 bool MessageLoopForIO::WatchFileDescriptor(int fd, | 701 bool MessageLoopForIO::WatchFileDescriptor(int fd, |
693 bool persistent, | 702 bool persistent, |
694 Mode mode, | 703 Mode mode, |
695 FileDescriptorWatcher *controller, | 704 FileDescriptorWatcher* controller, |
696 Watcher *delegate) { | 705 Watcher* delegate) { |
697 return ToPumpIO(pump_.get())->WatchFileDescriptor( | 706 return ToPumpIO(pump_.get())->WatchFileDescriptor( |
698 fd, | 707 fd, |
699 persistent, | 708 persistent, |
700 mode, | 709 mode, |
701 controller, | 710 controller, |
702 delegate); | 711 delegate); |
703 } | 712 } |
704 #endif | 713 #endif |
705 | 714 |
706 #endif // !defined(OS_NACL_SFI) | 715 #endif // !defined(OS_NACL_SFI) |
707 | 716 |
708 } // namespace base | 717 } // namespace base |
OLD | NEW |