blob: 181d76f704bc61731d7d67628d9bf1d14608bf00 [file] [log] [blame]
license.botf003cfe2008-08-24 09:55:55 +09001// 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.commit3f4a7322008-07-27 06:49:38 +09004
darin@google.com6ddeb842008-08-15 16:31:20 +09005#include "base/message_loop.h"
6
darin@google.com981f3552008-08-16 12:09:05 +09007#include <algorithm>
8
darin@google.combe165ae2008-09-07 17:08:29 +09009#if defined(OS_WIN)
10#include <mmsystem.h>
11#endif
12
mmentovai@google.comfa5f9932008-08-22 07:26:06 +090013#include "base/compiler_specific.h"
initial.commit3f4a7322008-07-27 06:49:38 +090014#include "base/logging.h"
darin@google.com12d40bb2008-08-20 03:36:23 +090015#include "base/message_pump_default.h"
initial.commit3f4a7322008-07-27 06:49:38 +090016#include "base/string_util.h"
17#include "base/thread_local_storage.h"
initial.commit3f4a7322008-07-27 06:49:38 +090018
19// a TLS index to the message loop for the current thread
20// Note that if we start doing complex stuff in other static initializers
21// this could cause problems.
evanm@google.comf26fd3a2008-08-21 07:54:52 +090022// TODO(evanm): this shouldn't rely on static initialization.
23// static
24TLSSlot MessageLoop::tls_index_;
initial.commit3f4a7322008-07-27 06:49:38 +090025
26//------------------------------------------------------------------------------
27
initial.commit3f4a7322008-07-27 06:49:38 +090028// Logical events for Histogram profiling. Run with -message-loop-histogrammer
29// to get an accounting of messages and actions taken on each thread.
darin@google.com981f3552008-08-16 12:09:05 +090030static const int kTaskRunEvent = 0x1;
31static const int kTimerEvent = 0x2;
initial.commit3f4a7322008-07-27 06:49:38 +090032
33// Provide range of message IDs for use in histogramming and debug display.
34static const int kLeastNonZeroMessageId = 1;
35static const int kMaxMessageId = 1099;
36static const int kNumberOfDistinctMessagesDisplayed = 1100;
37
38//------------------------------------------------------------------------------
39
darin@google.com981f3552008-08-16 12:09:05 +090040#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +090041
initial.commit3f4a7322008-07-27 06:49:38 +090042// Upon a SEH exception in this thread, it restores the original unhandled
43// exception filter.
44static int SEHFilter(LPTOP_LEVEL_EXCEPTION_FILTER old_filter) {
45 ::SetUnhandledExceptionFilter(old_filter);
46 return EXCEPTION_CONTINUE_SEARCH;
47}
48
49// Retrieves a pointer to the current unhandled exception filter. There
50// is no standalone getter method.
51static LPTOP_LEVEL_EXCEPTION_FILTER GetTopSEHFilter() {
52 LPTOP_LEVEL_EXCEPTION_FILTER top_filter = NULL;
53 top_filter = ::SetUnhandledExceptionFilter(0);
54 ::SetUnhandledExceptionFilter(top_filter);
55 return top_filter;
56}
57
darin@google.com981f3552008-08-16 12:09:05 +090058#endif // defined(OS_WIN)
59
initial.commit3f4a7322008-07-27 06:49:38 +090060//------------------------------------------------------------------------------
61
darin@google.comd936b5b2008-08-26 14:53:57 +090062MessageLoop::MessageLoop(Type type)
63 : type_(type),
darin@google.comee6fa722008-08-13 08:25:43 +090064 nestable_tasks_allowed_(true),
darin@google.com12d40bb2008-08-20 03:36:23 +090065 exception_restoration_(false),
darin@google.combe165ae2008-09-07 17:08:29 +090066 state_(NULL),
67 next_sequence_num_(0) {
darin@google.comd936b5b2008-08-26 14:53:57 +090068 DCHECK(!tls_index_.Get()) << "should only have one message loop per thread";
evanm@google.comf26fd3a2008-08-21 07:54:52 +090069 tls_index_.Set(this);
darin@google.comd936b5b2008-08-26 14:53:57 +090070
darin@google.combe165ae2008-09-07 17:08:29 +090071 // TODO(darin): This does not seem like the best place for this code to live!
72#if defined(OS_WIN)
73 // We've experimented with all sorts of timers, and initially tried
74 // to avoid using timeBeginPeriod because it does affect the system
75 // globally. However, after much investigation, it turns out that all
76 // of the major plugins (flash, windows media 9-11, and quicktime)
77 // already use timeBeginPeriod to increase the speed of the clock.
78 // Since the browser must work with these plugins, the browser already
79 // needs to support a fast clock. We may as well use this ourselves,
80 // as it really is the best timer mechanism for our needs.
81 timeBeginPeriod(1);
82#endif
83
darin@google.com132072e2008-08-26 15:52:41 +090084 // TODO(darin): Choose the pump based on the requested type.
darin@google.com981f3552008-08-16 12:09:05 +090085#if defined(OS_WIN)
darin@google.com0795f572008-08-30 09:22:48 +090086 if (type_ == TYPE_DEFAULT) {
87 pump_ = new base::MessagePumpDefault();
88 } else {
89 pump_ = new base::MessagePumpWin();
90 }
darin@google.com12d40bb2008-08-20 03:36:23 +090091#else
darin@google.com132072e2008-08-26 15:52:41 +090092 pump_ = new base::MessagePumpDefault();
darin@google.com981f3552008-08-16 12:09:05 +090093#endif
initial.commit3f4a7322008-07-27 06:49:38 +090094}
95
96MessageLoop::~MessageLoop() {
97 DCHECK(this == current());
darin@google.com965e5342008-08-06 08:16:41 +090098
99 // Let interested parties have one last shot at accessing this.
100 FOR_EACH_OBSERVER(DestructionObserver, destruction_observers_,
101 WillDestroyCurrentMessageLoop());
102
103 // OK, now make it so that no one can find us.
evanm@google.comf26fd3a2008-08-21 07:54:52 +0900104 tls_index_.Set(NULL);
darin@google.com965e5342008-08-06 08:16:41 +0900105
darin@google.com981f3552008-08-16 12:09:05 +0900106 DCHECK(!state_);
darin@google.com965e5342008-08-06 08:16:41 +0900107
initial.commit3f4a7322008-07-27 06:49:38 +0900108 // Most tasks that have not been Run() are deleted in the |timer_manager_|
109 // destructor after we remove our tls index. We delete the tasks in our
110 // queues here so their destuction is similar to the tasks in the
111 // |timer_manager_|.
112 DeletePendingTasks();
113 ReloadWorkQueue();
114 DeletePendingTasks();
darin@google.combe165ae2008-09-07 17:08:29 +0900115
116#if defined(OS_WIN)
117 // Match timeBeginPeriod() from construction.
118 timeEndPeriod(1);
119#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900120}
121
darin@google.com965e5342008-08-06 08:16:41 +0900122void MessageLoop::AddDestructionObserver(DestructionObserver *obs) {
123 DCHECK(this == current());
124 destruction_observers_.AddObserver(obs);
125}
126
127void MessageLoop::RemoveDestructionObserver(DestructionObserver *obs) {
128 DCHECK(this == current());
129 destruction_observers_.RemoveObserver(obs);
130}
131
darin@google.com6ddeb842008-08-15 16:31:20 +0900132void MessageLoop::Run() {
darin@google.com981f3552008-08-16 12:09:05 +0900133 AutoRunState save_state(this);
134 RunHandler();
darin@google.com6ddeb842008-08-15 16:31:20 +0900135}
136
jar@google.com9239e022008-07-31 22:10:20 +0900137void MessageLoop::RunAllPending() {
darin@google.com981f3552008-08-16 12:09:05 +0900138 AutoRunState save_state(this);
139 state_->quit_received = true; // Means run until we would otherwise block.
140 RunHandler();
initial.commit3f4a7322008-07-27 06:49:38 +0900141}
142
143// Runs the loop in two different SEH modes:
144// enable_SEH_restoration_ = false : any unhandled exception goes to the last
145// one that calls SetUnhandledExceptionFilter().
146// enable_SEH_restoration_ = true : any unhandled exception goes to the filter
147// that was existed before the loop was run.
darin@google.com981f3552008-08-16 12:09:05 +0900148void MessageLoop::RunHandler() {
149#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900150 if (exception_restoration_) {
151 LPTOP_LEVEL_EXCEPTION_FILTER current_filter = GetTopSEHFilter();
152 __try {
darin@google.com981f3552008-08-16 12:09:05 +0900153 RunInternal();
initial.commit3f4a7322008-07-27 06:49:38 +0900154 } __except(SEHFilter(current_filter)) {
155 }
darin@google.com981f3552008-08-16 12:09:05 +0900156 return;
initial.commit3f4a7322008-07-27 06:49:38 +0900157 }
darin@google.com981f3552008-08-16 12:09:05 +0900158#endif
159
160 RunInternal();
initial.commit3f4a7322008-07-27 06:49:38 +0900161}
162
163//------------------------------------------------------------------------------
initial.commit3f4a7322008-07-27 06:49:38 +0900164
darin@google.com981f3552008-08-16 12:09:05 +0900165void MessageLoop::RunInternal() {
166 DCHECK(this == current());
167
initial.commit3f4a7322008-07-27 06:49:38 +0900168 StartHistogrammer();
169
darin@google.com981f3552008-08-16 12:09:05 +0900170#if defined(OS_WIN)
171 if (state_->dispatcher) {
172 pump_win()->RunWithDispatcher(this, state_->dispatcher);
173 return;
jar@google.com9239e022008-07-31 22:10:20 +0900174 }
darin@google.com981f3552008-08-16 12:09:05 +0900175#endif
176
177 pump_->Run(this);
jar@google.comfbaaf692008-07-30 16:50:53 +0900178}
jar@google.com7ff36e62008-07-30 15:58:56 +0900179
jar@google.comb4d1bff2008-07-31 04:03:59 +0900180//------------------------------------------------------------------------------
181// Wrapper functions for use in above message loop framework.
182
initial.commit3f4a7322008-07-27 06:49:38 +0900183bool MessageLoop::ProcessNextDelayedNonNestableTask() {
darin@google.com981f3552008-08-16 12:09:05 +0900184 if (state_->run_depth != 1)
initial.commit3f4a7322008-07-27 06:49:38 +0900185 return false;
186
darin@google.combe165ae2008-09-07 17:08:29 +0900187 if (deferred_non_nestable_work_queue_.empty())
initial.commit3f4a7322008-07-27 06:49:38 +0900188 return false;
darin@google.combe165ae2008-09-07 17:08:29 +0900189
190 Task* task = deferred_non_nestable_work_queue_.front().task;
191 deferred_non_nestable_work_queue_.pop();
192
193 RunTask(task);
initial.commit3f4a7322008-07-27 06:49:38 +0900194 return true;
195}
196
initial.commit3f4a7322008-07-27 06:49:38 +0900197//------------------------------------------------------------------------------
198
199void MessageLoop::Quit() {
darin@google.com981f3552008-08-16 12:09:05 +0900200 DCHECK(current() == this);
201 if (state_) {
202 state_->quit_received = true;
203 } else {
204 NOTREACHED() << "Must be inside Run to call Quit";
initial.commit3f4a7322008-07-27 06:49:38 +0900205 }
initial.commit3f4a7322008-07-27 06:49:38 +0900206}
207
darin@google.combe165ae2008-09-07 17:08:29 +0900208void MessageLoop::PostTask(
209 const tracked_objects::Location& from_here, Task* task) {
210 PostTask_Helper(from_here, task, 0, true);
211}
212
213void MessageLoop::PostDelayedTask(
214 const tracked_objects::Location& from_here, Task* task, int delay_ms) {
215 PostTask_Helper(from_here, task, delay_ms, true);
216}
217
218void MessageLoop::PostNonNestableTask(
219 const tracked_objects::Location& from_here, Task* task) {
220 PostTask_Helper(from_here, task, 0, false);
221}
222
223void MessageLoop::PostNonNestableDelayedTask(
224 const tracked_objects::Location& from_here, Task* task, int delay_ms) {
225 PostTask_Helper(from_here, task, delay_ms, false);
226}
227
initial.commit3f4a7322008-07-27 06:49:38 +0900228// Possibly called on a background thread!
darin@google.combe165ae2008-09-07 17:08:29 +0900229void MessageLoop::PostTask_Helper(
230 const tracked_objects::Location& from_here, Task* task, int delay_ms,
231 bool nestable) {
initial.commit3f4a7322008-07-27 06:49:38 +0900232 task->SetBirthPlace(from_here);
darin@google.com0795f572008-08-30 09:22:48 +0900233
darin@google.combe165ae2008-09-07 17:08:29 +0900234 PendingTask pending_task(task, nestable);
darin@google.com0795f572008-08-30 09:22:48 +0900235
236 if (delay_ms > 0) {
darin@google.combe165ae2008-09-07 17:08:29 +0900237 pending_task.delayed_run_time =
darin@google.com0795f572008-08-30 09:22:48 +0900238 Time::Now() + TimeDelta::FromMilliseconds(delay_ms);
239 } else {
240 DCHECK(delay_ms == 0) << "delay should not be negative";
241 }
242
initial.commit3f4a7322008-07-27 06:49:38 +0900243 // Warning: Don't try to short-circuit, and handle this thread's tasks more
244 // directly, as it could starve handling of foreign threads. Put every task
245 // into this queue.
246
darin@google.com981f3552008-08-16 12:09:05 +0900247 scoped_refptr<base::MessagePump> pump;
initial.commit3f4a7322008-07-27 06:49:38 +0900248 {
darin@google.com981f3552008-08-16 12:09:05 +0900249 AutoLock locked(incoming_queue_lock_);
250
darin@google.combe165ae2008-09-07 17:08:29 +0900251 bool was_empty = incoming_queue_.empty();
252 incoming_queue_.push(pending_task);
initial.commit3f4a7322008-07-27 06:49:38 +0900253 if (!was_empty)
254 return; // Someone else should have started the sub-pump.
255
darin@google.com981f3552008-08-16 12:09:05 +0900256 pump = pump_;
darin@google.com6ddeb842008-08-15 16:31:20 +0900257 }
darin@google.com981f3552008-08-16 12:09:05 +0900258 // Since the incoming_queue_ may contain a task that destroys this message
259 // loop, we cannot exit incoming_queue_lock_ until we are done with |this|.
260 // We use a stack-based reference to the message pump so that we can call
261 // ScheduleWork outside of incoming_queue_lock_.
darin@google.com6ddeb842008-08-15 16:31:20 +0900262
darin@google.com981f3552008-08-16 12:09:05 +0900263 pump->ScheduleWork();
initial.commit3f4a7322008-07-27 06:49:38 +0900264}
265
266void MessageLoop::SetNestableTasksAllowed(bool allowed) {
mpcomplete@google.com989d5f82008-08-09 09:14:09 +0900267 if (nestable_tasks_allowed_ != allowed) {
268 nestable_tasks_allowed_ = allowed;
269 if (!nestable_tasks_allowed_)
270 return;
271 // Start the native pump if we are not already pumping.
darin@google.com981f3552008-08-16 12:09:05 +0900272 pump_->ScheduleWork();
mpcomplete@google.com989d5f82008-08-09 09:14:09 +0900273 }
initial.commit3f4a7322008-07-27 06:49:38 +0900274}
275
276bool MessageLoop::NestableTasksAllowed() const {
277 return nestable_tasks_allowed_;
278}
279
initial.commit3f4a7322008-07-27 06:49:38 +0900280//------------------------------------------------------------------------------
initial.commit3f4a7322008-07-27 06:49:38 +0900281
initial.commit3f4a7322008-07-27 06:49:38 +0900282void MessageLoop::RunTask(Task* task) {
initial.commit3f4a7322008-07-27 06:49:38 +0900283 DCHECK(nestable_tasks_allowed_);
284 // Execute the task and assume the worst: It is probably not reentrant.
285 nestable_tasks_allowed_ = false;
darin@google.combe165ae2008-09-07 17:08:29 +0900286
287 HistogramEvent(kTaskRunEvent);
288 task->Run();
289 delete task;
290
291 nestable_tasks_allowed_ = true;
initial.commit3f4a7322008-07-27 06:49:38 +0900292}
293
darin@google.combe165ae2008-09-07 17:08:29 +0900294bool MessageLoop::DeferOrRunPendingTask(const PendingTask& pending_task) {
295 if (pending_task.nestable || state_->run_depth == 1) {
296 RunTask(pending_task.task);
297 // Show that we ran a task (Note: a new one might arrive as a
298 // consequence!).
299 return true;
300 }
301
302 // We couldn't run the task now because we're in a nested message loop
303 // and the task isn't nestable.
304 deferred_non_nestable_work_queue_.push(pending_task);
305 return false;
initial.commit3f4a7322008-07-27 06:49:38 +0900306}
307
initial.commit3f4a7322008-07-27 06:49:38 +0900308void MessageLoop::ReloadWorkQueue() {
309 // We can improve performance of our loading tasks from incoming_queue_ to
darin@google.com981f3552008-08-16 12:09:05 +0900310 // work_queue_ by waiting until the last minute (work_queue_ is empty) to
311 // load. That reduces the number of locks-per-task significantly when our
darin@google.combe165ae2008-09-07 17:08:29 +0900312 // queues get large.
313 if (!work_queue_.empty())
initial.commit3f4a7322008-07-27 06:49:38 +0900314 return; // Wait till we *really* need to lock and load.
315
316 // Acquire all we can from the inter-thread queue with one lock acquisition.
initial.commit3f4a7322008-07-27 06:49:38 +0900317 {
318 AutoLock lock(incoming_queue_lock_);
darin@google.combe165ae2008-09-07 17:08:29 +0900319 if (incoming_queue_.empty())
initial.commit3f4a7322008-07-27 06:49:38 +0900320 return;
darin@google.combe165ae2008-09-07 17:08:29 +0900321 std::swap(incoming_queue_, work_queue_);
322 DCHECK(incoming_queue_.empty());
initial.commit3f4a7322008-07-27 06:49:38 +0900323 }
324}
325
326void MessageLoop::DeletePendingTasks() {
darin@google.com62b58bc2008-08-27 14:29:40 +0900327 /* Comment this out as it's causing crashes.
initial.commit3f4a7322008-07-27 06:49:38 +0900328 while (!work_queue_.Empty()) {
329 Task* task = work_queue_.Pop();
330 if (task->is_owned_by_message_loop())
331 delete task;
332 }
333
334 while (!delayed_non_nestable_queue_.Empty()) {
335 Task* task = delayed_non_nestable_queue_.Pop();
336 if (task->is_owned_by_message_loop())
337 delete task;
338 }
darin@google.com62b58bc2008-08-27 14:29:40 +0900339 */
initial.commit3f4a7322008-07-27 06:49:38 +0900340}
341
darin@google.com981f3552008-08-16 12:09:05 +0900342bool MessageLoop::DoWork() {
darin@google.combe165ae2008-09-07 17:08:29 +0900343 if (!nestable_tasks_allowed_) {
344 // Task can't be executed right now.
345 return false;
346 }
347
348 for (;;) {
349 ReloadWorkQueue();
350 if (work_queue_.empty())
351 break;
352
353 // Execute oldest task.
354 do {
355 PendingTask pending_task = work_queue_.front();
356 work_queue_.pop();
357 if (!pending_task.delayed_run_time.is_null()) {
358 bool was_empty = delayed_work_queue_.empty();
359
360 // Move to the delayed work queue. Initialize the sequence number
361 // before inserting into the delayed_work_queue_. The sequence number
362 // is used to faciliate FIFO sorting when two tasks have the same
363 // delayed_run_time value.
364 pending_task.sequence_num = next_sequence_num_++;
365 delayed_work_queue_.push(pending_task);
366
367 if (was_empty) // We only schedule the next delayed work item.
368 pump_->ScheduleDelayedWork(pending_task.delayed_run_time);
369 } else {
370 if (DeferOrRunPendingTask(pending_task))
371 return true;
372 }
373 } while (!work_queue_.empty());
374 }
375
376 // Nothing happened.
377 return false;
darin@google.com981f3552008-08-16 12:09:05 +0900378}
379
darin@google.com6393bed2008-08-20 15:30:58 +0900380bool MessageLoop::DoDelayedWork(Time* next_delayed_work_time) {
darin@google.combe165ae2008-09-07 17:08:29 +0900381 if (!nestable_tasks_allowed_ || delayed_work_queue_.empty()) {
382 *next_delayed_work_time = Time();
383 return false;
384 }
385
386 if (delayed_work_queue_.top().delayed_run_time > Time::Now()) {
387 *next_delayed_work_time = delayed_work_queue_.top().delayed_run_time;
388 return false;
389 }
darin@google.com981f3552008-08-16 12:09:05 +0900390
darin@google.combe165ae2008-09-07 17:08:29 +0900391 PendingTask pending_task = delayed_work_queue_.top();
392 delayed_work_queue_.pop();
393
394 if (!delayed_work_queue_.empty())
395 *next_delayed_work_time = delayed_work_queue_.top().delayed_run_time;
darin@google.com981f3552008-08-16 12:09:05 +0900396
darin@google.combe165ae2008-09-07 17:08:29 +0900397 return DeferOrRunPendingTask(pending_task);
darin@google.com981f3552008-08-16 12:09:05 +0900398}
399
400bool MessageLoop::DoIdleWork() {
401 if (ProcessNextDelayedNonNestableTask())
402 return true;
403
404 if (state_->quit_received)
405 pump_->Quit();
406
407 return false;
408}
409
410//------------------------------------------------------------------------------
411// MessageLoop::AutoRunState
412
413MessageLoop::AutoRunState::AutoRunState(MessageLoop* loop) : loop_(loop) {
414 // Make the loop reference us.
415 previous_state_ = loop_->state_;
416 if (previous_state_) {
417 run_depth = previous_state_->run_depth + 1;
darin@google.com6ddeb842008-08-15 16:31:20 +0900418 } else {
darin@google.com981f3552008-08-16 12:09:05 +0900419 run_depth = 1;
darin@google.com6ddeb842008-08-15 16:31:20 +0900420 }
darin@google.com981f3552008-08-16 12:09:05 +0900421 loop_->state_ = this;
422
423 // Initialize the other fields:
424 quit_received = false;
425#if defined(OS_WIN)
426 dispatcher = NULL;
427#endif
428}
429
430MessageLoop::AutoRunState::~AutoRunState() {
431 loop_->state_ = previous_state_;
darin@google.comee6fa722008-08-13 08:25:43 +0900432}
433
initial.commit3f4a7322008-07-27 06:49:38 +0900434//------------------------------------------------------------------------------
darin@google.combe165ae2008-09-07 17:08:29 +0900435// MessageLoop::PendingTask
initial.commit3f4a7322008-07-27 06:49:38 +0900436
darin@google.combe165ae2008-09-07 17:08:29 +0900437bool MessageLoop::PendingTask::operator<(const PendingTask& other) const {
438 // Since the top of a priority queue is defined as the "greatest" element, we
439 // need to invert the comparison here. We want the smaller time to be at the
440 // top of the heap.
initial.commit3f4a7322008-07-27 06:49:38 +0900441
darin@google.combe165ae2008-09-07 17:08:29 +0900442 if (delayed_run_time < other.delayed_run_time)
443 return false;
initial.commit3f4a7322008-07-27 06:49:38 +0900444
darin@google.combe165ae2008-09-07 17:08:29 +0900445 if (delayed_run_time > other.delayed_run_time)
446 return true;
initial.commit3f4a7322008-07-27 06:49:38 +0900447
darin@google.combe165ae2008-09-07 17:08:29 +0900448 // If the times happen to match, then we use the sequence number to decide.
449 // Compare the difference to support integer roll-over.
450 return (sequence_num - other.sequence_num) > 0;
initial.commit3f4a7322008-07-27 06:49:38 +0900451}
452
453//------------------------------------------------------------------------------
454// Method and data for histogramming events and actions taken by each instance
455// on each thread.
456
457// static
458bool MessageLoop::enable_histogrammer_ = false;
459
460// static
461void MessageLoop::EnableHistogrammer(bool enable) {
462 enable_histogrammer_ = enable;
463}
464
465void MessageLoop::StartHistogrammer() {
466 if (enable_histogrammer_ && !message_histogram_.get()
467 && StatisticsRecorder::WasStarted()) {
darin@google.com981f3552008-08-16 12:09:05 +0900468 DCHECK(!thread_name_.empty());
initial.commit3f4a7322008-07-27 06:49:38 +0900469 message_histogram_.reset(new LinearHistogram(
470 ASCIIToWide("MsgLoop:" + thread_name_).c_str(),
471 kLeastNonZeroMessageId,
472 kMaxMessageId,
473 kNumberOfDistinctMessagesDisplayed));
474 message_histogram_->SetFlags(message_histogram_->kHexRangePrintingFlag);
475 message_histogram_->SetRangeDescriptions(event_descriptions_);
476 }
477}
478
479void MessageLoop::HistogramEvent(int event) {
480 if (message_histogram_.get())
481 message_histogram_->Add(event);
482}
483
initial.commit3f4a7322008-07-27 06:49:38 +0900484// Provide a macro that takes an expression (such as a constant, or macro
485// constant) and creates a pair to initalize an array of pairs. In this case,
486// our pair consists of the expressions value, and the "stringized" version
487// of the expression (i.e., the exrpression put in quotes). For example, if
488// we have:
489// #define FOO 2
490// #define BAR 5
491// then the following:
492// VALUE_TO_NUMBER_AND_NAME(FOO + BAR)
493// will expand to:
494// {7, "FOO + BAR"}
495// We use the resulting array as an argument to our histogram, which reads the
496// number as a bucket identifier, and proceeds to use the corresponding name
497// in the pair (i.e., the quoted string) when printing out a histogram.
498#define VALUE_TO_NUMBER_AND_NAME(name) {name, #name},
499
initial.commit3f4a7322008-07-27 06:49:38 +0900500// static
501const LinearHistogram::DescriptionPair MessageLoop::event_descriptions_[] = {
initial.commit3f4a7322008-07-27 06:49:38 +0900502 // Provide some pretty print capability in our histogram for our internal
503 // messages.
504
initial.commit3f4a7322008-07-27 06:49:38 +0900505 // A few events we handle (kindred to messages), and used to profile actions.
506 VALUE_TO_NUMBER_AND_NAME(kTaskRunEvent)
initial.commit3f4a7322008-07-27 06:49:38 +0900507 VALUE_TO_NUMBER_AND_NAME(kTimerEvent)
508
509 {-1, NULL} // The list must be null terminated, per API to histogram.
510};
license.botf003cfe2008-08-24 09:55:55 +0900511
darin@google.comd936b5b2008-08-26 14:53:57 +0900512//------------------------------------------------------------------------------
513// MessageLoopForUI
514
515#if defined(OS_WIN)
516
517void MessageLoopForUI::Run(Dispatcher* dispatcher) {
518 AutoRunState save_state(this);
519 state_->dispatcher = dispatcher;
520 RunHandler();
521}
522
523void MessageLoopForUI::AddObserver(Observer* observer) {
524 pump_win()->AddObserver(observer);
525}
526
527void MessageLoopForUI::RemoveObserver(Observer* observer) {
528 pump_win()->RemoveObserver(observer);
529}
530
531void MessageLoopForUI::WillProcessMessage(const MSG& message) {
532 pump_win()->WillProcessMessage(message);
533}
534void MessageLoopForUI::DidProcessMessage(const MSG& message) {
535 pump_win()->DidProcessMessage(message);
536}
537void MessageLoopForUI::PumpOutPendingPaintMessages() {
538 pump_win()->PumpOutPendingPaintMessages();
539}
540
541#endif // defined(OS_WIN)
542
543//------------------------------------------------------------------------------
544// MessageLoopForIO
545
546#if defined(OS_WIN)
547
548void MessageLoopForIO::WatchObject(HANDLE object, Watcher* watcher) {
549 pump_win()->WatchObject(object, watcher);
550}
551
552#endif // defined(OS_WIN)