blob: 4b955320010dcb53210b73af3ea5236ed61f1701 [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]f886b7bf2008-09-10 10:54:0616// A lazily created thread local storage for quick access to a thread's message
17// loop, if one exists. This should be safe and free of static constructors.
18static base::LazyInstance<base::ThreadLocalPointer<MessageLoop> > lazy_tls_ptr(
19 base::LINKER_INITIALIZED);
initial.commitd7cae122008-07-26 21:49:3820
21//------------------------------------------------------------------------------
22
initial.commitd7cae122008-07-26 21:49:3823// Logical events for Histogram profiling. Run with -message-loop-histogrammer
24// to get an accounting of messages and actions taken on each thread.
[email protected]fc7fb6e2008-08-16 03:09:0525static const int kTaskRunEvent = 0x1;
26static const int kTimerEvent = 0x2;
initial.commitd7cae122008-07-26 21:49:3827
28// Provide range of message IDs for use in histogramming and debug display.
29static const int kLeastNonZeroMessageId = 1;
30static const int kMaxMessageId = 1099;
31static const int kNumberOfDistinctMessagesDisplayed = 1100;
32
33//------------------------------------------------------------------------------
34
[email protected]fc7fb6e2008-08-16 03:09:0535#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:3836
initial.commitd7cae122008-07-26 21:49:3837// Upon a SEH exception in this thread, it restores the original unhandled
38// exception filter.
39static int SEHFilter(LPTOP_LEVEL_EXCEPTION_FILTER old_filter) {
40 ::SetUnhandledExceptionFilter(old_filter);
41 return EXCEPTION_CONTINUE_SEARCH;
42}
43
44// Retrieves a pointer to the current unhandled exception filter. There
45// is no standalone getter method.
46static LPTOP_LEVEL_EXCEPTION_FILTER GetTopSEHFilter() {
47 LPTOP_LEVEL_EXCEPTION_FILTER top_filter = NULL;
48 top_filter = ::SetUnhandledExceptionFilter(0);
49 ::SetUnhandledExceptionFilter(top_filter);
50 return top_filter;
51}
52
[email protected]fc7fb6e2008-08-16 03:09:0553#endif // defined(OS_WIN)
54
initial.commitd7cae122008-07-26 21:49:3855//------------------------------------------------------------------------------
56
[email protected]f886b7bf2008-09-10 10:54:0657// static
58MessageLoop* MessageLoop::current() {
59 // TODO(darin): sadly, we cannot enable this yet since people call us even
60 // when they have no intention of using us.
61 //DCHECK(loop) << "Ouch, did you forget to initialize me?";
62 return lazy_tls_ptr.Pointer()->Get();
63}
64
[email protected]4d9bdfaf2008-08-26 05:53:5765MessageLoop::MessageLoop(Type type)
66 : type_(type),
[email protected]a5b94a92008-08-12 23:25:4367 nestable_tasks_allowed_(true),
[email protected]b16ef312008-08-19 18:36:2368 exception_restoration_(false),
[email protected]752578562008-09-07 08:08:2969 state_(NULL),
70 next_sequence_num_(0) {
[email protected]f886b7bf2008-09-10 10:54:0671 DCHECK(!current()) << "should only have one message loop per thread";
72 lazy_tls_ptr.Pointer()->Set(this);
[email protected]4d9bdfaf2008-08-26 05:53:5773
[email protected]ef8963172008-08-26 06:52:4174 // TODO(darin): Choose the pump based on the requested type.
[email protected]fc7fb6e2008-08-16 03:09:0575#if defined(OS_WIN)
[email protected]9bcbf472008-08-30 00:22:4876 if (type_ == TYPE_DEFAULT) {
77 pump_ = new base::MessagePumpDefault();
78 } else {
79 pump_ = new base::MessagePumpWin();
80 }
[email protected]b16ef312008-08-19 18:36:2381#else
[email protected]ef8963172008-08-26 06:52:4182 pump_ = new base::MessagePumpDefault();
[email protected]fc7fb6e2008-08-16 03:09:0583#endif
initial.commitd7cae122008-07-26 21:49:3884}
85
86MessageLoop::~MessageLoop() {
87 DCHECK(this == current());
[email protected]2a127252008-08-05 23:16:4188
89 // Let interested parties have one last shot at accessing this.
90 FOR_EACH_OBSERVER(DestructionObserver, destruction_observers_,
91 WillDestroyCurrentMessageLoop());
92
[email protected]08de3cde2008-09-09 05:55:3593 DCHECK(!state_);
94
[email protected]001747c2008-09-10 00:37:0795 // Clean up any unprocessed tasks, but take care: deleting a task could
96 // result in the addition of more tasks (e.g., via DeleteSoon). We set a
97 // limit on the number of times we will allow a deleted task to generate more
98 // tasks. Normally, we should only pass through this loop once or twice. If
99 // we end up hitting the loop limit, then it is probably due to one task that
100 // is being stubborn. Inspect the queues to see who is left.
101 bool did_work;
102 for (int i = 0; i < 100; ++i) {
103 DeletePendingTasks();
104 ReloadWorkQueue();
105 // If we end up with empty queues, then break out of the loop.
106 did_work = DeletePendingTasks();
107 if (!did_work)
108 break;
[email protected]08de3cde2008-09-09 05:55:35109 }
[email protected]001747c2008-09-10 00:37:07110 DCHECK(!did_work);
111
112 // OK, now make it so that no one can find us.
[email protected]2b89d22b22008-09-10 11:14:56113 lazy_tls_ptr.Pointer()->Set(NULL);
initial.commitd7cae122008-07-26 21:49:38114}
115
[email protected]2a127252008-08-05 23:16:41116void MessageLoop::AddDestructionObserver(DestructionObserver *obs) {
117 DCHECK(this == current());
118 destruction_observers_.AddObserver(obs);
119}
120
121void MessageLoop::RemoveDestructionObserver(DestructionObserver *obs) {
122 DCHECK(this == current());
123 destruction_observers_.RemoveObserver(obs);
124}
125
[email protected]ea15e982008-08-15 07:31:20126void MessageLoop::Run() {
[email protected]fc7fb6e2008-08-16 03:09:05127 AutoRunState save_state(this);
128 RunHandler();
[email protected]ea15e982008-08-15 07:31:20129}
130
[email protected]7e0e8762008-07-31 13:10:20131void MessageLoop::RunAllPending() {
[email protected]fc7fb6e2008-08-16 03:09:05132 AutoRunState save_state(this);
133 state_->quit_received = true; // Means run until we would otherwise block.
134 RunHandler();
initial.commitd7cae122008-07-26 21:49:38135}
136
137// Runs the loop in two different SEH modes:
138// enable_SEH_restoration_ = false : any unhandled exception goes to the last
139// one that calls SetUnhandledExceptionFilter().
140// enable_SEH_restoration_ = true : any unhandled exception goes to the filter
141// that was existed before the loop was run.
[email protected]fc7fb6e2008-08-16 03:09:05142void MessageLoop::RunHandler() {
143#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:38144 if (exception_restoration_) {
145 LPTOP_LEVEL_EXCEPTION_FILTER current_filter = GetTopSEHFilter();
146 __try {
[email protected]fc7fb6e2008-08-16 03:09:05147 RunInternal();
initial.commitd7cae122008-07-26 21:49:38148 } __except(SEHFilter(current_filter)) {
149 }
[email protected]fc7fb6e2008-08-16 03:09:05150 return;
initial.commitd7cae122008-07-26 21:49:38151 }
[email protected]fc7fb6e2008-08-16 03:09:05152#endif
153
154 RunInternal();
initial.commitd7cae122008-07-26 21:49:38155}
156
157//------------------------------------------------------------------------------
initial.commitd7cae122008-07-26 21:49:38158
[email protected]fc7fb6e2008-08-16 03:09:05159void MessageLoop::RunInternal() {
160 DCHECK(this == current());
161
initial.commitd7cae122008-07-26 21:49:38162 StartHistogrammer();
163
[email protected]fc7fb6e2008-08-16 03:09:05164#if defined(OS_WIN)
165 if (state_->dispatcher) {
166 pump_win()->RunWithDispatcher(this, state_->dispatcher);
167 return;
[email protected]7e0e8762008-07-31 13:10:20168 }
[email protected]fc7fb6e2008-08-16 03:09:05169#endif
170
171 pump_->Run(this);
[email protected]b8f2fe5d2008-07-30 07:50:53172}
[email protected]7622bd02008-07-30 06:58:56173
[email protected]3882c4332008-07-30 19:03:59174//------------------------------------------------------------------------------
175// Wrapper functions for use in above message loop framework.
176
initial.commitd7cae122008-07-26 21:49:38177bool MessageLoop::ProcessNextDelayedNonNestableTask() {
[email protected]fc7fb6e2008-08-16 03:09:05178 if (state_->run_depth != 1)
initial.commitd7cae122008-07-26 21:49:38179 return false;
180
[email protected]752578562008-09-07 08:08:29181 if (deferred_non_nestable_work_queue_.empty())
initial.commitd7cae122008-07-26 21:49:38182 return false;
[email protected]752578562008-09-07 08:08:29183
184 Task* task = deferred_non_nestable_work_queue_.front().task;
185 deferred_non_nestable_work_queue_.pop();
186
187 RunTask(task);
initial.commitd7cae122008-07-26 21:49:38188 return true;
189}
190
initial.commitd7cae122008-07-26 21:49:38191//------------------------------------------------------------------------------
192
193void MessageLoop::Quit() {
[email protected]fc7fb6e2008-08-16 03:09:05194 DCHECK(current() == this);
195 if (state_) {
196 state_->quit_received = true;
197 } else {
198 NOTREACHED() << "Must be inside Run to call Quit";
initial.commitd7cae122008-07-26 21:49:38199 }
initial.commitd7cae122008-07-26 21:49:38200}
201
[email protected]752578562008-09-07 08:08:29202void MessageLoop::PostTask(
203 const tracked_objects::Location& from_here, Task* task) {
204 PostTask_Helper(from_here, task, 0, true);
205}
206
207void MessageLoop::PostDelayedTask(
208 const tracked_objects::Location& from_here, Task* task, int delay_ms) {
209 PostTask_Helper(from_here, task, delay_ms, true);
210}
211
212void MessageLoop::PostNonNestableTask(
213 const tracked_objects::Location& from_here, Task* task) {
214 PostTask_Helper(from_here, task, 0, false);
215}
216
217void MessageLoop::PostNonNestableDelayedTask(
218 const tracked_objects::Location& from_here, Task* task, int delay_ms) {
219 PostTask_Helper(from_here, task, delay_ms, false);
220}
221
initial.commitd7cae122008-07-26 21:49:38222// Possibly called on a background thread!
[email protected]752578562008-09-07 08:08:29223void MessageLoop::PostTask_Helper(
224 const tracked_objects::Location& from_here, Task* task, int delay_ms,
225 bool nestable) {
initial.commitd7cae122008-07-26 21:49:38226 task->SetBirthPlace(from_here);
[email protected]9bcbf472008-08-30 00:22:48227
[email protected]752578562008-09-07 08:08:29228 PendingTask pending_task(task, nestable);
[email protected]9bcbf472008-08-30 00:22:48229
230 if (delay_ms > 0) {
[email protected]752578562008-09-07 08:08:29231 pending_task.delayed_run_time =
[email protected]9bcbf472008-08-30 00:22:48232 Time::Now() + TimeDelta::FromMilliseconds(delay_ms);
233 } else {
234 DCHECK(delay_ms == 0) << "delay should not be negative";
235 }
236
initial.commitd7cae122008-07-26 21:49:38237 // Warning: Don't try to short-circuit, and handle this thread's tasks more
238 // directly, as it could starve handling of foreign threads. Put every task
239 // into this queue.
240
[email protected]fc7fb6e2008-08-16 03:09:05241 scoped_refptr<base::MessagePump> pump;
initial.commitd7cae122008-07-26 21:49:38242 {
[email protected]fc7fb6e2008-08-16 03:09:05243 AutoLock locked(incoming_queue_lock_);
244
[email protected]752578562008-09-07 08:08:29245 bool was_empty = incoming_queue_.empty();
246 incoming_queue_.push(pending_task);
initial.commitd7cae122008-07-26 21:49:38247 if (!was_empty)
248 return; // Someone else should have started the sub-pump.
249
[email protected]fc7fb6e2008-08-16 03:09:05250 pump = pump_;
[email protected]ea15e982008-08-15 07:31:20251 }
[email protected]fc7fb6e2008-08-16 03:09:05252 // Since the incoming_queue_ may contain a task that destroys this message
253 // loop, we cannot exit incoming_queue_lock_ until we are done with |this|.
254 // We use a stack-based reference to the message pump so that we can call
255 // ScheduleWork outside of incoming_queue_lock_.
[email protected]ea15e982008-08-15 07:31:20256
[email protected]fc7fb6e2008-08-16 03:09:05257 pump->ScheduleWork();
initial.commitd7cae122008-07-26 21:49:38258}
259
260void MessageLoop::SetNestableTasksAllowed(bool allowed) {
[email protected]124a2bdf2008-08-09 00:14:09261 if (nestable_tasks_allowed_ != allowed) {
262 nestable_tasks_allowed_ = allowed;
263 if (!nestable_tasks_allowed_)
264 return;
265 // Start the native pump if we are not already pumping.
[email protected]fc7fb6e2008-08-16 03:09:05266 pump_->ScheduleWork();
[email protected]124a2bdf2008-08-09 00:14:09267 }
initial.commitd7cae122008-07-26 21:49:38268}
269
270bool MessageLoop::NestableTasksAllowed() const {
271 return nestable_tasks_allowed_;
272}
273
initial.commitd7cae122008-07-26 21:49:38274//------------------------------------------------------------------------------
initial.commitd7cae122008-07-26 21:49:38275
initial.commitd7cae122008-07-26 21:49:38276void MessageLoop::RunTask(Task* task) {
initial.commitd7cae122008-07-26 21:49:38277 DCHECK(nestable_tasks_allowed_);
278 // Execute the task and assume the worst: It is probably not reentrant.
279 nestable_tasks_allowed_ = false;
[email protected]752578562008-09-07 08:08:29280
281 HistogramEvent(kTaskRunEvent);
282 task->Run();
283 delete task;
284
285 nestable_tasks_allowed_ = true;
initial.commitd7cae122008-07-26 21:49:38286}
287
[email protected]752578562008-09-07 08:08:29288bool MessageLoop::DeferOrRunPendingTask(const PendingTask& pending_task) {
289 if (pending_task.nestable || state_->run_depth == 1) {
290 RunTask(pending_task.task);
291 // Show that we ran a task (Note: a new one might arrive as a
292 // consequence!).
293 return true;
294 }
295
296 // We couldn't run the task now because we're in a nested message loop
297 // and the task isn't nestable.
298 deferred_non_nestable_work_queue_.push(pending_task);
299 return false;
initial.commitd7cae122008-07-26 21:49:38300}
301
[email protected]001747c2008-09-10 00:37:07302void MessageLoop::AddToDelayedWorkQueue(const PendingTask& pending_task) {
303 // Move to the delayed work queue. Initialize the sequence number
304 // before inserting into the delayed_work_queue_. The sequence number
305 // is used to faciliate FIFO sorting when two tasks have the same
306 // delayed_run_time value.
307 PendingTask new_pending_task(pending_task);
308 new_pending_task.sequence_num = next_sequence_num_++;
309 delayed_work_queue_.push(new_pending_task);
310}
311
initial.commitd7cae122008-07-26 21:49:38312void MessageLoop::ReloadWorkQueue() {
313 // We can improve performance of our loading tasks from incoming_queue_ to
[email protected]fc7fb6e2008-08-16 03:09:05314 // work_queue_ by waiting until the last minute (work_queue_ is empty) to
315 // load. That reduces the number of locks-per-task significantly when our
[email protected]752578562008-09-07 08:08:29316 // queues get large.
317 if (!work_queue_.empty())
initial.commitd7cae122008-07-26 21:49:38318 return; // Wait till we *really* need to lock and load.
319
320 // Acquire all we can from the inter-thread queue with one lock acquisition.
initial.commitd7cae122008-07-26 21:49:38321 {
322 AutoLock lock(incoming_queue_lock_);
[email protected]752578562008-09-07 08:08:29323 if (incoming_queue_.empty())
initial.commitd7cae122008-07-26 21:49:38324 return;
[email protected]752578562008-09-07 08:08:29325 std::swap(incoming_queue_, work_queue_);
326 DCHECK(incoming_queue_.empty());
initial.commitd7cae122008-07-26 21:49:38327 }
328}
329
[email protected]001747c2008-09-10 00:37:07330bool MessageLoop::DeletePendingTasks() {
331 bool did_work = !work_queue_.empty();
332 while (!work_queue_.empty()) {
333 PendingTask pending_task = work_queue_.front();
334 work_queue_.pop();
335 if (!pending_task.delayed_run_time.is_null()) {
336 // We want to delete delayed tasks in the same order in which they would
337 // normally be deleted in case of any funny dependencies between delayed
338 // tasks.
339 AddToDelayedWorkQueue(pending_task);
340 } else {
341 // TODO(darin): Delete all tasks once it is safe to do so.
342 //delete task;
343 }
initial.commitd7cae122008-07-26 21:49:38344 }
[email protected]001747c2008-09-10 00:37:07345 did_work |= !deferred_non_nestable_work_queue_.empty();
346 while (!deferred_non_nestable_work_queue_.empty()) {
[email protected]001747c2008-09-10 00:37:07347 // TODO(darin): Delete all tasks once it is safe to do so.
[email protected]058bccb2008-09-10 01:16:17348 //Task* task = deferred_non_nestable_work_queue_.front().task;
349 deferred_non_nestable_work_queue_.pop();
[email protected]001747c2008-09-10 00:37:07350 //delete task;
initial.commitd7cae122008-07-26 21:49:38351 }
[email protected]001747c2008-09-10 00:37:07352 did_work |= !delayed_work_queue_.empty();
353 while (!delayed_work_queue_.empty()) {
354 Task* task = delayed_work_queue_.top().task;
355 delayed_work_queue_.pop();
356 delete task;
357 }
358 return did_work;
initial.commitd7cae122008-07-26 21:49:38359}
360
[email protected]fc7fb6e2008-08-16 03:09:05361bool MessageLoop::DoWork() {
[email protected]752578562008-09-07 08:08:29362 if (!nestable_tasks_allowed_) {
363 // Task can't be executed right now.
364 return false;
365 }
366
367 for (;;) {
368 ReloadWorkQueue();
369 if (work_queue_.empty())
370 break;
371
372 // Execute oldest task.
373 do {
374 PendingTask pending_task = work_queue_.front();
375 work_queue_.pop();
376 if (!pending_task.delayed_run_time.is_null()) {
377 bool was_empty = delayed_work_queue_.empty();
[email protected]001747c2008-09-10 00:37:07378 AddToDelayedWorkQueue(pending_task);
[email protected]752578562008-09-07 08:08:29379 if (was_empty) // We only schedule the next delayed work item.
380 pump_->ScheduleDelayedWork(pending_task.delayed_run_time);
381 } else {
382 if (DeferOrRunPendingTask(pending_task))
383 return true;
384 }
385 } while (!work_queue_.empty());
386 }
387
388 // Nothing happened.
389 return false;
[email protected]fc7fb6e2008-08-16 03:09:05390}
391
[email protected]b24250fc2008-08-20 06:30:58392bool MessageLoop::DoDelayedWork(Time* next_delayed_work_time) {
[email protected]752578562008-09-07 08:08:29393 if (!nestable_tasks_allowed_ || delayed_work_queue_.empty()) {
394 *next_delayed_work_time = Time();
395 return false;
396 }
397
398 if (delayed_work_queue_.top().delayed_run_time > Time::Now()) {
399 *next_delayed_work_time = delayed_work_queue_.top().delayed_run_time;
400 return false;
401 }
[email protected]fc7fb6e2008-08-16 03:09:05402
[email protected]752578562008-09-07 08:08:29403 PendingTask pending_task = delayed_work_queue_.top();
404 delayed_work_queue_.pop();
405
406 if (!delayed_work_queue_.empty())
407 *next_delayed_work_time = delayed_work_queue_.top().delayed_run_time;
[email protected]fc7fb6e2008-08-16 03:09:05408
[email protected]752578562008-09-07 08:08:29409 return DeferOrRunPendingTask(pending_task);
[email protected]fc7fb6e2008-08-16 03:09:05410}
411
412bool MessageLoop::DoIdleWork() {
413 if (ProcessNextDelayedNonNestableTask())
414 return true;
415
416 if (state_->quit_received)
417 pump_->Quit();
418
419 return false;
420}
421
422//------------------------------------------------------------------------------
423// MessageLoop::AutoRunState
424
425MessageLoop::AutoRunState::AutoRunState(MessageLoop* loop) : loop_(loop) {
426 // Make the loop reference us.
427 previous_state_ = loop_->state_;
428 if (previous_state_) {
429 run_depth = previous_state_->run_depth + 1;
[email protected]ea15e982008-08-15 07:31:20430 } else {
[email protected]fc7fb6e2008-08-16 03:09:05431 run_depth = 1;
[email protected]ea15e982008-08-15 07:31:20432 }
[email protected]fc7fb6e2008-08-16 03:09:05433 loop_->state_ = this;
434
435 // Initialize the other fields:
436 quit_received = false;
437#if defined(OS_WIN)
438 dispatcher = NULL;
439#endif
440}
441
442MessageLoop::AutoRunState::~AutoRunState() {
443 loop_->state_ = previous_state_;
[email protected]a5b94a92008-08-12 23:25:43444}
445
initial.commitd7cae122008-07-26 21:49:38446//------------------------------------------------------------------------------
[email protected]752578562008-09-07 08:08:29447// MessageLoop::PendingTask
initial.commitd7cae122008-07-26 21:49:38448
[email protected]752578562008-09-07 08:08:29449bool MessageLoop::PendingTask::operator<(const PendingTask& other) const {
450 // Since the top of a priority queue is defined as the "greatest" element, we
451 // need to invert the comparison here. We want the smaller time to be at the
452 // top of the heap.
initial.commitd7cae122008-07-26 21:49:38453
[email protected]752578562008-09-07 08:08:29454 if (delayed_run_time < other.delayed_run_time)
455 return false;
initial.commitd7cae122008-07-26 21:49:38456
[email protected]752578562008-09-07 08:08:29457 if (delayed_run_time > other.delayed_run_time)
458 return true;
initial.commitd7cae122008-07-26 21:49:38459
[email protected]752578562008-09-07 08:08:29460 // If the times happen to match, then we use the sequence number to decide.
461 // Compare the difference to support integer roll-over.
462 return (sequence_num - other.sequence_num) > 0;
initial.commitd7cae122008-07-26 21:49:38463}
464
465//------------------------------------------------------------------------------
466// Method and data for histogramming events and actions taken by each instance
467// on each thread.
468
469// static
470bool MessageLoop::enable_histogrammer_ = false;
471
472// static
473void MessageLoop::EnableHistogrammer(bool enable) {
474 enable_histogrammer_ = enable;
475}
476
477void MessageLoop::StartHistogrammer() {
478 if (enable_histogrammer_ && !message_histogram_.get()
479 && StatisticsRecorder::WasStarted()) {
[email protected]fc7fb6e2008-08-16 03:09:05480 DCHECK(!thread_name_.empty());
initial.commitd7cae122008-07-26 21:49:38481 message_histogram_.reset(new LinearHistogram(
482 ASCIIToWide("MsgLoop:" + thread_name_).c_str(),
483 kLeastNonZeroMessageId,
484 kMaxMessageId,
485 kNumberOfDistinctMessagesDisplayed));
486 message_histogram_->SetFlags(message_histogram_->kHexRangePrintingFlag);
487 message_histogram_->SetRangeDescriptions(event_descriptions_);
488 }
489}
490
491void MessageLoop::HistogramEvent(int event) {
492 if (message_histogram_.get())
493 message_histogram_->Add(event);
494}
495
initial.commitd7cae122008-07-26 21:49:38496// Provide a macro that takes an expression (such as a constant, or macro
497// constant) and creates a pair to initalize an array of pairs. In this case,
498// our pair consists of the expressions value, and the "stringized" version
499// of the expression (i.e., the exrpression put in quotes). For example, if
500// we have:
501// #define FOO 2
502// #define BAR 5
503// then the following:
504// VALUE_TO_NUMBER_AND_NAME(FOO + BAR)
505// will expand to:
506// {7, "FOO + BAR"}
507// We use the resulting array as an argument to our histogram, which reads the
508// number as a bucket identifier, and proceeds to use the corresponding name
509// in the pair (i.e., the quoted string) when printing out a histogram.
510#define VALUE_TO_NUMBER_AND_NAME(name) {name, #name},
511
initial.commitd7cae122008-07-26 21:49:38512// static
513const LinearHistogram::DescriptionPair MessageLoop::event_descriptions_[] = {
initial.commitd7cae122008-07-26 21:49:38514 // Provide some pretty print capability in our histogram for our internal
515 // messages.
516
initial.commitd7cae122008-07-26 21:49:38517 // A few events we handle (kindred to messages), and used to profile actions.
518 VALUE_TO_NUMBER_AND_NAME(kTaskRunEvent)
initial.commitd7cae122008-07-26 21:49:38519 VALUE_TO_NUMBER_AND_NAME(kTimerEvent)
520
521 {-1, NULL} // The list must be null terminated, per API to histogram.
522};
license.botbf09a502008-08-24 00:55:55523
[email protected]4d9bdfaf2008-08-26 05:53:57524//------------------------------------------------------------------------------
525// MessageLoopForUI
526
527#if defined(OS_WIN)
528
529void MessageLoopForUI::Run(Dispatcher* dispatcher) {
530 AutoRunState save_state(this);
531 state_->dispatcher = dispatcher;
532 RunHandler();
533}
534
535void MessageLoopForUI::AddObserver(Observer* observer) {
536 pump_win()->AddObserver(observer);
537}
538
539void MessageLoopForUI::RemoveObserver(Observer* observer) {
540 pump_win()->RemoveObserver(observer);
541}
542
543void MessageLoopForUI::WillProcessMessage(const MSG& message) {
544 pump_win()->WillProcessMessage(message);
545}
546void MessageLoopForUI::DidProcessMessage(const MSG& message) {
547 pump_win()->DidProcessMessage(message);
548}
549void MessageLoopForUI::PumpOutPendingPaintMessages() {
550 pump_win()->PumpOutPendingPaintMessages();
551}
552
553#endif // defined(OS_WIN)
554
555//------------------------------------------------------------------------------
556// MessageLoopForIO
557
558#if defined(OS_WIN)
559
560void MessageLoopForIO::WatchObject(HANDLE object, Watcher* watcher) {
561 pump_win()->WatchObject(object, watcher);
562}
563
[email protected]35aa85a2008-09-18 00:23:24564#endif // defined(OS_WIN)