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 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
136 | 136 |
137 MessageLoop::MessageLoop(Type type) | 137 MessageLoop::MessageLoop(Type type) |
138 : type_(type), | 138 : type_(type), |
139 nestable_tasks_allowed_(true), | 139 nestable_tasks_allowed_(true), |
140 exception_restoration_(false), | 140 exception_restoration_(false), |
141 message_histogram_(NULL), | 141 message_histogram_(NULL), |
142 run_loop_(NULL), | 142 run_loop_(NULL), |
143 #if defined(OS_WIN) | 143 #if defined(OS_WIN) |
144 os_modal_loop_(false), | 144 os_modal_loop_(false), |
145 #endif // OS_WIN | 145 #endif // OS_WIN |
146 next_sequence_num_(0) { | 146 next_sequence_num_(0), |
147 always_notify_pump_(false) { | |
147 DCHECK(!current()) << "should only have one message loop per thread"; | 148 DCHECK(!current()) << "should only have one message loop per thread"; |
148 lazy_tls_ptr.Pointer()->Set(this); | 149 lazy_tls_ptr.Pointer()->Set(this); |
149 | 150 |
150 message_loop_proxy_ = new MessageLoopProxyImpl(); | 151 message_loop_proxy_ = new MessageLoopProxyImpl(); |
151 thread_task_runner_handle_.reset( | 152 thread_task_runner_handle_.reset( |
152 new ThreadTaskRunnerHandle(message_loop_proxy_)); | 153 new ThreadTaskRunnerHandle(message_loop_proxy_)); |
153 | 154 |
154 // TODO(rvargas): Get rid of the OS guards. | 155 // TODO(rvargas): Get rid of the OS guards. |
155 #if defined(OS_WIN) | 156 #if defined(OS_WIN) |
156 #define MESSAGE_PUMP_UI new MessagePumpForUI() | 157 #define MESSAGE_PUMP_UI new MessagePumpForUI() |
(...skipping 22 matching lines...) Expand all Loading... | |
179 if (message_pump_for_ui_factory_) | 180 if (message_pump_for_ui_factory_) |
180 pump_ = message_pump_for_ui_factory_(); | 181 pump_ = message_pump_for_ui_factory_(); |
181 else | 182 else |
182 pump_ = MESSAGE_PUMP_UI; | 183 pump_ = MESSAGE_PUMP_UI; |
183 } else if (type_ == TYPE_IO) { | 184 } else if (type_ == TYPE_IO) { |
184 pump_ = MESSAGE_PUMP_IO; | 185 pump_ = MESSAGE_PUMP_IO; |
185 } else { | 186 } else { |
186 DCHECK_EQ(TYPE_DEFAULT, type_); | 187 DCHECK_EQ(TYPE_DEFAULT, type_); |
187 pump_ = new MessagePumpDefault(); | 188 pump_ = new MessagePumpDefault(); |
188 } | 189 } |
190 always_notify_pump_ = pump_->NeedsScheduleWorkPerTask(); | |
awong
2013/07/03 19:09:22
Always_notify_pump_ seems to effectively be a plat
Kristian Monsen
2013/07/03 21:20:03
I had a CL doing this locally, was not sure which
| |
189 } | 191 } |
190 | 192 |
191 MessageLoop::~MessageLoop() { | 193 MessageLoop::~MessageLoop() { |
192 DCHECK_EQ(this, current()); | 194 DCHECK_EQ(this, current()); |
193 | 195 |
194 DCHECK(!run_loop_); | 196 DCHECK(!run_loop_); |
195 | 197 |
196 // Clean up any unprocessed tasks, but take care: deleting a task could | 198 // Clean up any unprocessed tasks, but take care: deleting a task could |
197 // result in the addition of more tasks (e.g., via DeleteSoon). We set a | 199 // result in the addition of more tasks (e.g., via DeleteSoon). We set a |
198 // limit on the number of times we will allow a deleted task to generate more | 200 // limit on the number of times we will allow a deleted task to generate more |
(...skipping 418 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
617 // tasks (to faciliate FIFO sorting when two tasks have the same | 619 // tasks (to faciliate FIFO sorting when two tasks have the same |
618 // delayed_run_time value) and for identifying the task in about:tracing. | 620 // delayed_run_time value) and for identifying the task in about:tracing. |
619 pending_task->sequence_num = next_sequence_num_++; | 621 pending_task->sequence_num = next_sequence_num_++; |
620 | 622 |
621 TRACE_EVENT_FLOW_BEGIN0("task", "MessageLoop::PostTask", | 623 TRACE_EVENT_FLOW_BEGIN0("task", "MessageLoop::PostTask", |
622 TRACE_ID_MANGLE(GetTaskTraceID(*pending_task, this))); | 624 TRACE_ID_MANGLE(GetTaskTraceID(*pending_task, this))); |
623 | 625 |
624 bool was_empty = incoming_queue_.empty(); | 626 bool was_empty = incoming_queue_.empty(); |
625 incoming_queue_.push(*pending_task); | 627 incoming_queue_.push(*pending_task); |
626 pending_task->task.Reset(); | 628 pending_task->task.Reset(); |
627 if (!was_empty) | 629 // The Android UI message loop needs to get notified each time |
630 // a task is added to the incoming queue | |
awong
2013/07/03 19:09:22
nit: Please end sentences with a period.
Kristian Monsen
2013/07/03 21:20:03
Done.
| |
631 if (!was_empty && !always_notify_pump_) | |
628 return true; // Someone else should have started the sub-pump. | 632 return true; // Someone else should have started the sub-pump. |
629 | 633 |
630 pump = pump_; | 634 pump = pump_; |
631 } | 635 } |
632 // Since the incoming_queue_ may contain a task that destroys this message | 636 // Since the incoming_queue_ may contain a task that destroys this message |
633 // loop, we cannot exit incoming_queue_lock_ until we are done with |this|. | 637 // loop, we cannot exit incoming_queue_lock_ until we are done with |this|. |
634 // We use a stack-based reference to the message pump so that we can call | 638 // We use a stack-based reference to the message pump so that we can call |
635 // ScheduleWork outside of incoming_queue_lock_. | 639 // ScheduleWork outside of incoming_queue_lock_. |
636 | 640 |
637 pump->ScheduleWork(); | 641 pump->ScheduleWork(); |
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
825 fd, | 829 fd, |
826 persistent, | 830 persistent, |
827 mode, | 831 mode, |
828 controller, | 832 controller, |
829 delegate); | 833 delegate); |
830 } | 834 } |
831 | 835 |
832 #endif | 836 #endif |
833 | 837 |
834 } // namespace base | 838 } // namespace base |
OLD | NEW |