blob: 4f6f2fd5da5ea4f3f33de3b7246a2a7382038fc2 [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.com981f3552008-08-16 12:09:05 +0900103 DCHECK(!state_);
darin@google.com965e5342008-08-06 08:16:41 +0900104
darin@google.com510f6062008-09-09 14:15:56 +0900105 // Clean up any unprocessed tasks, but take care: deleting a task could
106 // result in the addition of more tasks (e.g., via DeleteSoon). We set a
107 // limit on the number of times we will allow a deleted task to generate more
108 // tasks. Normally, we should only pass through this loop once or twice. If
109 // we end up hitting the loop limit, then it is probably due to one task that
110 // is being stubborn. Inspect the queues to see who is left.
111 bool did_work;
112 for (int i = 0; i < 100; ++i) {
113 DeletePendingTasks();
114 ReloadWorkQueue();
115 // If we end up with empty queues, then break out of the loop.
116 did_work = DeletePendingTasks();
117 if (!did_work)
118 break;
darin@google.comebe504d2008-09-07 17:52:31 +0900119 }
darin@google.com510f6062008-09-09 14:15:56 +0900120 DCHECK(!did_work);
121
122 // OK, now make it so that no one can find us.
123 tls_index_.Set(NULL);
darin@google.comebe504d2008-09-07 17:52:31 +0900124
darin@google.combe165ae2008-09-07 17:08:29 +0900125#if defined(OS_WIN)
126 // Match timeBeginPeriod() from construction.
127 timeEndPeriod(1);
128#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900129}
130
darin@google.com965e5342008-08-06 08:16:41 +0900131void MessageLoop::AddDestructionObserver(DestructionObserver *obs) {
132 DCHECK(this == current());
133 destruction_observers_.AddObserver(obs);
134}
135
136void MessageLoop::RemoveDestructionObserver(DestructionObserver *obs) {
137 DCHECK(this == current());
138 destruction_observers_.RemoveObserver(obs);
139}
140
darin@google.com6ddeb842008-08-15 16:31:20 +0900141void MessageLoop::Run() {
darin@google.com981f3552008-08-16 12:09:05 +0900142 AutoRunState save_state(this);
143 RunHandler();
darin@google.com6ddeb842008-08-15 16:31:20 +0900144}
145
jar@google.com9239e022008-07-31 22:10:20 +0900146void MessageLoop::RunAllPending() {
darin@google.com981f3552008-08-16 12:09:05 +0900147 AutoRunState save_state(this);
148 state_->quit_received = true; // Means run until we would otherwise block.
149 RunHandler();
initial.commit3f4a7322008-07-27 06:49:38 +0900150}
151
152// Runs the loop in two different SEH modes:
153// enable_SEH_restoration_ = false : any unhandled exception goes to the last
154// one that calls SetUnhandledExceptionFilter().
155// enable_SEH_restoration_ = true : any unhandled exception goes to the filter
156// that was existed before the loop was run.
darin@google.com981f3552008-08-16 12:09:05 +0900157void MessageLoop::RunHandler() {
158#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900159 if (exception_restoration_) {
160 LPTOP_LEVEL_EXCEPTION_FILTER current_filter = GetTopSEHFilter();
161 __try {
darin@google.com981f3552008-08-16 12:09:05 +0900162 RunInternal();
initial.commit3f4a7322008-07-27 06:49:38 +0900163 } __except(SEHFilter(current_filter)) {
164 }
darin@google.com981f3552008-08-16 12:09:05 +0900165 return;
initial.commit3f4a7322008-07-27 06:49:38 +0900166 }
darin@google.com981f3552008-08-16 12:09:05 +0900167#endif
168
169 RunInternal();
initial.commit3f4a7322008-07-27 06:49:38 +0900170}
171
172//------------------------------------------------------------------------------
initial.commit3f4a7322008-07-27 06:49:38 +0900173
darin@google.com981f3552008-08-16 12:09:05 +0900174void MessageLoop::RunInternal() {
175 DCHECK(this == current());
176
initial.commit3f4a7322008-07-27 06:49:38 +0900177 StartHistogrammer();
178
darin@google.com981f3552008-08-16 12:09:05 +0900179#if defined(OS_WIN)
180 if (state_->dispatcher) {
181 pump_win()->RunWithDispatcher(this, state_->dispatcher);
182 return;
jar@google.com9239e022008-07-31 22:10:20 +0900183 }
darin@google.com981f3552008-08-16 12:09:05 +0900184#endif
185
186 pump_->Run(this);
jar@google.comfbaaf692008-07-30 16:50:53 +0900187}
jar@google.com7ff36e62008-07-30 15:58:56 +0900188
jar@google.comb4d1bff2008-07-31 04:03:59 +0900189//------------------------------------------------------------------------------
190// Wrapper functions for use in above message loop framework.
191
initial.commit3f4a7322008-07-27 06:49:38 +0900192bool MessageLoop::ProcessNextDelayedNonNestableTask() {
darin@google.com981f3552008-08-16 12:09:05 +0900193 if (state_->run_depth != 1)
initial.commit3f4a7322008-07-27 06:49:38 +0900194 return false;
195
darin@google.combe165ae2008-09-07 17:08:29 +0900196 if (deferred_non_nestable_work_queue_.empty())
initial.commit3f4a7322008-07-27 06:49:38 +0900197 return false;
darin@google.combe165ae2008-09-07 17:08:29 +0900198
199 Task* task = deferred_non_nestable_work_queue_.front().task;
200 deferred_non_nestable_work_queue_.pop();
201
202 RunTask(task);
initial.commit3f4a7322008-07-27 06:49:38 +0900203 return true;
204}
205
initial.commit3f4a7322008-07-27 06:49:38 +0900206//------------------------------------------------------------------------------
207
208void MessageLoop::Quit() {
darin@google.com981f3552008-08-16 12:09:05 +0900209 DCHECK(current() == this);
210 if (state_) {
211 state_->quit_received = true;
212 } else {
213 NOTREACHED() << "Must be inside Run to call Quit";
initial.commit3f4a7322008-07-27 06:49:38 +0900214 }
initial.commit3f4a7322008-07-27 06:49:38 +0900215}
216
darin@google.combe165ae2008-09-07 17:08:29 +0900217void MessageLoop::PostTask(
218 const tracked_objects::Location& from_here, Task* task) {
219 PostTask_Helper(from_here, task, 0, true);
220}
221
222void MessageLoop::PostDelayedTask(
223 const tracked_objects::Location& from_here, Task* task, int delay_ms) {
224 PostTask_Helper(from_here, task, delay_ms, true);
225}
226
227void MessageLoop::PostNonNestableTask(
228 const tracked_objects::Location& from_here, Task* task) {
229 PostTask_Helper(from_here, task, 0, false);
230}
231
232void MessageLoop::PostNonNestableDelayedTask(
233 const tracked_objects::Location& from_here, Task* task, int delay_ms) {
234 PostTask_Helper(from_here, task, delay_ms, false);
235}
236
initial.commit3f4a7322008-07-27 06:49:38 +0900237// Possibly called on a background thread!
darin@google.combe165ae2008-09-07 17:08:29 +0900238void MessageLoop::PostTask_Helper(
239 const tracked_objects::Location& from_here, Task* task, int delay_ms,
240 bool nestable) {
initial.commit3f4a7322008-07-27 06:49:38 +0900241 task->SetBirthPlace(from_here);
darin@google.com0795f572008-08-30 09:22:48 +0900242
darin@google.combe165ae2008-09-07 17:08:29 +0900243 PendingTask pending_task(task, nestable);
darin@google.com0795f572008-08-30 09:22:48 +0900244
245 if (delay_ms > 0) {
darin@google.combe165ae2008-09-07 17:08:29 +0900246 pending_task.delayed_run_time =
darin@google.com0795f572008-08-30 09:22:48 +0900247 Time::Now() + TimeDelta::FromMilliseconds(delay_ms);
248 } else {
249 DCHECK(delay_ms == 0) << "delay should not be negative";
250 }
251
initial.commit3f4a7322008-07-27 06:49:38 +0900252 // Warning: Don't try to short-circuit, and handle this thread's tasks more
253 // directly, as it could starve handling of foreign threads. Put every task
254 // into this queue.
255
darin@google.com981f3552008-08-16 12:09:05 +0900256 scoped_refptr<base::MessagePump> pump;
initial.commit3f4a7322008-07-27 06:49:38 +0900257 {
darin@google.com981f3552008-08-16 12:09:05 +0900258 AutoLock locked(incoming_queue_lock_);
259
darin@google.combe165ae2008-09-07 17:08:29 +0900260 bool was_empty = incoming_queue_.empty();
261 incoming_queue_.push(pending_task);
initial.commit3f4a7322008-07-27 06:49:38 +0900262 if (!was_empty)
263 return; // Someone else should have started the sub-pump.
264
darin@google.com981f3552008-08-16 12:09:05 +0900265 pump = pump_;
darin@google.com6ddeb842008-08-15 16:31:20 +0900266 }
darin@google.com981f3552008-08-16 12:09:05 +0900267 // Since the incoming_queue_ may contain a task that destroys this message
268 // loop, we cannot exit incoming_queue_lock_ until we are done with |this|.
269 // We use a stack-based reference to the message pump so that we can call
270 // ScheduleWork outside of incoming_queue_lock_.
darin@google.com6ddeb842008-08-15 16:31:20 +0900271
darin@google.com981f3552008-08-16 12:09:05 +0900272 pump->ScheduleWork();
initial.commit3f4a7322008-07-27 06:49:38 +0900273}
274
275void MessageLoop::SetNestableTasksAllowed(bool allowed) {
mpcomplete@google.com989d5f82008-08-09 09:14:09 +0900276 if (nestable_tasks_allowed_ != allowed) {
277 nestable_tasks_allowed_ = allowed;
278 if (!nestable_tasks_allowed_)
279 return;
280 // Start the native pump if we are not already pumping.
darin@google.com981f3552008-08-16 12:09:05 +0900281 pump_->ScheduleWork();
mpcomplete@google.com989d5f82008-08-09 09:14:09 +0900282 }
initial.commit3f4a7322008-07-27 06:49:38 +0900283}
284
285bool MessageLoop::NestableTasksAllowed() const {
286 return nestable_tasks_allowed_;
287}
288
initial.commit3f4a7322008-07-27 06:49:38 +0900289//------------------------------------------------------------------------------
initial.commit3f4a7322008-07-27 06:49:38 +0900290
initial.commit3f4a7322008-07-27 06:49:38 +0900291void MessageLoop::RunTask(Task* task) {
initial.commit3f4a7322008-07-27 06:49:38 +0900292 DCHECK(nestable_tasks_allowed_);
293 // Execute the task and assume the worst: It is probably not reentrant.
294 nestable_tasks_allowed_ = false;
darin@google.combe165ae2008-09-07 17:08:29 +0900295
296 HistogramEvent(kTaskRunEvent);
297 task->Run();
298 delete task;
299
300 nestable_tasks_allowed_ = true;
initial.commit3f4a7322008-07-27 06:49:38 +0900301}
302
darin@google.combe165ae2008-09-07 17:08:29 +0900303bool MessageLoop::DeferOrRunPendingTask(const PendingTask& pending_task) {
304 if (pending_task.nestable || state_->run_depth == 1) {
305 RunTask(pending_task.task);
306 // Show that we ran a task (Note: a new one might arrive as a
307 // consequence!).
308 return true;
309 }
310
311 // We couldn't run the task now because we're in a nested message loop
312 // and the task isn't nestable.
313 deferred_non_nestable_work_queue_.push(pending_task);
314 return false;
initial.commit3f4a7322008-07-27 06:49:38 +0900315}
316
initial.commit3f4a7322008-07-27 06:49:38 +0900317void MessageLoop::ReloadWorkQueue() {
318 // We can improve performance of our loading tasks from incoming_queue_ to
darin@google.com981f3552008-08-16 12:09:05 +0900319 // work_queue_ by waiting until the last minute (work_queue_ is empty) to
320 // load. That reduces the number of locks-per-task significantly when our
darin@google.combe165ae2008-09-07 17:08:29 +0900321 // queues get large.
322 if (!work_queue_.empty())
initial.commit3f4a7322008-07-27 06:49:38 +0900323 return; // Wait till we *really* need to lock and load.
324
325 // Acquire all we can from the inter-thread queue with one lock acquisition.
initial.commit3f4a7322008-07-27 06:49:38 +0900326 {
327 AutoLock lock(incoming_queue_lock_);
darin@google.combe165ae2008-09-07 17:08:29 +0900328 if (incoming_queue_.empty())
initial.commit3f4a7322008-07-27 06:49:38 +0900329 return;
darin@google.combe165ae2008-09-07 17:08:29 +0900330 std::swap(incoming_queue_, work_queue_);
331 DCHECK(incoming_queue_.empty());
initial.commit3f4a7322008-07-27 06:49:38 +0900332 }
333}
334
darin@google.com510f6062008-09-09 14:15:56 +0900335bool MessageLoop::DeletePendingTasks() {
336 bool did_work = !work_queue_.empty();
337 while (!work_queue_.empty()) {
338 Task* task = work_queue_.front().task;
339 work_queue_.pop();
340 delete task;
initial.commit3f4a7322008-07-27 06:49:38 +0900341 }
darin@google.com510f6062008-09-09 14:15:56 +0900342 did_work |= !deferred_non_nestable_work_queue_.empty();
343 while (!deferred_non_nestable_work_queue_.empty()) {
344 Task* task = deferred_non_nestable_work_queue_.front().task;
345 deferred_non_nestable_work_queue_.pop();
346 delete task;
initial.commit3f4a7322008-07-27 06:49:38 +0900347 }
darin@google.com510f6062008-09-09 14:15:56 +0900348 did_work |= !delayed_work_queue_.empty();
349 while (!delayed_work_queue_.empty()) {
350 Task* task = delayed_work_queue_.top().task;
351 delayed_work_queue_.pop();
352 delete task;
353 }
354 return did_work;
initial.commit3f4a7322008-07-27 06:49:38 +0900355}
356
darin@google.com981f3552008-08-16 12:09:05 +0900357bool MessageLoop::DoWork() {
darin@google.combe165ae2008-09-07 17:08:29 +0900358 if (!nestable_tasks_allowed_) {
359 // Task can't be executed right now.
360 return false;
361 }
362
363 for (;;) {
364 ReloadWorkQueue();
365 if (work_queue_.empty())
366 break;
367
368 // Execute oldest task.
369 do {
370 PendingTask pending_task = work_queue_.front();
371 work_queue_.pop();
372 if (!pending_task.delayed_run_time.is_null()) {
373 bool was_empty = delayed_work_queue_.empty();
374
375 // Move to the delayed work queue. Initialize the sequence number
376 // before inserting into the delayed_work_queue_. The sequence number
377 // is used to faciliate FIFO sorting when two tasks have the same
378 // delayed_run_time value.
379 pending_task.sequence_num = next_sequence_num_++;
380 delayed_work_queue_.push(pending_task);
381
382 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;
darin@google.com981f3552008-08-16 12:09:05 +0900393}
394
darin@google.com6393bed2008-08-20 15:30:58 +0900395bool MessageLoop::DoDelayedWork(Time* next_delayed_work_time) {
darin@google.combe165ae2008-09-07 17:08:29 +0900396 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 }
darin@google.com981f3552008-08-16 12:09:05 +0900405
darin@google.combe165ae2008-09-07 17:08:29 +0900406 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;
darin@google.com981f3552008-08-16 12:09:05 +0900411
darin@google.combe165ae2008-09-07 17:08:29 +0900412 return DeferOrRunPendingTask(pending_task);
darin@google.com981f3552008-08-16 12:09:05 +0900413}
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;
darin@google.com6ddeb842008-08-15 16:31:20 +0900433 } else {
darin@google.com981f3552008-08-16 12:09:05 +0900434 run_depth = 1;
darin@google.com6ddeb842008-08-15 16:31:20 +0900435 }
darin@google.com981f3552008-08-16 12:09:05 +0900436 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_;
darin@google.comee6fa722008-08-13 08:25:43 +0900447}
448
initial.commit3f4a7322008-07-27 06:49:38 +0900449//------------------------------------------------------------------------------
darin@google.combe165ae2008-09-07 17:08:29 +0900450// MessageLoop::PendingTask
initial.commit3f4a7322008-07-27 06:49:38 +0900451
darin@google.combe165ae2008-09-07 17:08:29 +0900452bool 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.commit3f4a7322008-07-27 06:49:38 +0900456
darin@google.combe165ae2008-09-07 17:08:29 +0900457 if (delayed_run_time < other.delayed_run_time)
458 return false;
initial.commit3f4a7322008-07-27 06:49:38 +0900459
darin@google.combe165ae2008-09-07 17:08:29 +0900460 if (delayed_run_time > other.delayed_run_time)
461 return true;
initial.commit3f4a7322008-07-27 06:49:38 +0900462
darin@google.combe165ae2008-09-07 17:08:29 +0900463 // 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.commit3f4a7322008-07-27 06:49:38 +0900466}
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()) {
darin@google.com981f3552008-08-16 12:09:05 +0900483 DCHECK(!thread_name_.empty());
initial.commit3f4a7322008-07-27 06:49:38 +0900484 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.commit3f4a7322008-07-27 06:49:38 +0900499// 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.commit3f4a7322008-07-27 06:49:38 +0900515// static
516const LinearHistogram::DescriptionPair MessageLoop::event_descriptions_[] = {
initial.commit3f4a7322008-07-27 06:49:38 +0900517 // Provide some pretty print capability in our histogram for our internal
518 // messages.
519
initial.commit3f4a7322008-07-27 06:49:38 +0900520 // A few events we handle (kindred to messages), and used to profile actions.
521 VALUE_TO_NUMBER_AND_NAME(kTaskRunEvent)
initial.commit3f4a7322008-07-27 06:49:38 +0900522 VALUE_TO_NUMBER_AND_NAME(kTimerEvent)
523
524 {-1, NULL} // The list must be null terminated, per API to histogram.
525};
license.botf003cfe2008-08-24 09:55:55 +0900526
darin@google.comd936b5b2008-08-26 14:53:57 +0900527//------------------------------------------------------------------------------
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)