blob: 60a8ce5689c815ce5938663950c1b926e08b359e [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
22
[email protected]f886b7bf2008-09-10 10:54:0623// A lazily created thread local storage for quick access to a thread's message
24// loop, if one exists. This should be safe and free of static constructors.
25static base::LazyInstance<base::ThreadLocalPointer<MessageLoop> > lazy_tls_ptr(
26 base::LINKER_INITIALIZED);
initial.commitd7cae122008-07-26 21:49:3827
28//------------------------------------------------------------------------------
29
initial.commitd7cae122008-07-26 21:49:3830// Logical events for Histogram profiling. Run with -message-loop-histogrammer
31// to get an accounting of messages and actions taken on each thread.
[email protected]fc7fb6e2008-08-16 03:09:0532static const int kTaskRunEvent = 0x1;
33static const int kTimerEvent = 0x2;
initial.commitd7cae122008-07-26 21:49:3834
35// Provide range of message IDs for use in histogramming and debug display.
36static const int kLeastNonZeroMessageId = 1;
37static const int kMaxMessageId = 1099;
38static const int kNumberOfDistinctMessagesDisplayed = 1100;
39
40//------------------------------------------------------------------------------
41
[email protected]fc7fb6e2008-08-16 03:09:0542#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:3843
initial.commitd7cae122008-07-26 21:49:3844// Upon a SEH exception in this thread, it restores the original unhandled
45// exception filter.
46static int SEHFilter(LPTOP_LEVEL_EXCEPTION_FILTER old_filter) {
47 ::SetUnhandledExceptionFilter(old_filter);
48 return EXCEPTION_CONTINUE_SEARCH;
49}
50
51// Retrieves a pointer to the current unhandled exception filter. There
52// is no standalone getter method.
53static LPTOP_LEVEL_EXCEPTION_FILTER GetTopSEHFilter() {
54 LPTOP_LEVEL_EXCEPTION_FILTER top_filter = NULL;
55 top_filter = ::SetUnhandledExceptionFilter(0);
56 ::SetUnhandledExceptionFilter(top_filter);
57 return top_filter;
58}
59
[email protected]fc7fb6e2008-08-16 03:09:0560#endif // defined(OS_WIN)
61
initial.commitd7cae122008-07-26 21:49:3862//------------------------------------------------------------------------------
63
[email protected]f886b7bf2008-09-10 10:54:0664// static
65MessageLoop* MessageLoop::current() {
66 // TODO(darin): sadly, we cannot enable this yet since people call us even
67 // when they have no intention of using us.
68 //DCHECK(loop) << "Ouch, did you forget to initialize me?";
69 return lazy_tls_ptr.Pointer()->Get();
70}
71
[email protected]4d9bdfaf2008-08-26 05:53:5772MessageLoop::MessageLoop(Type type)
73 : type_(type),
[email protected]a5b94a92008-08-12 23:25:4374 nestable_tasks_allowed_(true),
[email protected]b16ef312008-08-19 18:36:2375 exception_restoration_(false),
[email protected]752578562008-09-07 08:08:2976 state_(NULL),
77 next_sequence_num_(0) {
[email protected]f886b7bf2008-09-10 10:54:0678 DCHECK(!current()) << "should only have one message loop per thread";
79 lazy_tls_ptr.Pointer()->Set(this);
[email protected]4d9bdfaf2008-08-26 05:53:5780
[email protected]fc7fb6e2008-08-16 03:09:0581#if defined(OS_WIN)
[email protected]1a8f5d1d2008-09-25 20:33:0482 // TODO(rvargas): Get rid of the OS guards.
[email protected]9bcbf472008-08-30 00:22:4883 if (type_ == TYPE_DEFAULT) {
84 pump_ = new base::MessagePumpDefault();
[email protected]1a8f5d1d2008-09-25 20:33:0485 } else if (type_ == TYPE_IO) {
86 pump_ = new base::MessagePumpForIO();
[email protected]9bcbf472008-08-30 00:22:4887 } else {
[email protected]1a8f5d1d2008-09-25 20:33:0488 DCHECK(type_ == TYPE_UI);
89 pump_ = new base::MessagePumpForUI();
[email protected]9bcbf472008-08-30 00:22:4890 }
[email protected]36987e92008-09-18 18:46:2691#elif defined(OS_POSIX)
[email protected]96c9ea12008-09-23 21:08:2892#if defined(OS_MACOSX)
93 if (type_ == TYPE_UI) {
94 pump_ = base::MessagePumpMac::Create();
95 } else
96#endif // OS_MACOSX
[email protected]36987e92008-09-18 18:46:2697 if (type_ == TYPE_IO) {
98 pump_ = new base::MessagePumpLibevent();
99 } else {
100 pump_ = new base::MessagePumpDefault();
101 }
[email protected]96c9ea12008-09-23 21:08:28102#else // OS_POSIX
[email protected]ef8963172008-08-26 06:52:41103 pump_ = new base::MessagePumpDefault();
[email protected]96c9ea12008-09-23 21:08:28104#endif // OS_POSIX
initial.commitd7cae122008-07-26 21:49:38105}
106
107MessageLoop::~MessageLoop() {
108 DCHECK(this == current());
[email protected]2a127252008-08-05 23:16:41109
110 // Let interested parties have one last shot at accessing this.
111 FOR_EACH_OBSERVER(DestructionObserver, destruction_observers_,
112 WillDestroyCurrentMessageLoop());
113
[email protected]08de3cde2008-09-09 05:55:35114 DCHECK(!state_);
115
[email protected]001747c2008-09-10 00:37:07116 // Clean up any unprocessed tasks, but take care: deleting a task could
117 // result in the addition of more tasks (e.g., via DeleteSoon). We set a
118 // limit on the number of times we will allow a deleted task to generate more
119 // tasks. Normally, we should only pass through this loop once or twice. If
120 // we end up hitting the loop limit, then it is probably due to one task that
121 // is being stubborn. Inspect the queues to see who is left.
122 bool did_work;
123 for (int i = 0; i < 100; ++i) {
124 DeletePendingTasks();
125 ReloadWorkQueue();
126 // If we end up with empty queues, then break out of the loop.
127 did_work = DeletePendingTasks();
128 if (!did_work)
129 break;
[email protected]08de3cde2008-09-09 05:55:35130 }
[email protected]001747c2008-09-10 00:37:07131 DCHECK(!did_work);
132
133 // OK, now make it so that no one can find us.
[email protected]2b89d22b22008-09-10 11:14:56134 lazy_tls_ptr.Pointer()->Set(NULL);
initial.commitd7cae122008-07-26 21:49:38135}
136
[email protected]2a127252008-08-05 23:16:41137void MessageLoop::AddDestructionObserver(DestructionObserver *obs) {
138 DCHECK(this == current());
139 destruction_observers_.AddObserver(obs);
140}
141
142void MessageLoop::RemoveDestructionObserver(DestructionObserver *obs) {
143 DCHECK(this == current());
144 destruction_observers_.RemoveObserver(obs);
145}
146
[email protected]ea15e982008-08-15 07:31:20147void MessageLoop::Run() {
[email protected]fc7fb6e2008-08-16 03:09:05148 AutoRunState save_state(this);
149 RunHandler();
[email protected]ea15e982008-08-15 07:31:20150}
151
[email protected]7e0e8762008-07-31 13:10:20152void MessageLoop::RunAllPending() {
[email protected]fc7fb6e2008-08-16 03:09:05153 AutoRunState save_state(this);
154 state_->quit_received = true; // Means run until we would otherwise block.
155 RunHandler();
initial.commitd7cae122008-07-26 21:49:38156}
157
158// Runs the loop in two different SEH modes:
159// enable_SEH_restoration_ = false : any unhandled exception goes to the last
160// one that calls SetUnhandledExceptionFilter().
161// enable_SEH_restoration_ = true : any unhandled exception goes to the filter
162// that was existed before the loop was run.
[email protected]fc7fb6e2008-08-16 03:09:05163void MessageLoop::RunHandler() {
164#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:38165 if (exception_restoration_) {
166 LPTOP_LEVEL_EXCEPTION_FILTER current_filter = GetTopSEHFilter();
167 __try {
[email protected]fc7fb6e2008-08-16 03:09:05168 RunInternal();
initial.commitd7cae122008-07-26 21:49:38169 } __except(SEHFilter(current_filter)) {
170 }
[email protected]fc7fb6e2008-08-16 03:09:05171 return;
initial.commitd7cae122008-07-26 21:49:38172 }
[email protected]fc7fb6e2008-08-16 03:09:05173#endif
174
175 RunInternal();
initial.commitd7cae122008-07-26 21:49:38176}
177
178//------------------------------------------------------------------------------
initial.commitd7cae122008-07-26 21:49:38179
[email protected]fc7fb6e2008-08-16 03:09:05180void MessageLoop::RunInternal() {
181 DCHECK(this == current());
182
initial.commitd7cae122008-07-26 21:49:38183 StartHistogrammer();
184
[email protected]fc7fb6e2008-08-16 03:09:05185#if defined(OS_WIN)
186 if (state_->dispatcher) {
187 pump_win()->RunWithDispatcher(this, state_->dispatcher);
188 return;
[email protected]7e0e8762008-07-31 13:10:20189 }
[email protected]fc7fb6e2008-08-16 03:09:05190#endif
191
192 pump_->Run(this);
[email protected]b8f2fe5d2008-07-30 07:50:53193}
[email protected]7622bd02008-07-30 06:58:56194
[email protected]3882c4332008-07-30 19:03:59195//------------------------------------------------------------------------------
196// Wrapper functions for use in above message loop framework.
197
initial.commitd7cae122008-07-26 21:49:38198bool MessageLoop::ProcessNextDelayedNonNestableTask() {
[email protected]fc7fb6e2008-08-16 03:09:05199 if (state_->run_depth != 1)
initial.commitd7cae122008-07-26 21:49:38200 return false;
201
[email protected]752578562008-09-07 08:08:29202 if (deferred_non_nestable_work_queue_.empty())
initial.commitd7cae122008-07-26 21:49:38203 return false;
[email protected]752578562008-09-07 08:08:29204
205 Task* task = deferred_non_nestable_work_queue_.front().task;
206 deferred_non_nestable_work_queue_.pop();
207
208 RunTask(task);
initial.commitd7cae122008-07-26 21:49:38209 return true;
210}
211
initial.commitd7cae122008-07-26 21:49:38212//------------------------------------------------------------------------------
213
214void MessageLoop::Quit() {
[email protected]fc7fb6e2008-08-16 03:09:05215 DCHECK(current() == this);
216 if (state_) {
217 state_->quit_received = true;
218 } else {
219 NOTREACHED() << "Must be inside Run to call Quit";
initial.commitd7cae122008-07-26 21:49:38220 }
initial.commitd7cae122008-07-26 21:49:38221}
222
[email protected]752578562008-09-07 08:08:29223void MessageLoop::PostTask(
224 const tracked_objects::Location& from_here, Task* task) {
225 PostTask_Helper(from_here, task, 0, true);
226}
227
228void MessageLoop::PostDelayedTask(
229 const tracked_objects::Location& from_here, Task* task, int delay_ms) {
230 PostTask_Helper(from_here, task, delay_ms, true);
231}
232
233void MessageLoop::PostNonNestableTask(
234 const tracked_objects::Location& from_here, Task* task) {
235 PostTask_Helper(from_here, task, 0, false);
236}
237
238void MessageLoop::PostNonNestableDelayedTask(
239 const tracked_objects::Location& from_here, Task* task, int delay_ms) {
240 PostTask_Helper(from_here, task, delay_ms, false);
241}
242
initial.commitd7cae122008-07-26 21:49:38243// Possibly called on a background thread!
[email protected]752578562008-09-07 08:08:29244void MessageLoop::PostTask_Helper(
245 const tracked_objects::Location& from_here, Task* task, int delay_ms,
246 bool nestable) {
initial.commitd7cae122008-07-26 21:49:38247 task->SetBirthPlace(from_here);
[email protected]9bcbf472008-08-30 00:22:48248
[email protected]752578562008-09-07 08:08:29249 PendingTask pending_task(task, nestable);
[email protected]9bcbf472008-08-30 00:22:48250
251 if (delay_ms > 0) {
[email protected]752578562008-09-07 08:08:29252 pending_task.delayed_run_time =
[email protected]9bcbf472008-08-30 00:22:48253 Time::Now() + TimeDelta::FromMilliseconds(delay_ms);
254 } else {
255 DCHECK(delay_ms == 0) << "delay should not be negative";
256 }
257
initial.commitd7cae122008-07-26 21:49:38258 // Warning: Don't try to short-circuit, and handle this thread's tasks more
259 // directly, as it could starve handling of foreign threads. Put every task
260 // into this queue.
261
[email protected]fc7fb6e2008-08-16 03:09:05262 scoped_refptr<base::MessagePump> pump;
initial.commitd7cae122008-07-26 21:49:38263 {
[email protected]fc7fb6e2008-08-16 03:09:05264 AutoLock locked(incoming_queue_lock_);
265
[email protected]752578562008-09-07 08:08:29266 bool was_empty = incoming_queue_.empty();
267 incoming_queue_.push(pending_task);
initial.commitd7cae122008-07-26 21:49:38268 if (!was_empty)
269 return; // Someone else should have started the sub-pump.
270
[email protected]fc7fb6e2008-08-16 03:09:05271 pump = pump_;
[email protected]ea15e982008-08-15 07:31:20272 }
[email protected]fc7fb6e2008-08-16 03:09:05273 // Since the incoming_queue_ may contain a task that destroys this message
274 // loop, we cannot exit incoming_queue_lock_ until we are done with |this|.
275 // We use a stack-based reference to the message pump so that we can call
276 // ScheduleWork outside of incoming_queue_lock_.
[email protected]ea15e982008-08-15 07:31:20277
[email protected]fc7fb6e2008-08-16 03:09:05278 pump->ScheduleWork();
initial.commitd7cae122008-07-26 21:49:38279}
280
281void MessageLoop::SetNestableTasksAllowed(bool allowed) {
[email protected]124a2bdf2008-08-09 00:14:09282 if (nestable_tasks_allowed_ != allowed) {
283 nestable_tasks_allowed_ = allowed;
284 if (!nestable_tasks_allowed_)
285 return;
286 // Start the native pump if we are not already pumping.
[email protected]fc7fb6e2008-08-16 03:09:05287 pump_->ScheduleWork();
[email protected]124a2bdf2008-08-09 00:14:09288 }
initial.commitd7cae122008-07-26 21:49:38289}
290
291bool MessageLoop::NestableTasksAllowed() const {
292 return nestable_tasks_allowed_;
293}
294
initial.commitd7cae122008-07-26 21:49:38295//------------------------------------------------------------------------------
initial.commitd7cae122008-07-26 21:49:38296
initial.commitd7cae122008-07-26 21:49:38297void MessageLoop::RunTask(Task* task) {
initial.commitd7cae122008-07-26 21:49:38298 DCHECK(nestable_tasks_allowed_);
299 // Execute the task and assume the worst: It is probably not reentrant.
300 nestable_tasks_allowed_ = false;
[email protected]752578562008-09-07 08:08:29301
302 HistogramEvent(kTaskRunEvent);
303 task->Run();
304 delete task;
305
306 nestable_tasks_allowed_ = true;
initial.commitd7cae122008-07-26 21:49:38307}
308
[email protected]752578562008-09-07 08:08:29309bool MessageLoop::DeferOrRunPendingTask(const PendingTask& pending_task) {
310 if (pending_task.nestable || state_->run_depth == 1) {
311 RunTask(pending_task.task);
312 // Show that we ran a task (Note: a new one might arrive as a
313 // consequence!).
314 return true;
315 }
316
317 // We couldn't run the task now because we're in a nested message loop
318 // and the task isn't nestable.
319 deferred_non_nestable_work_queue_.push(pending_task);
320 return false;
initial.commitd7cae122008-07-26 21:49:38321}
322
[email protected]001747c2008-09-10 00:37:07323void MessageLoop::AddToDelayedWorkQueue(const PendingTask& pending_task) {
324 // Move to the delayed work queue. Initialize the sequence number
325 // before inserting into the delayed_work_queue_. The sequence number
326 // is used to faciliate FIFO sorting when two tasks have the same
327 // delayed_run_time value.
328 PendingTask new_pending_task(pending_task);
329 new_pending_task.sequence_num = next_sequence_num_++;
330 delayed_work_queue_.push(new_pending_task);
331}
332
initial.commitd7cae122008-07-26 21:49:38333void MessageLoop::ReloadWorkQueue() {
334 // We can improve performance of our loading tasks from incoming_queue_ to
[email protected]fc7fb6e2008-08-16 03:09:05335 // work_queue_ by waiting until the last minute (work_queue_ is empty) to
336 // load. That reduces the number of locks-per-task significantly when our
[email protected]752578562008-09-07 08:08:29337 // queues get large.
338 if (!work_queue_.empty())
initial.commitd7cae122008-07-26 21:49:38339 return; // Wait till we *really* need to lock and load.
340
341 // Acquire all we can from the inter-thread queue with one lock acquisition.
initial.commitd7cae122008-07-26 21:49:38342 {
343 AutoLock lock(incoming_queue_lock_);
[email protected]752578562008-09-07 08:08:29344 if (incoming_queue_.empty())
initial.commitd7cae122008-07-26 21:49:38345 return;
[email protected]752578562008-09-07 08:08:29346 std::swap(incoming_queue_, work_queue_);
347 DCHECK(incoming_queue_.empty());
initial.commitd7cae122008-07-26 21:49:38348 }
349}
350
[email protected]001747c2008-09-10 00:37:07351bool MessageLoop::DeletePendingTasks() {
352 bool did_work = !work_queue_.empty();
353 while (!work_queue_.empty()) {
354 PendingTask pending_task = work_queue_.front();
355 work_queue_.pop();
356 if (!pending_task.delayed_run_time.is_null()) {
357 // We want to delete delayed tasks in the same order in which they would
358 // normally be deleted in case of any funny dependencies between delayed
359 // tasks.
360 AddToDelayedWorkQueue(pending_task);
361 } else {
362 // TODO(darin): Delete all tasks once it is safe to do so.
363 //delete task;
364 }
initial.commitd7cae122008-07-26 21:49:38365 }
[email protected]001747c2008-09-10 00:37:07366 did_work |= !deferred_non_nestable_work_queue_.empty();
367 while (!deferred_non_nestable_work_queue_.empty()) {
[email protected]001747c2008-09-10 00:37:07368 // TODO(darin): Delete all tasks once it is safe to do so.
[email protected]058bccb2008-09-10 01:16:17369 //Task* task = deferred_non_nestable_work_queue_.front().task;
370 deferred_non_nestable_work_queue_.pop();
[email protected]001747c2008-09-10 00:37:07371 //delete task;
initial.commitd7cae122008-07-26 21:49:38372 }
[email protected]001747c2008-09-10 00:37:07373 did_work |= !delayed_work_queue_.empty();
374 while (!delayed_work_queue_.empty()) {
375 Task* task = delayed_work_queue_.top().task;
376 delayed_work_queue_.pop();
377 delete task;
378 }
379 return did_work;
initial.commitd7cae122008-07-26 21:49:38380}
381
[email protected]fc7fb6e2008-08-16 03:09:05382bool MessageLoop::DoWork() {
[email protected]752578562008-09-07 08:08:29383 if (!nestable_tasks_allowed_) {
384 // Task can't be executed right now.
385 return false;
386 }
387
388 for (;;) {
389 ReloadWorkQueue();
390 if (work_queue_.empty())
391 break;
392
393 // Execute oldest task.
394 do {
395 PendingTask pending_task = work_queue_.front();
396 work_queue_.pop();
397 if (!pending_task.delayed_run_time.is_null()) {
[email protected]001747c2008-09-10 00:37:07398 AddToDelayedWorkQueue(pending_task);
[email protected]72deacd2008-09-23 19:19:20399 // If we changed the topmost task, then it is time to re-schedule.
400 if (delayed_work_queue_.top().task == pending_task.task)
[email protected]752578562008-09-07 08:08:29401 pump_->ScheduleDelayedWork(pending_task.delayed_run_time);
402 } else {
403 if (DeferOrRunPendingTask(pending_task))
404 return true;
405 }
406 } while (!work_queue_.empty());
407 }
408
409 // Nothing happened.
410 return false;
[email protected]fc7fb6e2008-08-16 03:09:05411}
412
[email protected]b24250fc2008-08-20 06:30:58413bool MessageLoop::DoDelayedWork(Time* next_delayed_work_time) {
[email protected]752578562008-09-07 08:08:29414 if (!nestable_tasks_allowed_ || delayed_work_queue_.empty()) {
415 *next_delayed_work_time = Time();
416 return false;
417 }
418
419 if (delayed_work_queue_.top().delayed_run_time > Time::Now()) {
420 *next_delayed_work_time = delayed_work_queue_.top().delayed_run_time;
421 return false;
422 }
[email protected]fc7fb6e2008-08-16 03:09:05423
[email protected]752578562008-09-07 08:08:29424 PendingTask pending_task = delayed_work_queue_.top();
425 delayed_work_queue_.pop();
426
427 if (!delayed_work_queue_.empty())
428 *next_delayed_work_time = delayed_work_queue_.top().delayed_run_time;
[email protected]fc7fb6e2008-08-16 03:09:05429
[email protected]752578562008-09-07 08:08:29430 return DeferOrRunPendingTask(pending_task);
[email protected]fc7fb6e2008-08-16 03:09:05431}
432
433bool MessageLoop::DoIdleWork() {
434 if (ProcessNextDelayedNonNestableTask())
435 return true;
436
437 if (state_->quit_received)
438 pump_->Quit();
439
440 return false;
441}
442
443//------------------------------------------------------------------------------
444// MessageLoop::AutoRunState
445
446MessageLoop::AutoRunState::AutoRunState(MessageLoop* loop) : loop_(loop) {
447 // Make the loop reference us.
448 previous_state_ = loop_->state_;
449 if (previous_state_) {
450 run_depth = previous_state_->run_depth + 1;
[email protected]ea15e982008-08-15 07:31:20451 } else {
[email protected]fc7fb6e2008-08-16 03:09:05452 run_depth = 1;
[email protected]ea15e982008-08-15 07:31:20453 }
[email protected]fc7fb6e2008-08-16 03:09:05454 loop_->state_ = this;
455
456 // Initialize the other fields:
457 quit_received = false;
458#if defined(OS_WIN)
459 dispatcher = NULL;
460#endif
461}
462
463MessageLoop::AutoRunState::~AutoRunState() {
464 loop_->state_ = previous_state_;
[email protected]a5b94a92008-08-12 23:25:43465}
466
initial.commitd7cae122008-07-26 21:49:38467//------------------------------------------------------------------------------
[email protected]752578562008-09-07 08:08:29468// MessageLoop::PendingTask
initial.commitd7cae122008-07-26 21:49:38469
[email protected]752578562008-09-07 08:08:29470bool MessageLoop::PendingTask::operator<(const PendingTask& other) const {
471 // Since the top of a priority queue is defined as the "greatest" element, we
472 // need to invert the comparison here. We want the smaller time to be at the
473 // top of the heap.
initial.commitd7cae122008-07-26 21:49:38474
[email protected]752578562008-09-07 08:08:29475 if (delayed_run_time < other.delayed_run_time)
476 return false;
initial.commitd7cae122008-07-26 21:49:38477
[email protected]752578562008-09-07 08:08:29478 if (delayed_run_time > other.delayed_run_time)
479 return true;
initial.commitd7cae122008-07-26 21:49:38480
[email protected]752578562008-09-07 08:08:29481 // If the times happen to match, then we use the sequence number to decide.
482 // Compare the difference to support integer roll-over.
483 return (sequence_num - other.sequence_num) > 0;
initial.commitd7cae122008-07-26 21:49:38484}
485
486//------------------------------------------------------------------------------
487// Method and data for histogramming events and actions taken by each instance
488// on each thread.
489
490// static
491bool MessageLoop::enable_histogrammer_ = false;
492
493// static
494void MessageLoop::EnableHistogrammer(bool enable) {
495 enable_histogrammer_ = enable;
496}
497
498void MessageLoop::StartHistogrammer() {
499 if (enable_histogrammer_ && !message_histogram_.get()
500 && StatisticsRecorder::WasStarted()) {
[email protected]fc7fb6e2008-08-16 03:09:05501 DCHECK(!thread_name_.empty());
initial.commitd7cae122008-07-26 21:49:38502 message_histogram_.reset(new LinearHistogram(
503 ASCIIToWide("MsgLoop:" + thread_name_).c_str(),
504 kLeastNonZeroMessageId,
505 kMaxMessageId,
506 kNumberOfDistinctMessagesDisplayed));
507 message_histogram_->SetFlags(message_histogram_->kHexRangePrintingFlag);
508 message_histogram_->SetRangeDescriptions(event_descriptions_);
509 }
510}
511
512void MessageLoop::HistogramEvent(int event) {
513 if (message_histogram_.get())
514 message_histogram_->Add(event);
515}
516
initial.commitd7cae122008-07-26 21:49:38517// Provide a macro that takes an expression (such as a constant, or macro
518// constant) and creates a pair to initalize an array of pairs. In this case,
519// our pair consists of the expressions value, and the "stringized" version
520// of the expression (i.e., the exrpression put in quotes). For example, if
521// we have:
522// #define FOO 2
523// #define BAR 5
524// then the following:
525// VALUE_TO_NUMBER_AND_NAME(FOO + BAR)
526// will expand to:
527// {7, "FOO + BAR"}
528// We use the resulting array as an argument to our histogram, which reads the
529// number as a bucket identifier, and proceeds to use the corresponding name
530// in the pair (i.e., the quoted string) when printing out a histogram.
531#define VALUE_TO_NUMBER_AND_NAME(name) {name, #name},
532
initial.commitd7cae122008-07-26 21:49:38533// static
534const LinearHistogram::DescriptionPair MessageLoop::event_descriptions_[] = {
initial.commitd7cae122008-07-26 21:49:38535 // Provide some pretty print capability in our histogram for our internal
536 // messages.
537
initial.commitd7cae122008-07-26 21:49:38538 // A few events we handle (kindred to messages), and used to profile actions.
539 VALUE_TO_NUMBER_AND_NAME(kTaskRunEvent)
initial.commitd7cae122008-07-26 21:49:38540 VALUE_TO_NUMBER_AND_NAME(kTimerEvent)
541
542 {-1, NULL} // The list must be null terminated, per API to histogram.
543};
license.botbf09a502008-08-24 00:55:55544
[email protected]4d9bdfaf2008-08-26 05:53:57545//------------------------------------------------------------------------------
546// MessageLoopForUI
547
548#if defined(OS_WIN)
549
550void MessageLoopForUI::Run(Dispatcher* dispatcher) {
551 AutoRunState save_state(this);
552 state_->dispatcher = dispatcher;
553 RunHandler();
554}
555
556void MessageLoopForUI::AddObserver(Observer* observer) {
557 pump_win()->AddObserver(observer);
558}
559
560void MessageLoopForUI::RemoveObserver(Observer* observer) {
561 pump_win()->RemoveObserver(observer);
562}
563
564void MessageLoopForUI::WillProcessMessage(const MSG& message) {
565 pump_win()->WillProcessMessage(message);
566}
567void MessageLoopForUI::DidProcessMessage(const MSG& message) {
568 pump_win()->DidProcessMessage(message);
569}
570void MessageLoopForUI::PumpOutPendingPaintMessages() {
571 pump_win()->PumpOutPendingPaintMessages();
572}
573
574#endif // defined(OS_WIN)
575
576//------------------------------------------------------------------------------
577// MessageLoopForIO
578
579#if defined(OS_WIN)
580
581void MessageLoopForIO::WatchObject(HANDLE object, Watcher* watcher) {
[email protected]1a8f5d1d2008-09-25 20:33:04582 pump_io()->WatchObject(object, watcher);
[email protected]4d9bdfaf2008-08-26 05:53:57583}
584
[email protected]36987e92008-09-18 18:46:26585#elif defined(OS_POSIX)
586
587void MessageLoopForIO::WatchSocket(int socket, short interest_mask,
588 struct event* e, Watcher* watcher) {
589 pump_libevent()->WatchSocket(socket, interest_mask, e, watcher);
590}
591
592void MessageLoopForIO::UnwatchSocket(struct event* e) {
593 pump_libevent()->UnwatchSocket(e);
594}
595#endif