blob: 314a7a0d768b141caf8213dd98833291a7267937 [file] [log] [blame]
[email protected]91462212009-04-10 15:09:391// Copyright (c) 2009 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commitd7cae122008-07-26 21:49:384
[email protected]ea15e982008-08-15 07:31:205#include "base/message_loop.h"
6
[email protected]fc7fb6e2008-08-16 03:09:057#include <algorithm>
8
[email protected]f1ea2fa2008-08-21 22:26:069#include "base/compiler_specific.h"
[email protected]f886b7bf2008-09-10 10:54:0610#include "base/lazy_instance.h"
initial.commitd7cae122008-07-26 21:49:3811#include "base/logging.h"
[email protected]b16ef312008-08-19 18:36:2312#include "base/message_pump_default.h"
initial.commitd7cae122008-07-26 21:49:3813#include "base/string_util.h"
[email protected]f886b7bf2008-09-10 10:54:0614#include "base/thread_local.h"
initial.commitd7cae122008-07-26 21:49:3815
[email protected]96c9ea12008-09-23 21:08:2816#if defined(OS_MACOSX)
17#include "base/message_pump_mac.h"
18#endif
[email protected]36987e92008-09-18 18:46:2619#if defined(OS_POSIX)
20#include "base/message_pump_libevent.h"
[email protected]d680c5c2009-05-14 16:44:2621#include "base/third_party/valgrind/valgrind.h"
[email protected]36987e92008-09-18 18:46:2622#endif
[email protected]e43eddf12009-12-29 00:32:5223#if defined(OS_POSIX) && !defined(OS_MACOSX)
[email protected]8fc3a482008-10-03 16:52:5924#include "base/message_pump_glib.h"
25#endif
[email protected]36987e92008-09-18 18:46:2626
[email protected]e1acf6f2008-10-27 20:43:3327using base::Time;
28using base::TimeDelta;
29
[email protected]f886b7bf2008-09-10 10:54:0630// A lazily created thread local storage for quick access to a thread's message
31// loop, if one exists. This should be safe and free of static constructors.
32static base::LazyInstance<base::ThreadLocalPointer<MessageLoop> > lazy_tls_ptr(
33 base::LINKER_INITIALIZED);
initial.commitd7cae122008-07-26 21:49:3834
35//------------------------------------------------------------------------------
36
initial.commitd7cae122008-07-26 21:49:3837// Logical events for Histogram profiling. Run with -message-loop-histogrammer
38// to get an accounting of messages and actions taken on each thread.
[email protected]fc7fb6e2008-08-16 03:09:0539static const int kTaskRunEvent = 0x1;
40static const int kTimerEvent = 0x2;
initial.commitd7cae122008-07-26 21:49:3841
42// Provide range of message IDs for use in histogramming and debug display.
43static const int kLeastNonZeroMessageId = 1;
44static const int kMaxMessageId = 1099;
45static const int kNumberOfDistinctMessagesDisplayed = 1100;
46
47//------------------------------------------------------------------------------
48
[email protected]fc7fb6e2008-08-16 03:09:0549#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:3850
initial.commitd7cae122008-07-26 21:49:3851// Upon a SEH exception in this thread, it restores the original unhandled
52// exception filter.
53static int SEHFilter(LPTOP_LEVEL_EXCEPTION_FILTER old_filter) {
54 ::SetUnhandledExceptionFilter(old_filter);
55 return EXCEPTION_CONTINUE_SEARCH;
56}
57
58// Retrieves a pointer to the current unhandled exception filter. There
59// is no standalone getter method.
60static LPTOP_LEVEL_EXCEPTION_FILTER GetTopSEHFilter() {
61 LPTOP_LEVEL_EXCEPTION_FILTER top_filter = NULL;
62 top_filter = ::SetUnhandledExceptionFilter(0);
63 ::SetUnhandledExceptionFilter(top_filter);
64 return top_filter;
65}
66
[email protected]fc7fb6e2008-08-16 03:09:0567#endif // defined(OS_WIN)
68
initial.commitd7cae122008-07-26 21:49:3869//------------------------------------------------------------------------------
70
[email protected]f886b7bf2008-09-10 10:54:0671// static
72MessageLoop* MessageLoop::current() {
73 // TODO(darin): sadly, we cannot enable this yet since people call us even
74 // when they have no intention of using us.
75 //DCHECK(loop) << "Ouch, did you forget to initialize me?";
76 return lazy_tls_ptr.Pointer()->Get();
77}
78
[email protected]4d9bdfaf2008-08-26 05:53:5779MessageLoop::MessageLoop(Type type)
80 : type_(type),
[email protected]a5b94a92008-08-12 23:25:4381 nestable_tasks_allowed_(true),
[email protected]b16ef312008-08-19 18:36:2382 exception_restoration_(false),
[email protected]752578562008-09-07 08:08:2983 state_(NULL),
84 next_sequence_num_(0) {
[email protected]f886b7bf2008-09-10 10:54:0685 DCHECK(!current()) << "should only have one message loop per thread";
86 lazy_tls_ptr.Pointer()->Set(this);
[email protected]4d9bdfaf2008-08-26 05:53:5787
[email protected]fc7fb6e2008-08-16 03:09:0588#if defined(OS_WIN)
[email protected]1a8f5d1d2008-09-25 20:33:0489 // TODO(rvargas): Get rid of the OS guards.
[email protected]9bcbf472008-08-30 00:22:4890 if (type_ == TYPE_DEFAULT) {
91 pump_ = new base::MessagePumpDefault();
[email protected]1a8f5d1d2008-09-25 20:33:0492 } else if (type_ == TYPE_IO) {
93 pump_ = new base::MessagePumpForIO();
[email protected]9bcbf472008-08-30 00:22:4894 } else {
[email protected]1a8f5d1d2008-09-25 20:33:0495 DCHECK(type_ == TYPE_UI);
96 pump_ = new base::MessagePumpForUI();
[email protected]9bcbf472008-08-30 00:22:4897 }
[email protected]36987e92008-09-18 18:46:2698#elif defined(OS_POSIX)
[email protected]96c9ea12008-09-23 21:08:2899 if (type_ == TYPE_UI) {
[email protected]8fc3a482008-10-03 16:52:59100#if defined(OS_MACOSX)
[email protected]96c9ea12008-09-23 21:08:28101 pump_ = base::MessagePumpMac::Create();
[email protected]e43eddf12009-12-29 00:32:52102#else
[email protected]8fc3a482008-10-03 16:52:59103 pump_ = new base::MessagePumpForUI();
[email protected]e43eddf12009-12-29 00:32:52104#endif
[email protected]8fc3a482008-10-03 16:52:59105 } else if (type_ == TYPE_IO) {
[email protected]36987e92008-09-18 18:46:26106 pump_ = new base::MessagePumpLibevent();
107 } else {
108 pump_ = new base::MessagePumpDefault();
109 }
[email protected]96c9ea12008-09-23 21:08:28110#endif // OS_POSIX
initial.commitd7cae122008-07-26 21:49:38111}
112
113MessageLoop::~MessageLoop() {
114 DCHECK(this == current());
[email protected]2a127252008-08-05 23:16:41115
116 // Let interested parties have one last shot at accessing this.
117 FOR_EACH_OBSERVER(DestructionObserver, destruction_observers_,
118 WillDestroyCurrentMessageLoop());
119
[email protected]08de3cde2008-09-09 05:55:35120 DCHECK(!state_);
121
[email protected]001747c2008-09-10 00:37:07122 // Clean up any unprocessed tasks, but take care: deleting a task could
123 // result in the addition of more tasks (e.g., via DeleteSoon). We set a
124 // limit on the number of times we will allow a deleted task to generate more
125 // tasks. Normally, we should only pass through this loop once or twice. If
126 // we end up hitting the loop limit, then it is probably due to one task that
127 // is being stubborn. Inspect the queues to see who is left.
128 bool did_work;
129 for (int i = 0; i < 100; ++i) {
130 DeletePendingTasks();
131 ReloadWorkQueue();
132 // If we end up with empty queues, then break out of the loop.
133 did_work = DeletePendingTasks();
134 if (!did_work)
135 break;
[email protected]08de3cde2008-09-09 05:55:35136 }
[email protected]001747c2008-09-10 00:37:07137 DCHECK(!did_work);
138
139 // OK, now make it so that no one can find us.
[email protected]2b89d22b22008-09-10 11:14:56140 lazy_tls_ptr.Pointer()->Set(NULL);
initial.commitd7cae122008-07-26 21:49:38141}
142
[email protected]2a127252008-08-05 23:16:41143void MessageLoop::AddDestructionObserver(DestructionObserver *obs) {
144 DCHECK(this == current());
145 destruction_observers_.AddObserver(obs);
146}
147
148void MessageLoop::RemoveDestructionObserver(DestructionObserver *obs) {
149 DCHECK(this == current());
150 destruction_observers_.RemoveObserver(obs);
151}
152
[email protected]ea15e982008-08-15 07:31:20153void MessageLoop::Run() {
[email protected]fc7fb6e2008-08-16 03:09:05154 AutoRunState save_state(this);
155 RunHandler();
[email protected]ea15e982008-08-15 07:31:20156}
157
[email protected]7e0e8762008-07-31 13:10:20158void MessageLoop::RunAllPending() {
[email protected]fc7fb6e2008-08-16 03:09:05159 AutoRunState save_state(this);
160 state_->quit_received = true; // Means run until we would otherwise block.
161 RunHandler();
initial.commitd7cae122008-07-26 21:49:38162}
163
164// Runs the loop in two different SEH modes:
165// enable_SEH_restoration_ = false : any unhandled exception goes to the last
166// one that calls SetUnhandledExceptionFilter().
167// enable_SEH_restoration_ = true : any unhandled exception goes to the filter
168// that was existed before the loop was run.
[email protected]fc7fb6e2008-08-16 03:09:05169void MessageLoop::RunHandler() {
170#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:38171 if (exception_restoration_) {
[email protected]aff8a132009-10-26 18:15:59172 RunInternalInSEHFrame();
[email protected]fc7fb6e2008-08-16 03:09:05173 return;
initial.commitd7cae122008-07-26 21:49:38174 }
[email protected]fc7fb6e2008-08-16 03:09:05175#endif
176
177 RunInternal();
initial.commitd7cae122008-07-26 21:49:38178}
[email protected]aff8a132009-10-26 18:15:59179//------------------------------------------------------------------------------
180#if defined(OS_WIN)
181__declspec(noinline) void MessageLoop::RunInternalInSEHFrame() {
182 LPTOP_LEVEL_EXCEPTION_FILTER current_filter = GetTopSEHFilter();
183 __try {
184 RunInternal();
185 } __except(SEHFilter(current_filter)) {
186 }
187 return;
188}
189#endif
initial.commitd7cae122008-07-26 21:49:38190//------------------------------------------------------------------------------
initial.commitd7cae122008-07-26 21:49:38191
[email protected]fc7fb6e2008-08-16 03:09:05192void MessageLoop::RunInternal() {
193 DCHECK(this == current());
194
initial.commitd7cae122008-07-26 21:49:38195 StartHistogrammer();
196
[email protected]e43eddf12009-12-29 00:32:52197#if !defined(OS_MACOSX)
[email protected]148d1052009-07-31 22:53:37198 if (state_->dispatcher && type() == TYPE_UI) {
199 static_cast<base::MessagePumpForUI*>(pump_.get())->
200 RunWithDispatcher(this, state_->dispatcher);
[email protected]fc7fb6e2008-08-16 03:09:05201 return;
[email protected]7e0e8762008-07-31 13:10:20202 }
[email protected]fc7fb6e2008-08-16 03:09:05203#endif
[email protected]1d2eb132008-12-08 17:36:06204
[email protected]fc7fb6e2008-08-16 03:09:05205 pump_->Run(this);
[email protected]b8f2fe5d2008-07-30 07:50:53206}
[email protected]7622bd02008-07-30 06:58:56207
[email protected]3882c4332008-07-30 19:03:59208//------------------------------------------------------------------------------
209// Wrapper functions for use in above message loop framework.
210
initial.commitd7cae122008-07-26 21:49:38211bool MessageLoop::ProcessNextDelayedNonNestableTask() {
[email protected]fc7fb6e2008-08-16 03:09:05212 if (state_->run_depth != 1)
initial.commitd7cae122008-07-26 21:49:38213 return false;
214
[email protected]752578562008-09-07 08:08:29215 if (deferred_non_nestable_work_queue_.empty())
initial.commitd7cae122008-07-26 21:49:38216 return false;
[email protected]1d2eb132008-12-08 17:36:06217
[email protected]752578562008-09-07 08:08:29218 Task* task = deferred_non_nestable_work_queue_.front().task;
219 deferred_non_nestable_work_queue_.pop();
[email protected]1d2eb132008-12-08 17:36:06220
[email protected]752578562008-09-07 08:08:29221 RunTask(task);
initial.commitd7cae122008-07-26 21:49:38222 return true;
223}
224
initial.commitd7cae122008-07-26 21:49:38225//------------------------------------------------------------------------------
226
227void MessageLoop::Quit() {
[email protected]fc7fb6e2008-08-16 03:09:05228 DCHECK(current() == this);
229 if (state_) {
230 state_->quit_received = true;
231 } else {
232 NOTREACHED() << "Must be inside Run to call Quit";
initial.commitd7cae122008-07-26 21:49:38233 }
initial.commitd7cae122008-07-26 21:49:38234}
235
[email protected]752578562008-09-07 08:08:29236void MessageLoop::PostTask(
237 const tracked_objects::Location& from_here, Task* task) {
238 PostTask_Helper(from_here, task, 0, true);
239}
240
241void MessageLoop::PostDelayedTask(
[email protected]743ace42009-06-17 17:23:51242 const tracked_objects::Location& from_here, Task* task, int64 delay_ms) {
[email protected]752578562008-09-07 08:08:29243 PostTask_Helper(from_here, task, delay_ms, true);
244}
245
246void MessageLoop::PostNonNestableTask(
247 const tracked_objects::Location& from_here, Task* task) {
248 PostTask_Helper(from_here, task, 0, false);
249}
250
251void MessageLoop::PostNonNestableDelayedTask(
[email protected]743ace42009-06-17 17:23:51252 const tracked_objects::Location& from_here, Task* task, int64 delay_ms) {
[email protected]752578562008-09-07 08:08:29253 PostTask_Helper(from_here, task, delay_ms, false);
254}
255
initial.commitd7cae122008-07-26 21:49:38256// Possibly called on a background thread!
[email protected]752578562008-09-07 08:08:29257void MessageLoop::PostTask_Helper(
[email protected]743ace42009-06-17 17:23:51258 const tracked_objects::Location& from_here, Task* task, int64 delay_ms,
[email protected]752578562008-09-07 08:08:29259 bool nestable) {
initial.commitd7cae122008-07-26 21:49:38260 task->SetBirthPlace(from_here);
[email protected]9bcbf472008-08-30 00:22:48261
[email protected]752578562008-09-07 08:08:29262 PendingTask pending_task(task, nestable);
[email protected]9bcbf472008-08-30 00:22:48263
264 if (delay_ms > 0) {
[email protected]752578562008-09-07 08:08:29265 pending_task.delayed_run_time =
[email protected]9bcbf472008-08-30 00:22:48266 Time::Now() + TimeDelta::FromMilliseconds(delay_ms);
267 } else {
[email protected]2753b392009-12-28 06:59:52268 DCHECK_EQ(delay_ms, 0) << "delay should not be negative";
[email protected]9bcbf472008-08-30 00:22:48269 }
270
initial.commitd7cae122008-07-26 21:49:38271 // Warning: Don't try to short-circuit, and handle this thread's tasks more
272 // directly, as it could starve handling of foreign threads. Put every task
273 // into this queue.
274
[email protected]fc7fb6e2008-08-16 03:09:05275 scoped_refptr<base::MessagePump> pump;
initial.commitd7cae122008-07-26 21:49:38276 {
[email protected]fc7fb6e2008-08-16 03:09:05277 AutoLock locked(incoming_queue_lock_);
278
[email protected]752578562008-09-07 08:08:29279 bool was_empty = incoming_queue_.empty();
280 incoming_queue_.push(pending_task);
initial.commitd7cae122008-07-26 21:49:38281 if (!was_empty)
282 return; // Someone else should have started the sub-pump.
283
[email protected]fc7fb6e2008-08-16 03:09:05284 pump = pump_;
[email protected]ea15e982008-08-15 07:31:20285 }
[email protected]fc7fb6e2008-08-16 03:09:05286 // Since the incoming_queue_ may contain a task that destroys this message
287 // loop, we cannot exit incoming_queue_lock_ until we are done with |this|.
288 // We use a stack-based reference to the message pump so that we can call
289 // ScheduleWork outside of incoming_queue_lock_.
[email protected]ea15e982008-08-15 07:31:20290
[email protected]fc7fb6e2008-08-16 03:09:05291 pump->ScheduleWork();
initial.commitd7cae122008-07-26 21:49:38292}
293
294void MessageLoop::SetNestableTasksAllowed(bool allowed) {
[email protected]124a2bdf2008-08-09 00:14:09295 if (nestable_tasks_allowed_ != allowed) {
296 nestable_tasks_allowed_ = allowed;
297 if (!nestable_tasks_allowed_)
298 return;
299 // Start the native pump if we are not already pumping.
[email protected]fc7fb6e2008-08-16 03:09:05300 pump_->ScheduleWork();
[email protected]124a2bdf2008-08-09 00:14:09301 }
initial.commitd7cae122008-07-26 21:49:38302}
303
304bool MessageLoop::NestableTasksAllowed() const {
305 return nestable_tasks_allowed_;
306}
307
[email protected]b5f95102009-07-01 19:53:59308bool MessageLoop::IsNested() {
309 return state_->run_depth > 1;
310}
311
initial.commitd7cae122008-07-26 21:49:38312//------------------------------------------------------------------------------
initial.commitd7cae122008-07-26 21:49:38313
initial.commitd7cae122008-07-26 21:49:38314void MessageLoop::RunTask(Task* task) {
initial.commitd7cae122008-07-26 21:49:38315 DCHECK(nestable_tasks_allowed_);
316 // Execute the task and assume the worst: It is probably not reentrant.
317 nestable_tasks_allowed_ = false;
[email protected]752578562008-09-07 08:08:29318
319 HistogramEvent(kTaskRunEvent);
320 task->Run();
321 delete task;
322
323 nestable_tasks_allowed_ = true;
initial.commitd7cae122008-07-26 21:49:38324}
325
[email protected]752578562008-09-07 08:08:29326bool MessageLoop::DeferOrRunPendingTask(const PendingTask& pending_task) {
327 if (pending_task.nestable || state_->run_depth == 1) {
328 RunTask(pending_task.task);
329 // Show that we ran a task (Note: a new one might arrive as a
330 // consequence!).
331 return true;
332 }
333
334 // We couldn't run the task now because we're in a nested message loop
335 // and the task isn't nestable.
336 deferred_non_nestable_work_queue_.push(pending_task);
337 return false;
initial.commitd7cae122008-07-26 21:49:38338}
339
[email protected]001747c2008-09-10 00:37:07340void MessageLoop::AddToDelayedWorkQueue(const PendingTask& pending_task) {
341 // Move to the delayed work queue. Initialize the sequence number
342 // before inserting into the delayed_work_queue_. The sequence number
343 // is used to faciliate FIFO sorting when two tasks have the same
344 // delayed_run_time value.
345 PendingTask new_pending_task(pending_task);
346 new_pending_task.sequence_num = next_sequence_num_++;
347 delayed_work_queue_.push(new_pending_task);
348}
349
initial.commitd7cae122008-07-26 21:49:38350void MessageLoop::ReloadWorkQueue() {
351 // We can improve performance of our loading tasks from incoming_queue_ to
[email protected]fc7fb6e2008-08-16 03:09:05352 // work_queue_ by waiting until the last minute (work_queue_ is empty) to
353 // load. That reduces the number of locks-per-task significantly when our
[email protected]752578562008-09-07 08:08:29354 // queues get large.
355 if (!work_queue_.empty())
initial.commitd7cae122008-07-26 21:49:38356 return; // Wait till we *really* need to lock and load.
357
358 // Acquire all we can from the inter-thread queue with one lock acquisition.
initial.commitd7cae122008-07-26 21:49:38359 {
360 AutoLock lock(incoming_queue_lock_);
[email protected]752578562008-09-07 08:08:29361 if (incoming_queue_.empty())
initial.commitd7cae122008-07-26 21:49:38362 return;
[email protected]b2f0ea12009-09-02 20:05:21363 incoming_queue_.Swap(&work_queue_); // Constant time
[email protected]752578562008-09-07 08:08:29364 DCHECK(incoming_queue_.empty());
initial.commitd7cae122008-07-26 21:49:38365 }
366}
367
[email protected]001747c2008-09-10 00:37:07368bool MessageLoop::DeletePendingTasks() {
369 bool did_work = !work_queue_.empty();
370 while (!work_queue_.empty()) {
371 PendingTask pending_task = work_queue_.front();
372 work_queue_.pop();
373 if (!pending_task.delayed_run_time.is_null()) {
374 // We want to delete delayed tasks in the same order in which they would
375 // normally be deleted in case of any funny dependencies between delayed
376 // tasks.
377 AddToDelayedWorkQueue(pending_task);
378 } else {
379 // TODO(darin): Delete all tasks once it is safe to do so.
[email protected]d680c5c2009-05-14 16:44:26380 // Until it is totally safe, just do it when running Purify or
381 // Valgrind.
[email protected]686dd8d2009-07-23 22:28:49382#if defined(PURIFY)
[email protected]404ec5e2009-03-11 20:06:02383 delete pending_task.task;
[email protected]d680c5c2009-05-14 16:44:26384#elif defined(OS_POSIX)
385 if (RUNNING_ON_VALGRIND)
386 delete pending_task.task;
387#endif // defined(OS_POSIX)
[email protected]001747c2008-09-10 00:37:07388 }
initial.commitd7cae122008-07-26 21:49:38389 }
[email protected]001747c2008-09-10 00:37:07390 did_work |= !deferred_non_nestable_work_queue_.empty();
391 while (!deferred_non_nestable_work_queue_.empty()) {
[email protected]20472832009-03-11 19:46:41392 // TODO(darin): Delete all tasks once it is safe to do so.
[email protected]686dd8d2009-07-23 22:28:49393 // Until it is totaly safe, only delete them under Purify and Valgrind.
394 Task* task = NULL;
395#if defined(PURIFY)
396 task = deferred_non_nestable_work_queue_.front().task;
397#elif defined(OS_POSIX)
398 if (RUNNING_ON_VALGRIND)
399 task = deferred_non_nestable_work_queue_.front().task;
[email protected]8df21d32009-03-11 19:53:50400#endif
401 deferred_non_nestable_work_queue_.pop();
[email protected]686dd8d2009-07-23 22:28:49402 if (task)
403 delete task;
initial.commitd7cae122008-07-26 21:49:38404 }
[email protected]001747c2008-09-10 00:37:07405 did_work |= !delayed_work_queue_.empty();
406 while (!delayed_work_queue_.empty()) {
407 Task* task = delayed_work_queue_.top().task;
408 delayed_work_queue_.pop();
409 delete task;
410 }
411 return did_work;
initial.commitd7cae122008-07-26 21:49:38412}
413
[email protected]fc7fb6e2008-08-16 03:09:05414bool MessageLoop::DoWork() {
[email protected]752578562008-09-07 08:08:29415 if (!nestable_tasks_allowed_) {
416 // Task can't be executed right now.
417 return false;
418 }
419
420 for (;;) {
421 ReloadWorkQueue();
422 if (work_queue_.empty())
423 break;
424
425 // Execute oldest task.
426 do {
427 PendingTask pending_task = work_queue_.front();
428 work_queue_.pop();
429 if (!pending_task.delayed_run_time.is_null()) {
[email protected]001747c2008-09-10 00:37:07430 AddToDelayedWorkQueue(pending_task);
[email protected]72deacd2008-09-23 19:19:20431 // If we changed the topmost task, then it is time to re-schedule.
432 if (delayed_work_queue_.top().task == pending_task.task)
[email protected]752578562008-09-07 08:08:29433 pump_->ScheduleDelayedWork(pending_task.delayed_run_time);
434 } else {
435 if (DeferOrRunPendingTask(pending_task))
436 return true;
437 }
438 } while (!work_queue_.empty());
439 }
440
441 // Nothing happened.
442 return false;
[email protected]fc7fb6e2008-08-16 03:09:05443}
444
[email protected]b24250fc2008-08-20 06:30:58445bool MessageLoop::DoDelayedWork(Time* next_delayed_work_time) {
[email protected]752578562008-09-07 08:08:29446 if (!nestable_tasks_allowed_ || delayed_work_queue_.empty()) {
447 *next_delayed_work_time = Time();
448 return false;
449 }
[email protected]1d2eb132008-12-08 17:36:06450
[email protected]752578562008-09-07 08:08:29451 if (delayed_work_queue_.top().delayed_run_time > Time::Now()) {
452 *next_delayed_work_time = delayed_work_queue_.top().delayed_run_time;
453 return false;
454 }
[email protected]fc7fb6e2008-08-16 03:09:05455
[email protected]752578562008-09-07 08:08:29456 PendingTask pending_task = delayed_work_queue_.top();
457 delayed_work_queue_.pop();
[email protected]1d2eb132008-12-08 17:36:06458
[email protected]752578562008-09-07 08:08:29459 if (!delayed_work_queue_.empty())
460 *next_delayed_work_time = delayed_work_queue_.top().delayed_run_time;
[email protected]fc7fb6e2008-08-16 03:09:05461
[email protected]752578562008-09-07 08:08:29462 return DeferOrRunPendingTask(pending_task);
[email protected]fc7fb6e2008-08-16 03:09:05463}
464
465bool MessageLoop::DoIdleWork() {
466 if (ProcessNextDelayedNonNestableTask())
467 return true;
468
469 if (state_->quit_received)
470 pump_->Quit();
471
472 return false;
473}
474
475//------------------------------------------------------------------------------
476// MessageLoop::AutoRunState
477
478MessageLoop::AutoRunState::AutoRunState(MessageLoop* loop) : loop_(loop) {
479 // Make the loop reference us.
480 previous_state_ = loop_->state_;
481 if (previous_state_) {
482 run_depth = previous_state_->run_depth + 1;
[email protected]ea15e982008-08-15 07:31:20483 } else {
[email protected]fc7fb6e2008-08-16 03:09:05484 run_depth = 1;
[email protected]ea15e982008-08-15 07:31:20485 }
[email protected]fc7fb6e2008-08-16 03:09:05486 loop_->state_ = this;
487
488 // Initialize the other fields:
489 quit_received = false;
[email protected]e43eddf12009-12-29 00:32:52490#if !defined(OS_MACOSX)
[email protected]fc7fb6e2008-08-16 03:09:05491 dispatcher = NULL;
492#endif
493}
494
495MessageLoop::AutoRunState::~AutoRunState() {
496 loop_->state_ = previous_state_;
[email protected]a5b94a92008-08-12 23:25:43497}
498
initial.commitd7cae122008-07-26 21:49:38499//------------------------------------------------------------------------------
[email protected]752578562008-09-07 08:08:29500// MessageLoop::PendingTask
initial.commitd7cae122008-07-26 21:49:38501
[email protected]752578562008-09-07 08:08:29502bool MessageLoop::PendingTask::operator<(const PendingTask& other) const {
503 // Since the top of a priority queue is defined as the "greatest" element, we
504 // need to invert the comparison here. We want the smaller time to be at the
505 // top of the heap.
initial.commitd7cae122008-07-26 21:49:38506
[email protected]752578562008-09-07 08:08:29507 if (delayed_run_time < other.delayed_run_time)
508 return false;
initial.commitd7cae122008-07-26 21:49:38509
[email protected]752578562008-09-07 08:08:29510 if (delayed_run_time > other.delayed_run_time)
511 return true;
initial.commitd7cae122008-07-26 21:49:38512
[email protected]752578562008-09-07 08:08:29513 // If the times happen to match, then we use the sequence number to decide.
514 // Compare the difference to support integer roll-over.
515 return (sequence_num - other.sequence_num) > 0;
initial.commitd7cae122008-07-26 21:49:38516}
517
518//------------------------------------------------------------------------------
519// Method and data for histogramming events and actions taken by each instance
520// on each thread.
521
522// static
523bool MessageLoop::enable_histogrammer_ = false;
524
525// static
526void MessageLoop::EnableHistogrammer(bool enable) {
527 enable_histogrammer_ = enable;
528}
529
530void MessageLoop::StartHistogrammer() {
531 if (enable_histogrammer_ && !message_histogram_.get()
532 && StatisticsRecorder::WasStarted()) {
[email protected]fc7fb6e2008-08-16 03:09:05533 DCHECK(!thread_name_.empty());
[email protected]2753b392009-12-28 06:59:52534 message_histogram_ = LinearHistogram::FactoryGet("MsgLoop:" + thread_name_,
535 kLeastNonZeroMessageId, kMaxMessageId,
536 kNumberOfDistinctMessagesDisplayed,
537 message_histogram_->kHexRangePrintingFlag);
initial.commitd7cae122008-07-26 21:49:38538 message_histogram_->SetRangeDescriptions(event_descriptions_);
539 }
540}
541
542void MessageLoop::HistogramEvent(int event) {
543 if (message_histogram_.get())
544 message_histogram_->Add(event);
545}
546
initial.commitd7cae122008-07-26 21:49:38547// Provide a macro that takes an expression (such as a constant, or macro
548// constant) and creates a pair to initalize an array of pairs. In this case,
549// our pair consists of the expressions value, and the "stringized" version
550// of the expression (i.e., the exrpression put in quotes). For example, if
551// we have:
552// #define FOO 2
553// #define BAR 5
554// then the following:
555// VALUE_TO_NUMBER_AND_NAME(FOO + BAR)
556// will expand to:
557// {7, "FOO + BAR"}
558// We use the resulting array as an argument to our histogram, which reads the
559// number as a bucket identifier, and proceeds to use the corresponding name
560// in the pair (i.e., the quoted string) when printing out a histogram.
561#define VALUE_TO_NUMBER_AND_NAME(name) {name, #name},
562
initial.commitd7cae122008-07-26 21:49:38563// static
564const LinearHistogram::DescriptionPair MessageLoop::event_descriptions_[] = {
initial.commitd7cae122008-07-26 21:49:38565 // Provide some pretty print capability in our histogram for our internal
566 // messages.
567
initial.commitd7cae122008-07-26 21:49:38568 // A few events we handle (kindred to messages), and used to profile actions.
569 VALUE_TO_NUMBER_AND_NAME(kTaskRunEvent)
initial.commitd7cae122008-07-26 21:49:38570 VALUE_TO_NUMBER_AND_NAME(kTimerEvent)
571
572 {-1, NULL} // The list must be null terminated, per API to histogram.
573};
license.botbf09a502008-08-24 00:55:55574
[email protected]4d9bdfaf2008-08-26 05:53:57575//------------------------------------------------------------------------------
576// MessageLoopForUI
577
[email protected]4d9bdfaf2008-08-26 05:53:57578#if defined(OS_WIN)
[email protected]4d9bdfaf2008-08-26 05:53:57579void MessageLoopForUI::WillProcessMessage(const MSG& message) {
580 pump_win()->WillProcessMessage(message);
581}
582void MessageLoopForUI::DidProcessMessage(const MSG& message) {
583 pump_win()->DidProcessMessage(message);
584}
585void MessageLoopForUI::PumpOutPendingPaintMessages() {
[email protected]17b89142008-11-07 21:52:15586 pump_ui()->PumpOutPendingPaintMessages();
[email protected]4d9bdfaf2008-08-26 05:53:57587}
588
589#endif // defined(OS_WIN)
590
[email protected]e43eddf12009-12-29 00:32:52591#if !defined(OS_MACOSX)
[email protected]148d1052009-07-31 22:53:37592void MessageLoopForUI::AddObserver(Observer* observer) {
593 pump_ui()->AddObserver(observer);
594}
595
596void MessageLoopForUI::RemoveObserver(Observer* observer) {
597 pump_ui()->RemoveObserver(observer);
598}
599
600void MessageLoopForUI::Run(Dispatcher* dispatcher) {
601 AutoRunState save_state(this);
602 state_->dispatcher = dispatcher;
603 RunHandler();
604}
[email protected]e43eddf12009-12-29 00:32:52605#endif // !defined(OS_MACOSX)
[email protected]148d1052009-07-31 22:53:37606
[email protected]4d9bdfaf2008-08-26 05:53:57607//------------------------------------------------------------------------------
608// MessageLoopForIO
609
610#if defined(OS_WIN)
611
[email protected]32cda29d2008-10-09 23:58:43612void MessageLoopForIO::RegisterIOHandler(HANDLE file, IOHandler* handler) {
613 pump_io()->RegisterIOHandler(file, handler);
614}
615
[email protected]17b89142008-11-07 21:52:15616bool MessageLoopForIO::WaitForIOCompletion(DWORD timeout, IOHandler* filter) {
617 return pump_io()->WaitForIOCompletion(timeout, filter);
[email protected]32cda29d2008-10-09 23:58:43618}
619
[email protected]36987e92008-09-18 18:46:26620#elif defined(OS_POSIX)
621
[email protected]e45e6c02008-12-15 22:02:17622bool MessageLoopForIO::WatchFileDescriptor(int fd,
623 bool persistent,
624 Mode mode,
625 FileDescriptorWatcher *controller,
626 Watcher *delegate) {
627 return pump_libevent()->WatchFileDescriptor(
628 fd,
629 persistent,
630 static_cast<base::MessagePumpLibevent::Mode>(mode),
631 controller,
632 delegate);
[email protected]36987e92008-09-18 18:46:26633}
634
[email protected]36987e92008-09-18 18:46:26635#endif