blob: fffeb098e4095b11607028eed9685c710176736b [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
darin@google.com510f6062008-09-09 14:15:56 +0900103 // OK, now make it so that no one can find us.
104 tls_index_.Set(NULL);
darin@google.comebe504d2008-09-07 17:52:31 +0900105
darin@google.com0e500502008-09-09 14:55:35 +0900106 DCHECK(!state_);
107
108 // 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();
115
116 // Delete tasks in the delayed work queue.
117 while (!delayed_work_queue_.empty()) {
118 Task* task = delayed_work_queue_.top().task;
119 delayed_work_queue_.pop();
120 delete task;
121 }
122
darin@google.combe165ae2008-09-07 17:08:29 +0900123#if defined(OS_WIN)
124 // Match timeBeginPeriod() from construction.
125 timeEndPeriod(1);
126#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900127}
128
darin@google.com965e5342008-08-06 08:16:41 +0900129void MessageLoop::AddDestructionObserver(DestructionObserver *obs) {
130 DCHECK(this == current());
131 destruction_observers_.AddObserver(obs);
132}
133
134void MessageLoop::RemoveDestructionObserver(DestructionObserver *obs) {
135 DCHECK(this == current());
136 destruction_observers_.RemoveObserver(obs);
137}
138
darin@google.com6ddeb842008-08-15 16:31:20 +0900139void MessageLoop::Run() {
darin@google.com981f3552008-08-16 12:09:05 +0900140 AutoRunState save_state(this);
141 RunHandler();
darin@google.com6ddeb842008-08-15 16:31:20 +0900142}
143
jar@google.com9239e022008-07-31 22:10:20 +0900144void MessageLoop::RunAllPending() {
darin@google.com981f3552008-08-16 12:09:05 +0900145 AutoRunState save_state(this);
146 state_->quit_received = true; // Means run until we would otherwise block.
147 RunHandler();
initial.commit3f4a7322008-07-27 06:49:38 +0900148}
149
150// Runs the loop in two different SEH modes:
151// enable_SEH_restoration_ = false : any unhandled exception goes to the last
152// one that calls SetUnhandledExceptionFilter().
153// enable_SEH_restoration_ = true : any unhandled exception goes to the filter
154// that was existed before the loop was run.
darin@google.com981f3552008-08-16 12:09:05 +0900155void MessageLoop::RunHandler() {
156#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900157 if (exception_restoration_) {
158 LPTOP_LEVEL_EXCEPTION_FILTER current_filter = GetTopSEHFilter();
159 __try {
darin@google.com981f3552008-08-16 12:09:05 +0900160 RunInternal();
initial.commit3f4a7322008-07-27 06:49:38 +0900161 } __except(SEHFilter(current_filter)) {
162 }
darin@google.com981f3552008-08-16 12:09:05 +0900163 return;
initial.commit3f4a7322008-07-27 06:49:38 +0900164 }
darin@google.com981f3552008-08-16 12:09:05 +0900165#endif
166
167 RunInternal();
initial.commit3f4a7322008-07-27 06:49:38 +0900168}
169
170//------------------------------------------------------------------------------
initial.commit3f4a7322008-07-27 06:49:38 +0900171
darin@google.com981f3552008-08-16 12:09:05 +0900172void MessageLoop::RunInternal() {
173 DCHECK(this == current());
174
initial.commit3f4a7322008-07-27 06:49:38 +0900175 StartHistogrammer();
176
darin@google.com981f3552008-08-16 12:09:05 +0900177#if defined(OS_WIN)
178 if (state_->dispatcher) {
179 pump_win()->RunWithDispatcher(this, state_->dispatcher);
180 return;
jar@google.com9239e022008-07-31 22:10:20 +0900181 }
darin@google.com981f3552008-08-16 12:09:05 +0900182#endif
183
184 pump_->Run(this);
jar@google.comfbaaf692008-07-30 16:50:53 +0900185}
jar@google.com7ff36e62008-07-30 15:58:56 +0900186
jar@google.comb4d1bff2008-07-31 04:03:59 +0900187//------------------------------------------------------------------------------
188// Wrapper functions for use in above message loop framework.
189
initial.commit3f4a7322008-07-27 06:49:38 +0900190bool MessageLoop::ProcessNextDelayedNonNestableTask() {
darin@google.com981f3552008-08-16 12:09:05 +0900191 if (state_->run_depth != 1)
initial.commit3f4a7322008-07-27 06:49:38 +0900192 return false;
193
darin@google.combe165ae2008-09-07 17:08:29 +0900194 if (deferred_non_nestable_work_queue_.empty())
initial.commit3f4a7322008-07-27 06:49:38 +0900195 return false;
darin@google.combe165ae2008-09-07 17:08:29 +0900196
197 Task* task = deferred_non_nestable_work_queue_.front().task;
198 deferred_non_nestable_work_queue_.pop();
199
200 RunTask(task);
initial.commit3f4a7322008-07-27 06:49:38 +0900201 return true;
202}
203
initial.commit3f4a7322008-07-27 06:49:38 +0900204//------------------------------------------------------------------------------
205
206void MessageLoop::Quit() {
darin@google.com981f3552008-08-16 12:09:05 +0900207 DCHECK(current() == this);
208 if (state_) {
209 state_->quit_received = true;
210 } else {
211 NOTREACHED() << "Must be inside Run to call Quit";
initial.commit3f4a7322008-07-27 06:49:38 +0900212 }
initial.commit3f4a7322008-07-27 06:49:38 +0900213}
214
darin@google.combe165ae2008-09-07 17:08:29 +0900215void MessageLoop::PostTask(
216 const tracked_objects::Location& from_here, Task* task) {
217 PostTask_Helper(from_here, task, 0, true);
218}
219
220void MessageLoop::PostDelayedTask(
221 const tracked_objects::Location& from_here, Task* task, int delay_ms) {
222 PostTask_Helper(from_here, task, delay_ms, true);
223}
224
225void MessageLoop::PostNonNestableTask(
226 const tracked_objects::Location& from_here, Task* task) {
227 PostTask_Helper(from_here, task, 0, false);
228}
229
230void MessageLoop::PostNonNestableDelayedTask(
231 const tracked_objects::Location& from_here, Task* task, int delay_ms) {
232 PostTask_Helper(from_here, task, delay_ms, false);
233}
234
initial.commit3f4a7322008-07-27 06:49:38 +0900235// Possibly called on a background thread!
darin@google.combe165ae2008-09-07 17:08:29 +0900236void MessageLoop::PostTask_Helper(
237 const tracked_objects::Location& from_here, Task* task, int delay_ms,
238 bool nestable) {
initial.commit3f4a7322008-07-27 06:49:38 +0900239 task->SetBirthPlace(from_here);
darin@google.com0795f572008-08-30 09:22:48 +0900240
darin@google.combe165ae2008-09-07 17:08:29 +0900241 PendingTask pending_task(task, nestable);
darin@google.com0795f572008-08-30 09:22:48 +0900242
243 if (delay_ms > 0) {
darin@google.combe165ae2008-09-07 17:08:29 +0900244 pending_task.delayed_run_time =
darin@google.com0795f572008-08-30 09:22:48 +0900245 Time::Now() + TimeDelta::FromMilliseconds(delay_ms);
246 } else {
247 DCHECK(delay_ms == 0) << "delay should not be negative";
248 }
249
initial.commit3f4a7322008-07-27 06:49:38 +0900250 // Warning: Don't try to short-circuit, and handle this thread's tasks more
251 // directly, as it could starve handling of foreign threads. Put every task
252 // into this queue.
253
darin@google.com981f3552008-08-16 12:09:05 +0900254 scoped_refptr<base::MessagePump> pump;
initial.commit3f4a7322008-07-27 06:49:38 +0900255 {
darin@google.com981f3552008-08-16 12:09:05 +0900256 AutoLock locked(incoming_queue_lock_);
257
darin@google.combe165ae2008-09-07 17:08:29 +0900258 bool was_empty = incoming_queue_.empty();
259 incoming_queue_.push(pending_task);
initial.commit3f4a7322008-07-27 06:49:38 +0900260 if (!was_empty)
261 return; // Someone else should have started the sub-pump.
262
darin@google.com981f3552008-08-16 12:09:05 +0900263 pump = pump_;
darin@google.com6ddeb842008-08-15 16:31:20 +0900264 }
darin@google.com981f3552008-08-16 12:09:05 +0900265 // Since the incoming_queue_ may contain a task that destroys this message
266 // loop, we cannot exit incoming_queue_lock_ until we are done with |this|.
267 // We use a stack-based reference to the message pump so that we can call
268 // ScheduleWork outside of incoming_queue_lock_.
darin@google.com6ddeb842008-08-15 16:31:20 +0900269
darin@google.com981f3552008-08-16 12:09:05 +0900270 pump->ScheduleWork();
initial.commit3f4a7322008-07-27 06:49:38 +0900271}
272
273void MessageLoop::SetNestableTasksAllowed(bool allowed) {
mpcomplete@google.com989d5f82008-08-09 09:14:09 +0900274 if (nestable_tasks_allowed_ != allowed) {
275 nestable_tasks_allowed_ = allowed;
276 if (!nestable_tasks_allowed_)
277 return;
278 // Start the native pump if we are not already pumping.
darin@google.com981f3552008-08-16 12:09:05 +0900279 pump_->ScheduleWork();
mpcomplete@google.com989d5f82008-08-09 09:14:09 +0900280 }
initial.commit3f4a7322008-07-27 06:49:38 +0900281}
282
283bool MessageLoop::NestableTasksAllowed() const {
284 return nestable_tasks_allowed_;
285}
286
initial.commit3f4a7322008-07-27 06:49:38 +0900287//------------------------------------------------------------------------------
initial.commit3f4a7322008-07-27 06:49:38 +0900288
initial.commit3f4a7322008-07-27 06:49:38 +0900289void MessageLoop::RunTask(Task* task) {
initial.commit3f4a7322008-07-27 06:49:38 +0900290 DCHECK(nestable_tasks_allowed_);
291 // Execute the task and assume the worst: It is probably not reentrant.
292 nestable_tasks_allowed_ = false;
darin@google.combe165ae2008-09-07 17:08:29 +0900293
294 HistogramEvent(kTaskRunEvent);
295 task->Run();
296 delete task;
297
298 nestable_tasks_allowed_ = true;
initial.commit3f4a7322008-07-27 06:49:38 +0900299}
300
darin@google.combe165ae2008-09-07 17:08:29 +0900301bool MessageLoop::DeferOrRunPendingTask(const PendingTask& pending_task) {
302 if (pending_task.nestable || state_->run_depth == 1) {
303 RunTask(pending_task.task);
304 // Show that we ran a task (Note: a new one might arrive as a
305 // consequence!).
306 return true;
307 }
308
309 // We couldn't run the task now because we're in a nested message loop
310 // and the task isn't nestable.
311 deferred_non_nestable_work_queue_.push(pending_task);
312 return false;
initial.commit3f4a7322008-07-27 06:49:38 +0900313}
314
initial.commit3f4a7322008-07-27 06:49:38 +0900315void MessageLoop::ReloadWorkQueue() {
316 // We can improve performance of our loading tasks from incoming_queue_ to
darin@google.com981f3552008-08-16 12:09:05 +0900317 // 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
darin@google.combe165ae2008-09-07 17:08:29 +0900319 // queues get large.
320 if (!work_queue_.empty())
initial.commit3f4a7322008-07-27 06:49:38 +0900321 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.commit3f4a7322008-07-27 06:49:38 +0900324 {
325 AutoLock lock(incoming_queue_lock_);
darin@google.combe165ae2008-09-07 17:08:29 +0900326 if (incoming_queue_.empty())
initial.commit3f4a7322008-07-27 06:49:38 +0900327 return;
darin@google.combe165ae2008-09-07 17:08:29 +0900328 std::swap(incoming_queue_, work_queue_);
329 DCHECK(incoming_queue_.empty());
initial.commit3f4a7322008-07-27 06:49:38 +0900330 }
331}
332
darin@google.com0e500502008-09-09 14:55:35 +0900333void MessageLoop::DeletePendingTasks() {
334 /* Comment this out as it's causing crashes.
335 while (!work_queue_.Empty()) {
336 Task* task = work_queue_.Pop();
337 if (task->is_owned_by_message_loop())
338 delete task;
initial.commit3f4a7322008-07-27 06:49:38 +0900339 }
darin@google.com0e500502008-09-09 14:55:35 +0900340
341 while (!delayed_non_nestable_queue_.Empty()) {
342 Task* task = delayed_non_nestable_queue_.Pop();
343 if (task->is_owned_by_message_loop())
344 delete task;
initial.commit3f4a7322008-07-27 06:49:38 +0900345 }
darin@google.com0e500502008-09-09 14:55:35 +0900346 */
initial.commit3f4a7322008-07-27 06:49:38 +0900347}
348
darin@google.com981f3552008-08-16 12:09:05 +0900349bool MessageLoop::DoWork() {
darin@google.combe165ae2008-09-07 17:08:29 +0900350 if (!nestable_tasks_allowed_) {
351 // Task can't be executed right now.
352 return false;
353 }
354
355 for (;;) {
356 ReloadWorkQueue();
357 if (work_queue_.empty())
358 break;
359
360 // Execute oldest task.
361 do {
362 PendingTask pending_task = work_queue_.front();
363 work_queue_.pop();
364 if (!pending_task.delayed_run_time.is_null()) {
365 bool was_empty = delayed_work_queue_.empty();
366
367 // Move to the delayed work queue. Initialize the sequence number
368 // before inserting into the delayed_work_queue_. The sequence number
369 // is used to faciliate FIFO sorting when two tasks have the same
370 // delayed_run_time value.
371 pending_task.sequence_num = next_sequence_num_++;
372 delayed_work_queue_.push(pending_task);
373
374 if (was_empty) // We only schedule the next delayed work item.
375 pump_->ScheduleDelayedWork(pending_task.delayed_run_time);
376 } else {
377 if (DeferOrRunPendingTask(pending_task))
378 return true;
379 }
380 } while (!work_queue_.empty());
381 }
382
383 // Nothing happened.
384 return false;
darin@google.com981f3552008-08-16 12:09:05 +0900385}
386
darin@google.com6393bed2008-08-20 15:30:58 +0900387bool MessageLoop::DoDelayedWork(Time* next_delayed_work_time) {
darin@google.combe165ae2008-09-07 17:08:29 +0900388 if (!nestable_tasks_allowed_ || delayed_work_queue_.empty()) {
389 *next_delayed_work_time = Time();
390 return false;
391 }
392
393 if (delayed_work_queue_.top().delayed_run_time > Time::Now()) {
394 *next_delayed_work_time = delayed_work_queue_.top().delayed_run_time;
395 return false;
396 }
darin@google.com981f3552008-08-16 12:09:05 +0900397
darin@google.combe165ae2008-09-07 17:08:29 +0900398 PendingTask pending_task = delayed_work_queue_.top();
399 delayed_work_queue_.pop();
400
401 if (!delayed_work_queue_.empty())
402 *next_delayed_work_time = delayed_work_queue_.top().delayed_run_time;
darin@google.com981f3552008-08-16 12:09:05 +0900403
darin@google.combe165ae2008-09-07 17:08:29 +0900404 return DeferOrRunPendingTask(pending_task);
darin@google.com981f3552008-08-16 12:09:05 +0900405}
406
407bool MessageLoop::DoIdleWork() {
408 if (ProcessNextDelayedNonNestableTask())
409 return true;
410
411 if (state_->quit_received)
412 pump_->Quit();
413
414 return false;
415}
416
417//------------------------------------------------------------------------------
418// MessageLoop::AutoRunState
419
420MessageLoop::AutoRunState::AutoRunState(MessageLoop* loop) : loop_(loop) {
421 // Make the loop reference us.
422 previous_state_ = loop_->state_;
423 if (previous_state_) {
424 run_depth = previous_state_->run_depth + 1;
darin@google.com6ddeb842008-08-15 16:31:20 +0900425 } else {
darin@google.com981f3552008-08-16 12:09:05 +0900426 run_depth = 1;
darin@google.com6ddeb842008-08-15 16:31:20 +0900427 }
darin@google.com981f3552008-08-16 12:09:05 +0900428 loop_->state_ = this;
429
430 // Initialize the other fields:
431 quit_received = false;
432#if defined(OS_WIN)
433 dispatcher = NULL;
434#endif
435}
436
437MessageLoop::AutoRunState::~AutoRunState() {
438 loop_->state_ = previous_state_;
darin@google.comee6fa722008-08-13 08:25:43 +0900439}
440
initial.commit3f4a7322008-07-27 06:49:38 +0900441//------------------------------------------------------------------------------
darin@google.combe165ae2008-09-07 17:08:29 +0900442// MessageLoop::PendingTask
initial.commit3f4a7322008-07-27 06:49:38 +0900443
darin@google.combe165ae2008-09-07 17:08:29 +0900444bool MessageLoop::PendingTask::operator<(const PendingTask& other) const {
445 // Since the top of a priority queue is defined as the "greatest" element, we
446 // need to invert the comparison here. We want the smaller time to be at the
447 // top of the heap.
initial.commit3f4a7322008-07-27 06:49:38 +0900448
darin@google.combe165ae2008-09-07 17:08:29 +0900449 if (delayed_run_time < other.delayed_run_time)
450 return false;
initial.commit3f4a7322008-07-27 06:49:38 +0900451
darin@google.combe165ae2008-09-07 17:08:29 +0900452 if (delayed_run_time > other.delayed_run_time)
453 return true;
initial.commit3f4a7322008-07-27 06:49:38 +0900454
darin@google.combe165ae2008-09-07 17:08:29 +0900455 // If the times happen to match, then we use the sequence number to decide.
456 // Compare the difference to support integer roll-over.
457 return (sequence_num - other.sequence_num) > 0;
initial.commit3f4a7322008-07-27 06:49:38 +0900458}
459
460//------------------------------------------------------------------------------
461// Method and data for histogramming events and actions taken by each instance
462// on each thread.
463
464// static
465bool MessageLoop::enable_histogrammer_ = false;
466
467// static
468void MessageLoop::EnableHistogrammer(bool enable) {
469 enable_histogrammer_ = enable;
470}
471
472void MessageLoop::StartHistogrammer() {
473 if (enable_histogrammer_ && !message_histogram_.get()
474 && StatisticsRecorder::WasStarted()) {
darin@google.com981f3552008-08-16 12:09:05 +0900475 DCHECK(!thread_name_.empty());
initial.commit3f4a7322008-07-27 06:49:38 +0900476 message_histogram_.reset(new LinearHistogram(
477 ASCIIToWide("MsgLoop:" + thread_name_).c_str(),
478 kLeastNonZeroMessageId,
479 kMaxMessageId,
480 kNumberOfDistinctMessagesDisplayed));
481 message_histogram_->SetFlags(message_histogram_->kHexRangePrintingFlag);
482 message_histogram_->SetRangeDescriptions(event_descriptions_);
483 }
484}
485
486void MessageLoop::HistogramEvent(int event) {
487 if (message_histogram_.get())
488 message_histogram_->Add(event);
489}
490
initial.commit3f4a7322008-07-27 06:49:38 +0900491// Provide a macro that takes an expression (such as a constant, or macro
492// constant) and creates a pair to initalize an array of pairs. In this case,
493// our pair consists of the expressions value, and the "stringized" version
494// of the expression (i.e., the exrpression put in quotes). For example, if
495// we have:
496// #define FOO 2
497// #define BAR 5
498// then the following:
499// VALUE_TO_NUMBER_AND_NAME(FOO + BAR)
500// will expand to:
501// {7, "FOO + BAR"}
502// We use the resulting array as an argument to our histogram, which reads the
503// number as a bucket identifier, and proceeds to use the corresponding name
504// in the pair (i.e., the quoted string) when printing out a histogram.
505#define VALUE_TO_NUMBER_AND_NAME(name) {name, #name},
506
initial.commit3f4a7322008-07-27 06:49:38 +0900507// static
508const LinearHistogram::DescriptionPair MessageLoop::event_descriptions_[] = {
initial.commit3f4a7322008-07-27 06:49:38 +0900509 // Provide some pretty print capability in our histogram for our internal
510 // messages.
511
initial.commit3f4a7322008-07-27 06:49:38 +0900512 // A few events we handle (kindred to messages), and used to profile actions.
513 VALUE_TO_NUMBER_AND_NAME(kTaskRunEvent)
initial.commit3f4a7322008-07-27 06:49:38 +0900514 VALUE_TO_NUMBER_AND_NAME(kTimerEvent)
515
516 {-1, NULL} // The list must be null terminated, per API to histogram.
517};
license.botf003cfe2008-08-24 09:55:55 +0900518
darin@google.comd936b5b2008-08-26 14:53:57 +0900519//------------------------------------------------------------------------------
520// MessageLoopForUI
521
522#if defined(OS_WIN)
523
524void MessageLoopForUI::Run(Dispatcher* dispatcher) {
525 AutoRunState save_state(this);
526 state_->dispatcher = dispatcher;
527 RunHandler();
528}
529
530void MessageLoopForUI::AddObserver(Observer* observer) {
531 pump_win()->AddObserver(observer);
532}
533
534void MessageLoopForUI::RemoveObserver(Observer* observer) {
535 pump_win()->RemoveObserver(observer);
536}
537
538void MessageLoopForUI::WillProcessMessage(const MSG& message) {
539 pump_win()->WillProcessMessage(message);
540}
541void MessageLoopForUI::DidProcessMessage(const MSG& message) {
542 pump_win()->DidProcessMessage(message);
543}
544void MessageLoopForUI::PumpOutPendingPaintMessages() {
545 pump_win()->PumpOutPendingPaintMessages();
546}
547
548#endif // defined(OS_WIN)
549
550//------------------------------------------------------------------------------
551// MessageLoopForIO
552
553#if defined(OS_WIN)
554
555void MessageLoopForIO::WatchObject(HANDLE object, Watcher* watcher) {
556 pump_win()->WatchObject(object, watcher);
557}
558
559#endif // defined(OS_WIN)