blob: 3abf0c8f05f855e731b40b0cd44cd08fe54c9fba [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]f886b7bf2008-09-10 10:54:0693 // OK, now make it so that no one can find us.
94 lazy_tls_ptr.Pointer()->Set(NULL);
95
[email protected]08de3cde2008-09-09 05:55:3596 DCHECK(!state_);
97
[email protected]001747c2008-09-10 00:37:0798 // Clean up any unprocessed tasks, but take care: deleting a task could
99 // result in the addition of more tasks (e.g., via DeleteSoon). We set a
100 // limit on the number of times we will allow a deleted task to generate more
101 // tasks. Normally, we should only pass through this loop once or twice. If
102 // we end up hitting the loop limit, then it is probably due to one task that
103 // is being stubborn. Inspect the queues to see who is left.
104 bool did_work;
105 for (int i = 0; i < 100; ++i) {
106 DeletePendingTasks();
107 ReloadWorkQueue();
108 // If we end up with empty queues, then break out of the loop.
109 did_work = DeletePendingTasks();
110 if (!did_work)
111 break;
[email protected]08de3cde2008-09-09 05:55:35112 }
[email protected]001747c2008-09-10 00:37:07113 DCHECK(!did_work);
114
115 // OK, now make it so that no one can find us.
116 tls_index_.Set(NULL);
initial.commitd7cae122008-07-26 21:49:38117}
118
[email protected]2a127252008-08-05 23:16:41119void MessageLoop::AddDestructionObserver(DestructionObserver *obs) {
120 DCHECK(this == current());
121 destruction_observers_.AddObserver(obs);
122}
123
124void MessageLoop::RemoveDestructionObserver(DestructionObserver *obs) {
125 DCHECK(this == current());
126 destruction_observers_.RemoveObserver(obs);
127}
128
[email protected]ea15e982008-08-15 07:31:20129void MessageLoop::Run() {
[email protected]fc7fb6e2008-08-16 03:09:05130 AutoRunState save_state(this);
131 RunHandler();
[email protected]ea15e982008-08-15 07:31:20132}
133
[email protected]7e0e8762008-07-31 13:10:20134void MessageLoop::RunAllPending() {
[email protected]fc7fb6e2008-08-16 03:09:05135 AutoRunState save_state(this);
136 state_->quit_received = true; // Means run until we would otherwise block.
137 RunHandler();
initial.commitd7cae122008-07-26 21:49:38138}
139
140// Runs the loop in two different SEH modes:
141// enable_SEH_restoration_ = false : any unhandled exception goes to the last
142// one that calls SetUnhandledExceptionFilter().
143// enable_SEH_restoration_ = true : any unhandled exception goes to the filter
144// that was existed before the loop was run.
[email protected]fc7fb6e2008-08-16 03:09:05145void MessageLoop::RunHandler() {
146#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:38147 if (exception_restoration_) {
148 LPTOP_LEVEL_EXCEPTION_FILTER current_filter = GetTopSEHFilter();
149 __try {
[email protected]fc7fb6e2008-08-16 03:09:05150 RunInternal();
initial.commitd7cae122008-07-26 21:49:38151 } __except(SEHFilter(current_filter)) {
152 }
[email protected]fc7fb6e2008-08-16 03:09:05153 return;
initial.commitd7cae122008-07-26 21:49:38154 }
[email protected]fc7fb6e2008-08-16 03:09:05155#endif
156
157 RunInternal();
initial.commitd7cae122008-07-26 21:49:38158}
159
160//------------------------------------------------------------------------------
initial.commitd7cae122008-07-26 21:49:38161
[email protected]fc7fb6e2008-08-16 03:09:05162void MessageLoop::RunInternal() {
163 DCHECK(this == current());
164
initial.commitd7cae122008-07-26 21:49:38165 StartHistogrammer();
166
[email protected]fc7fb6e2008-08-16 03:09:05167#if defined(OS_WIN)
168 if (state_->dispatcher) {
169 pump_win()->RunWithDispatcher(this, state_->dispatcher);
170 return;
[email protected]7e0e8762008-07-31 13:10:20171 }
[email protected]fc7fb6e2008-08-16 03:09:05172#endif
173
174 pump_->Run(this);
[email protected]b8f2fe5d2008-07-30 07:50:53175}
[email protected]7622bd02008-07-30 06:58:56176
[email protected]3882c4332008-07-30 19:03:59177//------------------------------------------------------------------------------
178// Wrapper functions for use in above message loop framework.
179
initial.commitd7cae122008-07-26 21:49:38180bool MessageLoop::ProcessNextDelayedNonNestableTask() {
[email protected]fc7fb6e2008-08-16 03:09:05181 if (state_->run_depth != 1)
initial.commitd7cae122008-07-26 21:49:38182 return false;
183
[email protected]752578562008-09-07 08:08:29184 if (deferred_non_nestable_work_queue_.empty())
initial.commitd7cae122008-07-26 21:49:38185 return false;
[email protected]752578562008-09-07 08:08:29186
187 Task* task = deferred_non_nestable_work_queue_.front().task;
188 deferred_non_nestable_work_queue_.pop();
189
190 RunTask(task);
initial.commitd7cae122008-07-26 21:49:38191 return true;
192}
193
initial.commitd7cae122008-07-26 21:49:38194//------------------------------------------------------------------------------
195
196void MessageLoop::Quit() {
[email protected]fc7fb6e2008-08-16 03:09:05197 DCHECK(current() == this);
198 if (state_) {
199 state_->quit_received = true;
200 } else {
201 NOTREACHED() << "Must be inside Run to call Quit";
initial.commitd7cae122008-07-26 21:49:38202 }
initial.commitd7cae122008-07-26 21:49:38203}
204
[email protected]752578562008-09-07 08:08:29205void MessageLoop::PostTask(
206 const tracked_objects::Location& from_here, Task* task) {
207 PostTask_Helper(from_here, task, 0, true);
208}
209
210void MessageLoop::PostDelayedTask(
211 const tracked_objects::Location& from_here, Task* task, int delay_ms) {
212 PostTask_Helper(from_here, task, delay_ms, true);
213}
214
215void MessageLoop::PostNonNestableTask(
216 const tracked_objects::Location& from_here, Task* task) {
217 PostTask_Helper(from_here, task, 0, false);
218}
219
220void MessageLoop::PostNonNestableDelayedTask(
221 const tracked_objects::Location& from_here, Task* task, int delay_ms) {
222 PostTask_Helper(from_here, task, delay_ms, false);
223}
224
initial.commitd7cae122008-07-26 21:49:38225// Possibly called on a background thread!
[email protected]752578562008-09-07 08:08:29226void MessageLoop::PostTask_Helper(
227 const tracked_objects::Location& from_here, Task* task, int delay_ms,
228 bool nestable) {
initial.commitd7cae122008-07-26 21:49:38229 task->SetBirthPlace(from_here);
[email protected]9bcbf472008-08-30 00:22:48230
[email protected]752578562008-09-07 08:08:29231 PendingTask pending_task(task, nestable);
[email protected]9bcbf472008-08-30 00:22:48232
233 if (delay_ms > 0) {
[email protected]752578562008-09-07 08:08:29234 pending_task.delayed_run_time =
[email protected]9bcbf472008-08-30 00:22:48235 Time::Now() + TimeDelta::FromMilliseconds(delay_ms);
236 } else {
237 DCHECK(delay_ms == 0) << "delay should not be negative";
238 }
239
initial.commitd7cae122008-07-26 21:49:38240 // Warning: Don't try to short-circuit, and handle this thread's tasks more
241 // directly, as it could starve handling of foreign threads. Put every task
242 // into this queue.
243
[email protected]fc7fb6e2008-08-16 03:09:05244 scoped_refptr<base::MessagePump> pump;
initial.commitd7cae122008-07-26 21:49:38245 {
[email protected]fc7fb6e2008-08-16 03:09:05246 AutoLock locked(incoming_queue_lock_);
247
[email protected]752578562008-09-07 08:08:29248 bool was_empty = incoming_queue_.empty();
249 incoming_queue_.push(pending_task);
initial.commitd7cae122008-07-26 21:49:38250 if (!was_empty)
251 return; // Someone else should have started the sub-pump.
252
[email protected]fc7fb6e2008-08-16 03:09:05253 pump = pump_;
[email protected]ea15e982008-08-15 07:31:20254 }
[email protected]fc7fb6e2008-08-16 03:09:05255 // Since the incoming_queue_ may contain a task that destroys this message
256 // loop, we cannot exit incoming_queue_lock_ until we are done with |this|.
257 // We use a stack-based reference to the message pump so that we can call
258 // ScheduleWork outside of incoming_queue_lock_.
[email protected]ea15e982008-08-15 07:31:20259
[email protected]fc7fb6e2008-08-16 03:09:05260 pump->ScheduleWork();
initial.commitd7cae122008-07-26 21:49:38261}
262
263void MessageLoop::SetNestableTasksAllowed(bool allowed) {
[email protected]124a2bdf2008-08-09 00:14:09264 if (nestable_tasks_allowed_ != allowed) {
265 nestable_tasks_allowed_ = allowed;
266 if (!nestable_tasks_allowed_)
267 return;
268 // Start the native pump if we are not already pumping.
[email protected]fc7fb6e2008-08-16 03:09:05269 pump_->ScheduleWork();
[email protected]124a2bdf2008-08-09 00:14:09270 }
initial.commitd7cae122008-07-26 21:49:38271}
272
273bool MessageLoop::NestableTasksAllowed() const {
274 return nestable_tasks_allowed_;
275}
276
initial.commitd7cae122008-07-26 21:49:38277//------------------------------------------------------------------------------
initial.commitd7cae122008-07-26 21:49:38278
initial.commitd7cae122008-07-26 21:49:38279void MessageLoop::RunTask(Task* task) {
initial.commitd7cae122008-07-26 21:49:38280 DCHECK(nestable_tasks_allowed_);
281 // Execute the task and assume the worst: It is probably not reentrant.
282 nestable_tasks_allowed_ = false;
[email protected]752578562008-09-07 08:08:29283
284 HistogramEvent(kTaskRunEvent);
285 task->Run();
286 delete task;
287
288 nestable_tasks_allowed_ = true;
initial.commitd7cae122008-07-26 21:49:38289}
290
[email protected]752578562008-09-07 08:08:29291bool MessageLoop::DeferOrRunPendingTask(const PendingTask& pending_task) {
292 if (pending_task.nestable || state_->run_depth == 1) {
293 RunTask(pending_task.task);
294 // Show that we ran a task (Note: a new one might arrive as a
295 // consequence!).
296 return true;
297 }
298
299 // We couldn't run the task now because we're in a nested message loop
300 // and the task isn't nestable.
301 deferred_non_nestable_work_queue_.push(pending_task);
302 return false;
initial.commitd7cae122008-07-26 21:49:38303}
304
[email protected]001747c2008-09-10 00:37:07305void MessageLoop::AddToDelayedWorkQueue(const PendingTask& pending_task) {
306 // Move to the delayed work queue. Initialize the sequence number
307 // before inserting into the delayed_work_queue_. The sequence number
308 // is used to faciliate FIFO sorting when two tasks have the same
309 // delayed_run_time value.
310 PendingTask new_pending_task(pending_task);
311 new_pending_task.sequence_num = next_sequence_num_++;
312 delayed_work_queue_.push(new_pending_task);
313}
314
initial.commitd7cae122008-07-26 21:49:38315void MessageLoop::ReloadWorkQueue() {
316 // We can improve performance of our loading tasks from incoming_queue_ to
[email protected]fc7fb6e2008-08-16 03:09:05317 // work_queue_ by waiting until the last minute (work_queue_ is empty) to
318 // load. That reduces the number of locks-per-task significantly when our
[email protected]752578562008-09-07 08:08:29319 // queues get large.
320 if (!work_queue_.empty())
initial.commitd7cae122008-07-26 21:49:38321 return; // Wait till we *really* need to lock and load.
322
323 // Acquire all we can from the inter-thread queue with one lock acquisition.
initial.commitd7cae122008-07-26 21:49:38324 {
325 AutoLock lock(incoming_queue_lock_);
[email protected]752578562008-09-07 08:08:29326 if (incoming_queue_.empty())
initial.commitd7cae122008-07-26 21:49:38327 return;
[email protected]752578562008-09-07 08:08:29328 std::swap(incoming_queue_, work_queue_);
329 DCHECK(incoming_queue_.empty());
initial.commitd7cae122008-07-26 21:49:38330 }
331}
332
[email protected]001747c2008-09-10 00:37:07333bool MessageLoop::DeletePendingTasks() {
334 bool did_work = !work_queue_.empty();
335 while (!work_queue_.empty()) {
336 PendingTask pending_task = work_queue_.front();
337 work_queue_.pop();
338 if (!pending_task.delayed_run_time.is_null()) {
339 // We want to delete delayed tasks in the same order in which they would
340 // normally be deleted in case of any funny dependencies between delayed
341 // tasks.
342 AddToDelayedWorkQueue(pending_task);
343 } else {
344 // TODO(darin): Delete all tasks once it is safe to do so.
345 //delete task;
346 }
initial.commitd7cae122008-07-26 21:49:38347 }
[email protected]001747c2008-09-10 00:37:07348 did_work |= !deferred_non_nestable_work_queue_.empty();
349 while (!deferred_non_nestable_work_queue_.empty()) {
[email protected]001747c2008-09-10 00:37:07350 // TODO(darin): Delete all tasks once it is safe to do so.
[email protected]058bccb2008-09-10 01:16:17351 //Task* task = deferred_non_nestable_work_queue_.front().task;
352 deferred_non_nestable_work_queue_.pop();
[email protected]001747c2008-09-10 00:37:07353 //delete task;
initial.commitd7cae122008-07-26 21:49:38354 }
[email protected]001747c2008-09-10 00:37:07355 did_work |= !delayed_work_queue_.empty();
356 while (!delayed_work_queue_.empty()) {
357 Task* task = delayed_work_queue_.top().task;
358 delayed_work_queue_.pop();
359 delete task;
360 }
361 return did_work;
initial.commitd7cae122008-07-26 21:49:38362}
363
[email protected]fc7fb6e2008-08-16 03:09:05364bool MessageLoop::DoWork() {
[email protected]752578562008-09-07 08:08:29365 if (!nestable_tasks_allowed_) {
366 // Task can't be executed right now.
367 return false;
368 }
369
370 for (;;) {
371 ReloadWorkQueue();
372 if (work_queue_.empty())
373 break;
374
375 // Execute oldest task.
376 do {
377 PendingTask pending_task = work_queue_.front();
378 work_queue_.pop();
379 if (!pending_task.delayed_run_time.is_null()) {
380 bool was_empty = delayed_work_queue_.empty();
[email protected]001747c2008-09-10 00:37:07381 AddToDelayedWorkQueue(pending_task);
[email protected]752578562008-09-07 08:08:29382 if (was_empty) // We only schedule the next delayed work item.
383 pump_->ScheduleDelayedWork(pending_task.delayed_run_time);
384 } else {
385 if (DeferOrRunPendingTask(pending_task))
386 return true;
387 }
388 } while (!work_queue_.empty());
389 }
390
391 // Nothing happened.
392 return false;
[email protected]fc7fb6e2008-08-16 03:09:05393}
394
[email protected]b24250fc2008-08-20 06:30:58395bool MessageLoop::DoDelayedWork(Time* next_delayed_work_time) {
[email protected]752578562008-09-07 08:08:29396 if (!nestable_tasks_allowed_ || delayed_work_queue_.empty()) {
397 *next_delayed_work_time = Time();
398 return false;
399 }
400
401 if (delayed_work_queue_.top().delayed_run_time > Time::Now()) {
402 *next_delayed_work_time = delayed_work_queue_.top().delayed_run_time;
403 return false;
404 }
[email protected]fc7fb6e2008-08-16 03:09:05405
[email protected]752578562008-09-07 08:08:29406 PendingTask pending_task = delayed_work_queue_.top();
407 delayed_work_queue_.pop();
408
409 if (!delayed_work_queue_.empty())
410 *next_delayed_work_time = delayed_work_queue_.top().delayed_run_time;
[email protected]fc7fb6e2008-08-16 03:09:05411
[email protected]752578562008-09-07 08:08:29412 return DeferOrRunPendingTask(pending_task);
[email protected]fc7fb6e2008-08-16 03:09:05413}
414
415bool MessageLoop::DoIdleWork() {
416 if (ProcessNextDelayedNonNestableTask())
417 return true;
418
419 if (state_->quit_received)
420 pump_->Quit();
421
422 return false;
423}
424
425//------------------------------------------------------------------------------
426// MessageLoop::AutoRunState
427
428MessageLoop::AutoRunState::AutoRunState(MessageLoop* loop) : loop_(loop) {
429 // Make the loop reference us.
430 previous_state_ = loop_->state_;
431 if (previous_state_) {
432 run_depth = previous_state_->run_depth + 1;
[email protected]ea15e982008-08-15 07:31:20433 } else {
[email protected]fc7fb6e2008-08-16 03:09:05434 run_depth = 1;
[email protected]ea15e982008-08-15 07:31:20435 }
[email protected]fc7fb6e2008-08-16 03:09:05436 loop_->state_ = this;
437
438 // Initialize the other fields:
439 quit_received = false;
440#if defined(OS_WIN)
441 dispatcher = NULL;
442#endif
443}
444
445MessageLoop::AutoRunState::~AutoRunState() {
446 loop_->state_ = previous_state_;
[email protected]a5b94a92008-08-12 23:25:43447}
448
initial.commitd7cae122008-07-26 21:49:38449//------------------------------------------------------------------------------
[email protected]752578562008-09-07 08:08:29450// MessageLoop::PendingTask
initial.commitd7cae122008-07-26 21:49:38451
[email protected]752578562008-09-07 08:08:29452bool MessageLoop::PendingTask::operator<(const PendingTask& other) const {
453 // Since the top of a priority queue is defined as the "greatest" element, we
454 // need to invert the comparison here. We want the smaller time to be at the
455 // top of the heap.
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 false;
initial.commitd7cae122008-07-26 21:49:38459
[email protected]752578562008-09-07 08:08:29460 if (delayed_run_time > other.delayed_run_time)
461 return true;
initial.commitd7cae122008-07-26 21:49:38462
[email protected]752578562008-09-07 08:08:29463 // If the times happen to match, then we use the sequence number to decide.
464 // Compare the difference to support integer roll-over.
465 return (sequence_num - other.sequence_num) > 0;
initial.commitd7cae122008-07-26 21:49:38466}
467
468//------------------------------------------------------------------------------
469// Method and data for histogramming events and actions taken by each instance
470// on each thread.
471
472// static
473bool MessageLoop::enable_histogrammer_ = false;
474
475// static
476void MessageLoop::EnableHistogrammer(bool enable) {
477 enable_histogrammer_ = enable;
478}
479
480void MessageLoop::StartHistogrammer() {
481 if (enable_histogrammer_ && !message_histogram_.get()
482 && StatisticsRecorder::WasStarted()) {
[email protected]fc7fb6e2008-08-16 03:09:05483 DCHECK(!thread_name_.empty());
initial.commitd7cae122008-07-26 21:49:38484 message_histogram_.reset(new LinearHistogram(
485 ASCIIToWide("MsgLoop:" + thread_name_).c_str(),
486 kLeastNonZeroMessageId,
487 kMaxMessageId,
488 kNumberOfDistinctMessagesDisplayed));
489 message_histogram_->SetFlags(message_histogram_->kHexRangePrintingFlag);
490 message_histogram_->SetRangeDescriptions(event_descriptions_);
491 }
492}
493
494void MessageLoop::HistogramEvent(int event) {
495 if (message_histogram_.get())
496 message_histogram_->Add(event);
497}
498
initial.commitd7cae122008-07-26 21:49:38499// Provide a macro that takes an expression (such as a constant, or macro
500// constant) and creates a pair to initalize an array of pairs. In this case,
501// our pair consists of the expressions value, and the "stringized" version
502// of the expression (i.e., the exrpression put in quotes). For example, if
503// we have:
504// #define FOO 2
505// #define BAR 5
506// then the following:
507// VALUE_TO_NUMBER_AND_NAME(FOO + BAR)
508// will expand to:
509// {7, "FOO + BAR"}
510// We use the resulting array as an argument to our histogram, which reads the
511// number as a bucket identifier, and proceeds to use the corresponding name
512// in the pair (i.e., the quoted string) when printing out a histogram.
513#define VALUE_TO_NUMBER_AND_NAME(name) {name, #name},
514
initial.commitd7cae122008-07-26 21:49:38515// static
516const LinearHistogram::DescriptionPair MessageLoop::event_descriptions_[] = {
initial.commitd7cae122008-07-26 21:49:38517 // Provide some pretty print capability in our histogram for our internal
518 // messages.
519
initial.commitd7cae122008-07-26 21:49:38520 // A few events we handle (kindred to messages), and used to profile actions.
521 VALUE_TO_NUMBER_AND_NAME(kTaskRunEvent)
initial.commitd7cae122008-07-26 21:49:38522 VALUE_TO_NUMBER_AND_NAME(kTimerEvent)
523
524 {-1, NULL} // The list must be null terminated, per API to histogram.
525};
license.botbf09a502008-08-24 00:55:55526
[email protected]4d9bdfaf2008-08-26 05:53:57527//------------------------------------------------------------------------------
528// MessageLoopForUI
529
530#if defined(OS_WIN)
531
532void MessageLoopForUI::Run(Dispatcher* dispatcher) {
533 AutoRunState save_state(this);
534 state_->dispatcher = dispatcher;
535 RunHandler();
536}
537
538void MessageLoopForUI::AddObserver(Observer* observer) {
539 pump_win()->AddObserver(observer);
540}
541
542void MessageLoopForUI::RemoveObserver(Observer* observer) {
543 pump_win()->RemoveObserver(observer);
544}
545
546void MessageLoopForUI::WillProcessMessage(const MSG& message) {
547 pump_win()->WillProcessMessage(message);
548}
549void MessageLoopForUI::DidProcessMessage(const MSG& message) {
550 pump_win()->DidProcessMessage(message);
551}
552void MessageLoopForUI::PumpOutPendingPaintMessages() {
553 pump_win()->PumpOutPendingPaintMessages();
554}
555
556#endif // defined(OS_WIN)
557
558//------------------------------------------------------------------------------
559// MessageLoopForIO
560
561#if defined(OS_WIN)
562
563void MessageLoopForIO::WatchObject(HANDLE object, Watcher* watcher) {
564 pump_win()->WatchObject(object, watcher);
565}
566
567#endif // defined(OS_WIN)