blob: 898fbfa94abd13eb95ca9b5c499dfc1b9168840f [file] [log] [blame]
license.botbf09a502008-08-24 00:55:551// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
2// 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"
21#endif
[email protected]8fc3a482008-10-03 16:52:5922#if defined(OS_LINUX)
23#include "base/message_pump_glib.h"
24#endif
[email protected]36987e92008-09-18 18:46:2625
[email protected]e1acf6f2008-10-27 20:43:3326using base::Time;
27using base::TimeDelta;
28
[email protected]f886b7bf2008-09-10 10:54:0629// A lazily created thread local storage for quick access to a thread's message
30// loop, if one exists. This should be safe and free of static constructors.
31static base::LazyInstance<base::ThreadLocalPointer<MessageLoop> > lazy_tls_ptr(
32 base::LINKER_INITIALIZED);
initial.commitd7cae122008-07-26 21:49:3833
34//------------------------------------------------------------------------------
35
initial.commitd7cae122008-07-26 21:49:3836// Logical events for Histogram profiling. Run with -message-loop-histogrammer
37// to get an accounting of messages and actions taken on each thread.
[email protected]fc7fb6e2008-08-16 03:09:0538static const int kTaskRunEvent = 0x1;
39static const int kTimerEvent = 0x2;
initial.commitd7cae122008-07-26 21:49:3840
41// Provide range of message IDs for use in histogramming and debug display.
42static const int kLeastNonZeroMessageId = 1;
43static const int kMaxMessageId = 1099;
44static const int kNumberOfDistinctMessagesDisplayed = 1100;
45
46//------------------------------------------------------------------------------
47
[email protected]fc7fb6e2008-08-16 03:09:0548#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:3849
initial.commitd7cae122008-07-26 21:49:3850// Upon a SEH exception in this thread, it restores the original unhandled
51// exception filter.
52static int SEHFilter(LPTOP_LEVEL_EXCEPTION_FILTER old_filter) {
53 ::SetUnhandledExceptionFilter(old_filter);
54 return EXCEPTION_CONTINUE_SEARCH;
55}
56
57// Retrieves a pointer to the current unhandled exception filter. There
58// is no standalone getter method.
59static LPTOP_LEVEL_EXCEPTION_FILTER GetTopSEHFilter() {
60 LPTOP_LEVEL_EXCEPTION_FILTER top_filter = NULL;
61 top_filter = ::SetUnhandledExceptionFilter(0);
62 ::SetUnhandledExceptionFilter(top_filter);
63 return top_filter;
64}
65
[email protected]fc7fb6e2008-08-16 03:09:0566#endif // defined(OS_WIN)
67
initial.commitd7cae122008-07-26 21:49:3868//------------------------------------------------------------------------------
69
[email protected]f886b7bf2008-09-10 10:54:0670// static
71MessageLoop* MessageLoop::current() {
72 // TODO(darin): sadly, we cannot enable this yet since people call us even
73 // when they have no intention of using us.
74 //DCHECK(loop) << "Ouch, did you forget to initialize me?";
75 return lazy_tls_ptr.Pointer()->Get();
76}
77
[email protected]4d9bdfaf2008-08-26 05:53:5778MessageLoop::MessageLoop(Type type)
79 : type_(type),
[email protected]a5b94a92008-08-12 23:25:4380 nestable_tasks_allowed_(true),
[email protected]b16ef312008-08-19 18:36:2381 exception_restoration_(false),
[email protected]752578562008-09-07 08:08:2982 state_(NULL),
83 next_sequence_num_(0) {
[email protected]f886b7bf2008-09-10 10:54:0684 DCHECK(!current()) << "should only have one message loop per thread";
85 lazy_tls_ptr.Pointer()->Set(this);
[email protected]4d9bdfaf2008-08-26 05:53:5786
[email protected]fc7fb6e2008-08-16 03:09:0587#if defined(OS_WIN)
[email protected]1a8f5d1d2008-09-25 20:33:0488 // TODO(rvargas): Get rid of the OS guards.
[email protected]9bcbf472008-08-30 00:22:4889 if (type_ == TYPE_DEFAULT) {
90 pump_ = new base::MessagePumpDefault();
[email protected]1a8f5d1d2008-09-25 20:33:0491 } else if (type_ == TYPE_IO) {
92 pump_ = new base::MessagePumpForIO();
[email protected]9bcbf472008-08-30 00:22:4893 } else {
[email protected]1a8f5d1d2008-09-25 20:33:0494 DCHECK(type_ == TYPE_UI);
95 pump_ = new base::MessagePumpForUI();
[email protected]9bcbf472008-08-30 00:22:4896 }
[email protected]36987e92008-09-18 18:46:2697#elif defined(OS_POSIX)
[email protected]96c9ea12008-09-23 21:08:2898 if (type_ == TYPE_UI) {
[email protected]8fc3a482008-10-03 16:52:5999#if defined(OS_MACOSX)
[email protected]96c9ea12008-09-23 21:08:28100 pump_ = base::MessagePumpMac::Create();
[email protected]8fc3a482008-10-03 16:52:59101#elif defined(OS_LINUX)
102 pump_ = new base::MessagePumpForUI();
103#endif // OS_LINUX
104 } else if (type_ == TYPE_IO) {
[email protected]36987e92008-09-18 18:46:26105 pump_ = new base::MessagePumpLibevent();
106 } else {
107 pump_ = new base::MessagePumpDefault();
108 }
[email protected]96c9ea12008-09-23 21:08:28109#endif // OS_POSIX
initial.commitd7cae122008-07-26 21:49:38110}
111
112MessageLoop::~MessageLoop() {
113 DCHECK(this == current());
[email protected]2a127252008-08-05 23:16:41114
115 // Let interested parties have one last shot at accessing this.
116 FOR_EACH_OBSERVER(DestructionObserver, destruction_observers_,
117 WillDestroyCurrentMessageLoop());
118
[email protected]08de3cde2008-09-09 05:55:35119 DCHECK(!state_);
120
[email protected]001747c2008-09-10 00:37:07121 // Clean up any unprocessed tasks, but take care: deleting a task could
122 // result in the addition of more tasks (e.g., via DeleteSoon). We set a
123 // limit on the number of times we will allow a deleted task to generate more
124 // tasks. Normally, we should only pass through this loop once or twice. If
125 // we end up hitting the loop limit, then it is probably due to one task that
126 // is being stubborn. Inspect the queues to see who is left.
127 bool did_work;
128 for (int i = 0; i < 100; ++i) {
129 DeletePendingTasks();
130 ReloadWorkQueue();
131 // If we end up with empty queues, then break out of the loop.
132 did_work = DeletePendingTasks();
133 if (!did_work)
134 break;
[email protected]08de3cde2008-09-09 05:55:35135 }
[email protected]001747c2008-09-10 00:37:07136 DCHECK(!did_work);
137
138 // OK, now make it so that no one can find us.
[email protected]2b89d22b22008-09-10 11:14:56139 lazy_tls_ptr.Pointer()->Set(NULL);
initial.commitd7cae122008-07-26 21:49:38140}
141
[email protected]2a127252008-08-05 23:16:41142void MessageLoop::AddDestructionObserver(DestructionObserver *obs) {
143 DCHECK(this == current());
144 destruction_observers_.AddObserver(obs);
145}
146
147void MessageLoop::RemoveDestructionObserver(DestructionObserver *obs) {
148 DCHECK(this == current());
149 destruction_observers_.RemoveObserver(obs);
150}
151
[email protected]ea15e982008-08-15 07:31:20152void MessageLoop::Run() {
[email protected]fc7fb6e2008-08-16 03:09:05153 AutoRunState save_state(this);
154 RunHandler();
[email protected]ea15e982008-08-15 07:31:20155}
156
[email protected]7e0e8762008-07-31 13:10:20157void MessageLoop::RunAllPending() {
[email protected]fc7fb6e2008-08-16 03:09:05158 AutoRunState save_state(this);
159 state_->quit_received = true; // Means run until we would otherwise block.
160 RunHandler();
initial.commitd7cae122008-07-26 21:49:38161}
162
163// Runs the loop in two different SEH modes:
164// enable_SEH_restoration_ = false : any unhandled exception goes to the last
165// one that calls SetUnhandledExceptionFilter().
166// enable_SEH_restoration_ = true : any unhandled exception goes to the filter
167// that was existed before the loop was run.
[email protected]fc7fb6e2008-08-16 03:09:05168void MessageLoop::RunHandler() {
169#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:38170 if (exception_restoration_) {
171 LPTOP_LEVEL_EXCEPTION_FILTER current_filter = GetTopSEHFilter();
172 __try {
[email protected]fc7fb6e2008-08-16 03:09:05173 RunInternal();
initial.commitd7cae122008-07-26 21:49:38174 } __except(SEHFilter(current_filter)) {
175 }
[email protected]fc7fb6e2008-08-16 03:09:05176 return;
initial.commitd7cae122008-07-26 21:49:38177 }
[email protected]fc7fb6e2008-08-16 03:09:05178#endif
179
180 RunInternal();
initial.commitd7cae122008-07-26 21:49:38181}
182
183//------------------------------------------------------------------------------
initial.commitd7cae122008-07-26 21:49:38184
[email protected]fc7fb6e2008-08-16 03:09:05185void MessageLoop::RunInternal() {
186 DCHECK(this == current());
187
initial.commitd7cae122008-07-26 21:49:38188 StartHistogrammer();
189
[email protected]fc7fb6e2008-08-16 03:09:05190#if defined(OS_WIN)
191 if (state_->dispatcher) {
192 pump_win()->RunWithDispatcher(this, state_->dispatcher);
193 return;
[email protected]7e0e8762008-07-31 13:10:20194 }
[email protected]fc7fb6e2008-08-16 03:09:05195#endif
[email protected]1d2eb132008-12-08 17:36:06196
[email protected]fc7fb6e2008-08-16 03:09:05197 pump_->Run(this);
[email protected]b8f2fe5d2008-07-30 07:50:53198}
[email protected]7622bd02008-07-30 06:58:56199
[email protected]3882c4332008-07-30 19:03:59200//------------------------------------------------------------------------------
201// Wrapper functions for use in above message loop framework.
202
initial.commitd7cae122008-07-26 21:49:38203bool MessageLoop::ProcessNextDelayedNonNestableTask() {
[email protected]fc7fb6e2008-08-16 03:09:05204 if (state_->run_depth != 1)
initial.commitd7cae122008-07-26 21:49:38205 return false;
206
[email protected]752578562008-09-07 08:08:29207 if (deferred_non_nestable_work_queue_.empty())
initial.commitd7cae122008-07-26 21:49:38208 return false;
[email protected]1d2eb132008-12-08 17:36:06209
[email protected]752578562008-09-07 08:08:29210 Task* task = deferred_non_nestable_work_queue_.front().task;
211 deferred_non_nestable_work_queue_.pop();
[email protected]1d2eb132008-12-08 17:36:06212
[email protected]752578562008-09-07 08:08:29213 RunTask(task);
initial.commitd7cae122008-07-26 21:49:38214 return true;
215}
216
initial.commitd7cae122008-07-26 21:49:38217//------------------------------------------------------------------------------
218
219void MessageLoop::Quit() {
[email protected]fc7fb6e2008-08-16 03:09:05220 DCHECK(current() == this);
221 if (state_) {
222 state_->quit_received = true;
223 } else {
224 NOTREACHED() << "Must be inside Run to call Quit";
initial.commitd7cae122008-07-26 21:49:38225 }
initial.commitd7cae122008-07-26 21:49:38226}
227
[email protected]752578562008-09-07 08:08:29228void MessageLoop::PostTask(
229 const tracked_objects::Location& from_here, Task* task) {
230 PostTask_Helper(from_here, task, 0, true);
231}
232
233void MessageLoop::PostDelayedTask(
234 const tracked_objects::Location& from_here, Task* task, int delay_ms) {
235 PostTask_Helper(from_here, task, delay_ms, true);
236}
237
238void MessageLoop::PostNonNestableTask(
239 const tracked_objects::Location& from_here, Task* task) {
240 PostTask_Helper(from_here, task, 0, false);
241}
242
243void MessageLoop::PostNonNestableDelayedTask(
244 const tracked_objects::Location& from_here, Task* task, int delay_ms) {
245 PostTask_Helper(from_here, task, delay_ms, false);
246}
247
initial.commitd7cae122008-07-26 21:49:38248// Possibly called on a background thread!
[email protected]752578562008-09-07 08:08:29249void MessageLoop::PostTask_Helper(
250 const tracked_objects::Location& from_here, Task* task, int delay_ms,
251 bool nestable) {
initial.commitd7cae122008-07-26 21:49:38252 task->SetBirthPlace(from_here);
[email protected]9bcbf472008-08-30 00:22:48253
[email protected]752578562008-09-07 08:08:29254 PendingTask pending_task(task, nestable);
[email protected]9bcbf472008-08-30 00:22:48255
256 if (delay_ms > 0) {
[email protected]752578562008-09-07 08:08:29257 pending_task.delayed_run_time =
[email protected]9bcbf472008-08-30 00:22:48258 Time::Now() + TimeDelta::FromMilliseconds(delay_ms);
259 } else {
260 DCHECK(delay_ms == 0) << "delay should not be negative";
261 }
262
initial.commitd7cae122008-07-26 21:49:38263 // Warning: Don't try to short-circuit, and handle this thread's tasks more
264 // directly, as it could starve handling of foreign threads. Put every task
265 // into this queue.
266
[email protected]fc7fb6e2008-08-16 03:09:05267 scoped_refptr<base::MessagePump> pump;
initial.commitd7cae122008-07-26 21:49:38268 {
[email protected]fc7fb6e2008-08-16 03:09:05269 AutoLock locked(incoming_queue_lock_);
270
[email protected]752578562008-09-07 08:08:29271 bool was_empty = incoming_queue_.empty();
272 incoming_queue_.push(pending_task);
initial.commitd7cae122008-07-26 21:49:38273 if (!was_empty)
274 return; // Someone else should have started the sub-pump.
275
[email protected]fc7fb6e2008-08-16 03:09:05276 pump = pump_;
[email protected]ea15e982008-08-15 07:31:20277 }
[email protected]fc7fb6e2008-08-16 03:09:05278 // Since the incoming_queue_ may contain a task that destroys this message
279 // loop, we cannot exit incoming_queue_lock_ until we are done with |this|.
280 // We use a stack-based reference to the message pump so that we can call
281 // ScheduleWork outside of incoming_queue_lock_.
[email protected]ea15e982008-08-15 07:31:20282
[email protected]fc7fb6e2008-08-16 03:09:05283 pump->ScheduleWork();
initial.commitd7cae122008-07-26 21:49:38284}
285
286void MessageLoop::SetNestableTasksAllowed(bool allowed) {
[email protected]124a2bdf2008-08-09 00:14:09287 if (nestable_tasks_allowed_ != allowed) {
288 nestable_tasks_allowed_ = allowed;
289 if (!nestable_tasks_allowed_)
290 return;
291 // Start the native pump if we are not already pumping.
[email protected]fc7fb6e2008-08-16 03:09:05292 pump_->ScheduleWork();
[email protected]124a2bdf2008-08-09 00:14:09293 }
initial.commitd7cae122008-07-26 21:49:38294}
295
296bool MessageLoop::NestableTasksAllowed() const {
297 return nestable_tasks_allowed_;
298}
299
initial.commitd7cae122008-07-26 21:49:38300//------------------------------------------------------------------------------
initial.commitd7cae122008-07-26 21:49:38301
initial.commitd7cae122008-07-26 21:49:38302void MessageLoop::RunTask(Task* task) {
initial.commitd7cae122008-07-26 21:49:38303 DCHECK(nestable_tasks_allowed_);
304 // Execute the task and assume the worst: It is probably not reentrant.
305 nestable_tasks_allowed_ = false;
[email protected]752578562008-09-07 08:08:29306
307 HistogramEvent(kTaskRunEvent);
308 task->Run();
309 delete task;
310
311 nestable_tasks_allowed_ = true;
initial.commitd7cae122008-07-26 21:49:38312}
313
[email protected]752578562008-09-07 08:08:29314bool MessageLoop::DeferOrRunPendingTask(const PendingTask& pending_task) {
315 if (pending_task.nestable || state_->run_depth == 1) {
316 RunTask(pending_task.task);
317 // Show that we ran a task (Note: a new one might arrive as a
318 // consequence!).
319 return true;
320 }
321
322 // We couldn't run the task now because we're in a nested message loop
323 // and the task isn't nestable.
324 deferred_non_nestable_work_queue_.push(pending_task);
325 return false;
initial.commitd7cae122008-07-26 21:49:38326}
327
[email protected]001747c2008-09-10 00:37:07328void MessageLoop::AddToDelayedWorkQueue(const PendingTask& pending_task) {
329 // Move to the delayed work queue. Initialize the sequence number
330 // before inserting into the delayed_work_queue_. The sequence number
331 // is used to faciliate FIFO sorting when two tasks have the same
332 // delayed_run_time value.
333 PendingTask new_pending_task(pending_task);
334 new_pending_task.sequence_num = next_sequence_num_++;
335 delayed_work_queue_.push(new_pending_task);
336}
337
initial.commitd7cae122008-07-26 21:49:38338void MessageLoop::ReloadWorkQueue() {
339 // We can improve performance of our loading tasks from incoming_queue_ to
[email protected]fc7fb6e2008-08-16 03:09:05340 // work_queue_ by waiting until the last minute (work_queue_ is empty) to
341 // load. That reduces the number of locks-per-task significantly when our
[email protected]752578562008-09-07 08:08:29342 // queues get large.
343 if (!work_queue_.empty())
initial.commitd7cae122008-07-26 21:49:38344 return; // Wait till we *really* need to lock and load.
345
346 // Acquire all we can from the inter-thread queue with one lock acquisition.
initial.commitd7cae122008-07-26 21:49:38347 {
348 AutoLock lock(incoming_queue_lock_);
[email protected]752578562008-09-07 08:08:29349 if (incoming_queue_.empty())
initial.commitd7cae122008-07-26 21:49:38350 return;
[email protected]752578562008-09-07 08:08:29351 std::swap(incoming_queue_, work_queue_);
352 DCHECK(incoming_queue_.empty());
initial.commitd7cae122008-07-26 21:49:38353 }
354}
355
[email protected]001747c2008-09-10 00:37:07356bool MessageLoop::DeletePendingTasks() {
357 bool did_work = !work_queue_.empty();
358 while (!work_queue_.empty()) {
359 PendingTask pending_task = work_queue_.front();
360 work_queue_.pop();
361 if (!pending_task.delayed_run_time.is_null()) {
362 // We want to delete delayed tasks in the same order in which they would
363 // normally be deleted in case of any funny dependencies between delayed
364 // tasks.
365 AddToDelayedWorkQueue(pending_task);
366 } else {
367 // TODO(darin): Delete all tasks once it is safe to do so.
368 //delete task;
369 }
initial.commitd7cae122008-07-26 21:49:38370 }
[email protected]001747c2008-09-10 00:37:07371 did_work |= !deferred_non_nestable_work_queue_.empty();
372 while (!deferred_non_nestable_work_queue_.empty()) {
[email protected]001747c2008-09-10 00:37:07373 // TODO(darin): Delete all tasks once it is safe to do so.
[email protected]058bccb2008-09-10 01:16:17374 //Task* task = deferred_non_nestable_work_queue_.front().task;
375 deferred_non_nestable_work_queue_.pop();
[email protected]001747c2008-09-10 00:37:07376 //delete task;
initial.commitd7cae122008-07-26 21:49:38377 }
[email protected]001747c2008-09-10 00:37:07378 did_work |= !delayed_work_queue_.empty();
379 while (!delayed_work_queue_.empty()) {
380 Task* task = delayed_work_queue_.top().task;
381 delayed_work_queue_.pop();
382 delete task;
383 }
384 return did_work;
initial.commitd7cae122008-07-26 21:49:38385}
386
[email protected]fc7fb6e2008-08-16 03:09:05387bool MessageLoop::DoWork() {
[email protected]752578562008-09-07 08:08:29388 if (!nestable_tasks_allowed_) {
389 // Task can't be executed right now.
390 return false;
391 }
392
393 for (;;) {
394 ReloadWorkQueue();
395 if (work_queue_.empty())
396 break;
397
398 // Execute oldest task.
399 do {
400 PendingTask pending_task = work_queue_.front();
401 work_queue_.pop();
402 if (!pending_task.delayed_run_time.is_null()) {
[email protected]001747c2008-09-10 00:37:07403 AddToDelayedWorkQueue(pending_task);
[email protected]72deacd2008-09-23 19:19:20404 // If we changed the topmost task, then it is time to re-schedule.
405 if (delayed_work_queue_.top().task == pending_task.task)
[email protected]752578562008-09-07 08:08:29406 pump_->ScheduleDelayedWork(pending_task.delayed_run_time);
407 } else {
408 if (DeferOrRunPendingTask(pending_task))
409 return true;
410 }
411 } while (!work_queue_.empty());
412 }
413
414 // Nothing happened.
415 return false;
[email protected]fc7fb6e2008-08-16 03:09:05416}
417
[email protected]b24250fc2008-08-20 06:30:58418bool MessageLoop::DoDelayedWork(Time* next_delayed_work_time) {
[email protected]752578562008-09-07 08:08:29419 if (!nestable_tasks_allowed_ || delayed_work_queue_.empty()) {
420 *next_delayed_work_time = Time();
421 return false;
422 }
[email protected]1d2eb132008-12-08 17:36:06423
[email protected]752578562008-09-07 08:08:29424 if (delayed_work_queue_.top().delayed_run_time > Time::Now()) {
425 *next_delayed_work_time = delayed_work_queue_.top().delayed_run_time;
426 return false;
427 }
[email protected]fc7fb6e2008-08-16 03:09:05428
[email protected]752578562008-09-07 08:08:29429 PendingTask pending_task = delayed_work_queue_.top();
430 delayed_work_queue_.pop();
[email protected]1d2eb132008-12-08 17:36:06431
[email protected]752578562008-09-07 08:08:29432 if (!delayed_work_queue_.empty())
433 *next_delayed_work_time = delayed_work_queue_.top().delayed_run_time;
[email protected]fc7fb6e2008-08-16 03:09:05434
[email protected]752578562008-09-07 08:08:29435 return DeferOrRunPendingTask(pending_task);
[email protected]fc7fb6e2008-08-16 03:09:05436}
437
438bool MessageLoop::DoIdleWork() {
439 if (ProcessNextDelayedNonNestableTask())
440 return true;
441
442 if (state_->quit_received)
443 pump_->Quit();
444
445 return false;
446}
447
448//------------------------------------------------------------------------------
449// MessageLoop::AutoRunState
450
451MessageLoop::AutoRunState::AutoRunState(MessageLoop* loop) : loop_(loop) {
452 // Make the loop reference us.
453 previous_state_ = loop_->state_;
454 if (previous_state_) {
455 run_depth = previous_state_->run_depth + 1;
[email protected]ea15e982008-08-15 07:31:20456 } else {
[email protected]fc7fb6e2008-08-16 03:09:05457 run_depth = 1;
[email protected]ea15e982008-08-15 07:31:20458 }
[email protected]fc7fb6e2008-08-16 03:09:05459 loop_->state_ = this;
460
461 // Initialize the other fields:
462 quit_received = false;
463#if defined(OS_WIN)
464 dispatcher = NULL;
465#endif
466}
467
468MessageLoop::AutoRunState::~AutoRunState() {
469 loop_->state_ = previous_state_;
[email protected]a5b94a92008-08-12 23:25:43470}
471
initial.commitd7cae122008-07-26 21:49:38472//------------------------------------------------------------------------------
[email protected]752578562008-09-07 08:08:29473// MessageLoop::PendingTask
initial.commitd7cae122008-07-26 21:49:38474
[email protected]752578562008-09-07 08:08:29475bool MessageLoop::PendingTask::operator<(const PendingTask& other) const {
476 // Since the top of a priority queue is defined as the "greatest" element, we
477 // need to invert the comparison here. We want the smaller time to be at the
478 // top of the heap.
initial.commitd7cae122008-07-26 21:49:38479
[email protected]752578562008-09-07 08:08:29480 if (delayed_run_time < other.delayed_run_time)
481 return false;
initial.commitd7cae122008-07-26 21:49:38482
[email protected]752578562008-09-07 08:08:29483 if (delayed_run_time > other.delayed_run_time)
484 return true;
initial.commitd7cae122008-07-26 21:49:38485
[email protected]752578562008-09-07 08:08:29486 // If the times happen to match, then we use the sequence number to decide.
487 // Compare the difference to support integer roll-over.
488 return (sequence_num - other.sequence_num) > 0;
initial.commitd7cae122008-07-26 21:49:38489}
490
491//------------------------------------------------------------------------------
492// Method and data for histogramming events and actions taken by each instance
493// on each thread.
494
495// static
496bool MessageLoop::enable_histogrammer_ = false;
497
498// static
499void MessageLoop::EnableHistogrammer(bool enable) {
500 enable_histogrammer_ = enable;
501}
502
503void MessageLoop::StartHistogrammer() {
504 if (enable_histogrammer_ && !message_histogram_.get()
505 && StatisticsRecorder::WasStarted()) {
[email protected]fc7fb6e2008-08-16 03:09:05506 DCHECK(!thread_name_.empty());
initial.commitd7cae122008-07-26 21:49:38507 message_histogram_.reset(new LinearHistogram(
508 ASCIIToWide("MsgLoop:" + thread_name_).c_str(),
509 kLeastNonZeroMessageId,
510 kMaxMessageId,
511 kNumberOfDistinctMessagesDisplayed));
512 message_histogram_->SetFlags(message_histogram_->kHexRangePrintingFlag);
513 message_histogram_->SetRangeDescriptions(event_descriptions_);
514 }
515}
516
517void MessageLoop::HistogramEvent(int event) {
518 if (message_histogram_.get())
519 message_histogram_->Add(event);
520}
521
initial.commitd7cae122008-07-26 21:49:38522// Provide a macro that takes an expression (such as a constant, or macro
523// constant) and creates a pair to initalize an array of pairs. In this case,
524// our pair consists of the expressions value, and the "stringized" version
525// of the expression (i.e., the exrpression put in quotes). For example, if
526// we have:
527// #define FOO 2
528// #define BAR 5
529// then the following:
530// VALUE_TO_NUMBER_AND_NAME(FOO + BAR)
531// will expand to:
532// {7, "FOO + BAR"}
533// We use the resulting array as an argument to our histogram, which reads the
534// number as a bucket identifier, and proceeds to use the corresponding name
535// in the pair (i.e., the quoted string) when printing out a histogram.
536#define VALUE_TO_NUMBER_AND_NAME(name) {name, #name},
537
initial.commitd7cae122008-07-26 21:49:38538// static
539const LinearHistogram::DescriptionPair MessageLoop::event_descriptions_[] = {
initial.commitd7cae122008-07-26 21:49:38540 // Provide some pretty print capability in our histogram for our internal
541 // messages.
542
initial.commitd7cae122008-07-26 21:49:38543 // A few events we handle (kindred to messages), and used to profile actions.
544 VALUE_TO_NUMBER_AND_NAME(kTaskRunEvent)
initial.commitd7cae122008-07-26 21:49:38545 VALUE_TO_NUMBER_AND_NAME(kTimerEvent)
546
547 {-1, NULL} // The list must be null terminated, per API to histogram.
548};
license.botbf09a502008-08-24 00:55:55549
[email protected]4d9bdfaf2008-08-26 05:53:57550//------------------------------------------------------------------------------
551// MessageLoopForUI
552
553#if defined(OS_WIN)
554
555void MessageLoopForUI::Run(Dispatcher* dispatcher) {
556 AutoRunState save_state(this);
557 state_->dispatcher = dispatcher;
558 RunHandler();
559}
560
561void MessageLoopForUI::AddObserver(Observer* observer) {
562 pump_win()->AddObserver(observer);
563}
564
565void MessageLoopForUI::RemoveObserver(Observer* observer) {
566 pump_win()->RemoveObserver(observer);
567}
568
569void MessageLoopForUI::WillProcessMessage(const MSG& message) {
570 pump_win()->WillProcessMessage(message);
571}
572void MessageLoopForUI::DidProcessMessage(const MSG& message) {
573 pump_win()->DidProcessMessage(message);
574}
575void MessageLoopForUI::PumpOutPendingPaintMessages() {
[email protected]17b89142008-11-07 21:52:15576 pump_ui()->PumpOutPendingPaintMessages();
[email protected]4d9bdfaf2008-08-26 05:53:57577}
578
579#endif // defined(OS_WIN)
580
581//------------------------------------------------------------------------------
582// MessageLoopForIO
583
584#if defined(OS_WIN)
585
[email protected]32cda29d2008-10-09 23:58:43586void MessageLoopForIO::RegisterIOHandler(HANDLE file, IOHandler* handler) {
587 pump_io()->RegisterIOHandler(file, handler);
588}
589
[email protected]17b89142008-11-07 21:52:15590bool MessageLoopForIO::WaitForIOCompletion(DWORD timeout, IOHandler* filter) {
591 return pump_io()->WaitForIOCompletion(timeout, filter);
[email protected]32cda29d2008-10-09 23:58:43592}
593
[email protected]36987e92008-09-18 18:46:26594#elif defined(OS_POSIX)
595
[email protected]1d2eb132008-12-08 17:36:06596void MessageLoopForIO::WatchSocket(int socket, short interest_mask,
[email protected]36987e92008-09-18 18:46:26597 struct event* e, Watcher* watcher) {
598 pump_libevent()->WatchSocket(socket, interest_mask, e, watcher);
599}
600
[email protected]1d2eb132008-12-08 17:36:06601void MessageLoopForIO::WatchFileHandle(int fd, short interest_mask,
602 struct event* e, FileWatcher* watcher) {
603 pump_libevent()->WatchFileHandle(fd, interest_mask, e, watcher);
604}
605
606
[email protected]36987e92008-09-18 18:46:26607void MessageLoopForIO::UnwatchSocket(struct event* e) {
608 pump_libevent()->UnwatchSocket(e);
609}
[email protected]1d2eb132008-12-08 17:36:06610
611void MessageLoopForIO::UnwatchFileHandle(struct event* e) {
612 pump_libevent()->UnwatchFileHandle(e);
613}
[email protected]36987e92008-09-18 18:46:26614#endif