blob: e90f0b2a549c64dd6b899f81930f6f4808e2f772 [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
mmentovai@google.comfa5f9932008-08-22 07:26:06 +09009#include "base/compiler_specific.h"
initial.commit3f4a7322008-07-27 06:49:38 +090010#include "base/logging.h"
darin@google.com12d40bb2008-08-20 03:36:23 +090011#include "base/message_pump_default.h"
initial.commit3f4a7322008-07-27 06:49:38 +090012#include "base/string_util.h"
13#include "base/thread_local_storage.h"
initial.commit3f4a7322008-07-27 06:49:38 +090014
15// a TLS index to the message loop for the current thread
16// Note that if we start doing complex stuff in other static initializers
17// this could cause problems.
evanm@google.comf26fd3a2008-08-21 07:54:52 +090018// TODO(evanm): this shouldn't rely on static initialization.
19// static
20TLSSlot MessageLoop::tls_index_;
initial.commit3f4a7322008-07-27 06:49:38 +090021
22//------------------------------------------------------------------------------
23
initial.commit3f4a7322008-07-27 06:49:38 +090024// Logical events for Histogram profiling. Run with -message-loop-histogrammer
25// to get an accounting of messages and actions taken on each thread.
darin@google.com981f3552008-08-16 12:09:05 +090026static const int kTaskRunEvent = 0x1;
27static const int kTimerEvent = 0x2;
initial.commit3f4a7322008-07-27 06:49:38 +090028
29// Provide range of message IDs for use in histogramming and debug display.
30static const int kLeastNonZeroMessageId = 1;
31static const int kMaxMessageId = 1099;
32static const int kNumberOfDistinctMessagesDisplayed = 1100;
33
34//------------------------------------------------------------------------------
35
darin@google.com981f3552008-08-16 12:09:05 +090036#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +090037
initial.commit3f4a7322008-07-27 06:49:38 +090038// Upon a SEH exception in this thread, it restores the original unhandled
39// exception filter.
40static int SEHFilter(LPTOP_LEVEL_EXCEPTION_FILTER old_filter) {
41 ::SetUnhandledExceptionFilter(old_filter);
42 return EXCEPTION_CONTINUE_SEARCH;
43}
44
45// Retrieves a pointer to the current unhandled exception filter. There
46// is no standalone getter method.
47static LPTOP_LEVEL_EXCEPTION_FILTER GetTopSEHFilter() {
48 LPTOP_LEVEL_EXCEPTION_FILTER top_filter = NULL;
49 top_filter = ::SetUnhandledExceptionFilter(0);
50 ::SetUnhandledExceptionFilter(top_filter);
51 return top_filter;
52}
53
darin@google.com981f3552008-08-16 12:09:05 +090054#endif // defined(OS_WIN)
55
initial.commit3f4a7322008-07-27 06:49:38 +090056//------------------------------------------------------------------------------
57
darin@google.comd936b5b2008-08-26 14:53:57 +090058MessageLoop::MessageLoop(Type type)
59 : type_(type),
darin@google.comee6fa722008-08-13 08:25:43 +090060 nestable_tasks_allowed_(true),
darin@google.com12d40bb2008-08-20 03:36:23 +090061 exception_restoration_(false),
darin@google.combe165ae2008-09-07 17:08:29 +090062 state_(NULL),
63 next_sequence_num_(0) {
darin@google.comd936b5b2008-08-26 14:53:57 +090064 DCHECK(!tls_index_.Get()) << "should only have one message loop per thread";
evanm@google.comf26fd3a2008-08-21 07:54:52 +090065 tls_index_.Set(this);
darin@google.comd936b5b2008-08-26 14:53:57 +090066
darin@google.com132072e2008-08-26 15:52:41 +090067 // TODO(darin): Choose the pump based on the requested type.
darin@google.com981f3552008-08-16 12:09:05 +090068#if defined(OS_WIN)
darin@google.com0795f572008-08-30 09:22:48 +090069 if (type_ == TYPE_DEFAULT) {
70 pump_ = new base::MessagePumpDefault();
71 } else {
72 pump_ = new base::MessagePumpWin();
73 }
darin@google.com12d40bb2008-08-20 03:36:23 +090074#else
darin@google.com132072e2008-08-26 15:52:41 +090075 pump_ = new base::MessagePumpDefault();
darin@google.com981f3552008-08-16 12:09:05 +090076#endif
initial.commit3f4a7322008-07-27 06:49:38 +090077}
78
79MessageLoop::~MessageLoop() {
80 DCHECK(this == current());
darin@google.com965e5342008-08-06 08:16:41 +090081
82 // Let interested parties have one last shot at accessing this.
83 FOR_EACH_OBSERVER(DestructionObserver, destruction_observers_,
84 WillDestroyCurrentMessageLoop());
85
darin@google.com0e500502008-09-09 14:55:35 +090086 DCHECK(!state_);
87
darin@chromium.orge3af17f2008-09-10 09:37:07 +090088 // Clean up any unprocessed tasks, but take care: deleting a task could
89 // result in the addition of more tasks (e.g., via DeleteSoon). We set a
90 // limit on the number of times we will allow a deleted task to generate more
91 // tasks. Normally, we should only pass through this loop once or twice. If
92 // we end up hitting the loop limit, then it is probably due to one task that
93 // is being stubborn. Inspect the queues to see who is left.
94 bool did_work;
95 for (int i = 0; i < 100; ++i) {
96 DeletePendingTasks();
97 ReloadWorkQueue();
98 // If we end up with empty queues, then break out of the loop.
99 did_work = DeletePendingTasks();
100 if (!did_work)
101 break;
darin@google.com0e500502008-09-09 14:55:35 +0900102 }
darin@chromium.orge3af17f2008-09-10 09:37:07 +0900103 DCHECK(!did_work);
104
105 // OK, now make it so that no one can find us.
106 tls_index_.Set(NULL);
initial.commit3f4a7322008-07-27 06:49:38 +0900107}
108
darin@google.com965e5342008-08-06 08:16:41 +0900109void MessageLoop::AddDestructionObserver(DestructionObserver *obs) {
110 DCHECK(this == current());
111 destruction_observers_.AddObserver(obs);
112}
113
114void MessageLoop::RemoveDestructionObserver(DestructionObserver *obs) {
115 DCHECK(this == current());
116 destruction_observers_.RemoveObserver(obs);
117}
118
darin@google.com6ddeb842008-08-15 16:31:20 +0900119void MessageLoop::Run() {
darin@google.com981f3552008-08-16 12:09:05 +0900120 AutoRunState save_state(this);
121 RunHandler();
darin@google.com6ddeb842008-08-15 16:31:20 +0900122}
123
jar@google.com9239e022008-07-31 22:10:20 +0900124void MessageLoop::RunAllPending() {
darin@google.com981f3552008-08-16 12:09:05 +0900125 AutoRunState save_state(this);
126 state_->quit_received = true; // Means run until we would otherwise block.
127 RunHandler();
initial.commit3f4a7322008-07-27 06:49:38 +0900128}
129
130// Runs the loop in two different SEH modes:
131// enable_SEH_restoration_ = false : any unhandled exception goes to the last
132// one that calls SetUnhandledExceptionFilter().
133// enable_SEH_restoration_ = true : any unhandled exception goes to the filter
134// that was existed before the loop was run.
darin@google.com981f3552008-08-16 12:09:05 +0900135void MessageLoop::RunHandler() {
136#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900137 if (exception_restoration_) {
138 LPTOP_LEVEL_EXCEPTION_FILTER current_filter = GetTopSEHFilter();
139 __try {
darin@google.com981f3552008-08-16 12:09:05 +0900140 RunInternal();
initial.commit3f4a7322008-07-27 06:49:38 +0900141 } __except(SEHFilter(current_filter)) {
142 }
darin@google.com981f3552008-08-16 12:09:05 +0900143 return;
initial.commit3f4a7322008-07-27 06:49:38 +0900144 }
darin@google.com981f3552008-08-16 12:09:05 +0900145#endif
146
147 RunInternal();
initial.commit3f4a7322008-07-27 06:49:38 +0900148}
149
150//------------------------------------------------------------------------------
initial.commit3f4a7322008-07-27 06:49:38 +0900151
darin@google.com981f3552008-08-16 12:09:05 +0900152void MessageLoop::RunInternal() {
153 DCHECK(this == current());
154
initial.commit3f4a7322008-07-27 06:49:38 +0900155 StartHistogrammer();
156
darin@google.com981f3552008-08-16 12:09:05 +0900157#if defined(OS_WIN)
158 if (state_->dispatcher) {
159 pump_win()->RunWithDispatcher(this, state_->dispatcher);
160 return;
jar@google.com9239e022008-07-31 22:10:20 +0900161 }
darin@google.com981f3552008-08-16 12:09:05 +0900162#endif
163
164 pump_->Run(this);
jar@google.comfbaaf692008-07-30 16:50:53 +0900165}
jar@google.com7ff36e62008-07-30 15:58:56 +0900166
jar@google.comb4d1bff2008-07-31 04:03:59 +0900167//------------------------------------------------------------------------------
168// Wrapper functions for use in above message loop framework.
169
initial.commit3f4a7322008-07-27 06:49:38 +0900170bool MessageLoop::ProcessNextDelayedNonNestableTask() {
darin@google.com981f3552008-08-16 12:09:05 +0900171 if (state_->run_depth != 1)
initial.commit3f4a7322008-07-27 06:49:38 +0900172 return false;
173
darin@google.combe165ae2008-09-07 17:08:29 +0900174 if (deferred_non_nestable_work_queue_.empty())
initial.commit3f4a7322008-07-27 06:49:38 +0900175 return false;
darin@google.combe165ae2008-09-07 17:08:29 +0900176
177 Task* task = deferred_non_nestable_work_queue_.front().task;
178 deferred_non_nestable_work_queue_.pop();
179
180 RunTask(task);
initial.commit3f4a7322008-07-27 06:49:38 +0900181 return true;
182}
183
initial.commit3f4a7322008-07-27 06:49:38 +0900184//------------------------------------------------------------------------------
185
186void MessageLoop::Quit() {
darin@google.com981f3552008-08-16 12:09:05 +0900187 DCHECK(current() == this);
188 if (state_) {
189 state_->quit_received = true;
190 } else {
191 NOTREACHED() << "Must be inside Run to call Quit";
initial.commit3f4a7322008-07-27 06:49:38 +0900192 }
initial.commit3f4a7322008-07-27 06:49:38 +0900193}
194
darin@google.combe165ae2008-09-07 17:08:29 +0900195void MessageLoop::PostTask(
196 const tracked_objects::Location& from_here, Task* task) {
197 PostTask_Helper(from_here, task, 0, true);
198}
199
200void MessageLoop::PostDelayedTask(
201 const tracked_objects::Location& from_here, Task* task, int delay_ms) {
202 PostTask_Helper(from_here, task, delay_ms, true);
203}
204
205void MessageLoop::PostNonNestableTask(
206 const tracked_objects::Location& from_here, Task* task) {
207 PostTask_Helper(from_here, task, 0, false);
208}
209
210void MessageLoop::PostNonNestableDelayedTask(
211 const tracked_objects::Location& from_here, Task* task, int delay_ms) {
212 PostTask_Helper(from_here, task, delay_ms, false);
213}
214
initial.commit3f4a7322008-07-27 06:49:38 +0900215// Possibly called on a background thread!
darin@google.combe165ae2008-09-07 17:08:29 +0900216void MessageLoop::PostTask_Helper(
217 const tracked_objects::Location& from_here, Task* task, int delay_ms,
218 bool nestable) {
initial.commit3f4a7322008-07-27 06:49:38 +0900219 task->SetBirthPlace(from_here);
darin@google.com0795f572008-08-30 09:22:48 +0900220
darin@google.combe165ae2008-09-07 17:08:29 +0900221 PendingTask pending_task(task, nestable);
darin@google.com0795f572008-08-30 09:22:48 +0900222
223 if (delay_ms > 0) {
darin@google.combe165ae2008-09-07 17:08:29 +0900224 pending_task.delayed_run_time =
darin@google.com0795f572008-08-30 09:22:48 +0900225 Time::Now() + TimeDelta::FromMilliseconds(delay_ms);
226 } else {
227 DCHECK(delay_ms == 0) << "delay should not be negative";
228 }
229
initial.commit3f4a7322008-07-27 06:49:38 +0900230 // Warning: Don't try to short-circuit, and handle this thread's tasks more
231 // directly, as it could starve handling of foreign threads. Put every task
232 // into this queue.
233
darin@google.com981f3552008-08-16 12:09:05 +0900234 scoped_refptr<base::MessagePump> pump;
initial.commit3f4a7322008-07-27 06:49:38 +0900235 {
darin@google.com981f3552008-08-16 12:09:05 +0900236 AutoLock locked(incoming_queue_lock_);
237
darin@google.combe165ae2008-09-07 17:08:29 +0900238 bool was_empty = incoming_queue_.empty();
239 incoming_queue_.push(pending_task);
initial.commit3f4a7322008-07-27 06:49:38 +0900240 if (!was_empty)
241 return; // Someone else should have started the sub-pump.
242
darin@google.com981f3552008-08-16 12:09:05 +0900243 pump = pump_;
darin@google.com6ddeb842008-08-15 16:31:20 +0900244 }
darin@google.com981f3552008-08-16 12:09:05 +0900245 // Since the incoming_queue_ may contain a task that destroys this message
246 // loop, we cannot exit incoming_queue_lock_ until we are done with |this|.
247 // We use a stack-based reference to the message pump so that we can call
248 // ScheduleWork outside of incoming_queue_lock_.
darin@google.com6ddeb842008-08-15 16:31:20 +0900249
darin@google.com981f3552008-08-16 12:09:05 +0900250 pump->ScheduleWork();
initial.commit3f4a7322008-07-27 06:49:38 +0900251}
252
253void MessageLoop::SetNestableTasksAllowed(bool allowed) {
mpcomplete@google.com989d5f82008-08-09 09:14:09 +0900254 if (nestable_tasks_allowed_ != allowed) {
255 nestable_tasks_allowed_ = allowed;
256 if (!nestable_tasks_allowed_)
257 return;
258 // Start the native pump if we are not already pumping.
darin@google.com981f3552008-08-16 12:09:05 +0900259 pump_->ScheduleWork();
mpcomplete@google.com989d5f82008-08-09 09:14:09 +0900260 }
initial.commit3f4a7322008-07-27 06:49:38 +0900261}
262
263bool MessageLoop::NestableTasksAllowed() const {
264 return nestable_tasks_allowed_;
265}
266
initial.commit3f4a7322008-07-27 06:49:38 +0900267//------------------------------------------------------------------------------
initial.commit3f4a7322008-07-27 06:49:38 +0900268
initial.commit3f4a7322008-07-27 06:49:38 +0900269void MessageLoop::RunTask(Task* task) {
initial.commit3f4a7322008-07-27 06:49:38 +0900270 DCHECK(nestable_tasks_allowed_);
271 // Execute the task and assume the worst: It is probably not reentrant.
272 nestable_tasks_allowed_ = false;
darin@google.combe165ae2008-09-07 17:08:29 +0900273
274 HistogramEvent(kTaskRunEvent);
275 task->Run();
276 delete task;
277
278 nestable_tasks_allowed_ = true;
initial.commit3f4a7322008-07-27 06:49:38 +0900279}
280
darin@google.combe165ae2008-09-07 17:08:29 +0900281bool MessageLoop::DeferOrRunPendingTask(const PendingTask& pending_task) {
282 if (pending_task.nestable || state_->run_depth == 1) {
283 RunTask(pending_task.task);
284 // Show that we ran a task (Note: a new one might arrive as a
285 // consequence!).
286 return true;
287 }
288
289 // We couldn't run the task now because we're in a nested message loop
290 // and the task isn't nestable.
291 deferred_non_nestable_work_queue_.push(pending_task);
292 return false;
initial.commit3f4a7322008-07-27 06:49:38 +0900293}
294
darin@chromium.orge3af17f2008-09-10 09:37:07 +0900295void MessageLoop::AddToDelayedWorkQueue(const PendingTask& pending_task) {
296 // Move to the delayed work queue. Initialize the sequence number
297 // before inserting into the delayed_work_queue_. The sequence number
298 // is used to faciliate FIFO sorting when two tasks have the same
299 // delayed_run_time value.
300 PendingTask new_pending_task(pending_task);
301 new_pending_task.sequence_num = next_sequence_num_++;
302 delayed_work_queue_.push(new_pending_task);
303}
304
initial.commit3f4a7322008-07-27 06:49:38 +0900305void MessageLoop::ReloadWorkQueue() {
306 // We can improve performance of our loading tasks from incoming_queue_ to
darin@google.com981f3552008-08-16 12:09:05 +0900307 // work_queue_ by waiting until the last minute (work_queue_ is empty) to
308 // load. That reduces the number of locks-per-task significantly when our
darin@google.combe165ae2008-09-07 17:08:29 +0900309 // queues get large.
310 if (!work_queue_.empty())
initial.commit3f4a7322008-07-27 06:49:38 +0900311 return; // Wait till we *really* need to lock and load.
312
313 // Acquire all we can from the inter-thread queue with one lock acquisition.
initial.commit3f4a7322008-07-27 06:49:38 +0900314 {
315 AutoLock lock(incoming_queue_lock_);
darin@google.combe165ae2008-09-07 17:08:29 +0900316 if (incoming_queue_.empty())
initial.commit3f4a7322008-07-27 06:49:38 +0900317 return;
darin@google.combe165ae2008-09-07 17:08:29 +0900318 std::swap(incoming_queue_, work_queue_);
319 DCHECK(incoming_queue_.empty());
initial.commit3f4a7322008-07-27 06:49:38 +0900320 }
321}
322
darin@chromium.orge3af17f2008-09-10 09:37:07 +0900323bool MessageLoop::DeletePendingTasks() {
324 bool did_work = !work_queue_.empty();
325 while (!work_queue_.empty()) {
326 PendingTask pending_task = work_queue_.front();
327 work_queue_.pop();
328 if (!pending_task.delayed_run_time.is_null()) {
329 // We want to delete delayed tasks in the same order in which they would
330 // normally be deleted in case of any funny dependencies between delayed
331 // tasks.
332 AddToDelayedWorkQueue(pending_task);
333 } else {
334 // TODO(darin): Delete all tasks once it is safe to do so.
335 //delete task;
336 }
initial.commit3f4a7322008-07-27 06:49:38 +0900337 }
darin@chromium.orge3af17f2008-09-10 09:37:07 +0900338 did_work |= !deferred_non_nestable_work_queue_.empty();
339 while (!deferred_non_nestable_work_queue_.empty()) {
darin@chromium.orge3af17f2008-09-10 09:37:07 +0900340 // TODO(darin): Delete all tasks once it is safe to do so.
darin@chromium.orgfea7a862008-09-10 10:16:17 +0900341 //Task* task = deferred_non_nestable_work_queue_.front().task;
342 deferred_non_nestable_work_queue_.pop();
darin@chromium.orge3af17f2008-09-10 09:37:07 +0900343 //delete task;
initial.commit3f4a7322008-07-27 06:49:38 +0900344 }
darin@chromium.orge3af17f2008-09-10 09:37:07 +0900345 did_work |= !delayed_work_queue_.empty();
346 while (!delayed_work_queue_.empty()) {
347 Task* task = delayed_work_queue_.top().task;
348 delayed_work_queue_.pop();
349 delete task;
350 }
351 return did_work;
initial.commit3f4a7322008-07-27 06:49:38 +0900352}
353
darin@google.com981f3552008-08-16 12:09:05 +0900354bool MessageLoop::DoWork() {
darin@google.combe165ae2008-09-07 17:08:29 +0900355 if (!nestable_tasks_allowed_) {
356 // Task can't be executed right now.
357 return false;
358 }
359
360 for (;;) {
361 ReloadWorkQueue();
362 if (work_queue_.empty())
363 break;
364
365 // Execute oldest task.
366 do {
367 PendingTask pending_task = work_queue_.front();
368 work_queue_.pop();
369 if (!pending_task.delayed_run_time.is_null()) {
370 bool was_empty = delayed_work_queue_.empty();
darin@chromium.orge3af17f2008-09-10 09:37:07 +0900371 AddToDelayedWorkQueue(pending_task);
darin@google.combe165ae2008-09-07 17:08:29 +0900372 if (was_empty) // We only schedule the next delayed work item.
373 pump_->ScheduleDelayedWork(pending_task.delayed_run_time);
374 } else {
375 if (DeferOrRunPendingTask(pending_task))
376 return true;
377 }
378 } while (!work_queue_.empty());
379 }
380
381 // Nothing happened.
382 return false;
darin@google.com981f3552008-08-16 12:09:05 +0900383}
384
darin@google.com6393bed2008-08-20 15:30:58 +0900385bool MessageLoop::DoDelayedWork(Time* next_delayed_work_time) {
darin@google.combe165ae2008-09-07 17:08:29 +0900386 if (!nestable_tasks_allowed_ || delayed_work_queue_.empty()) {
387 *next_delayed_work_time = Time();
388 return false;
389 }
390
391 if (delayed_work_queue_.top().delayed_run_time > Time::Now()) {
392 *next_delayed_work_time = delayed_work_queue_.top().delayed_run_time;
393 return false;
394 }
darin@google.com981f3552008-08-16 12:09:05 +0900395
darin@google.combe165ae2008-09-07 17:08:29 +0900396 PendingTask pending_task = delayed_work_queue_.top();
397 delayed_work_queue_.pop();
398
399 if (!delayed_work_queue_.empty())
400 *next_delayed_work_time = delayed_work_queue_.top().delayed_run_time;
darin@google.com981f3552008-08-16 12:09:05 +0900401
darin@google.combe165ae2008-09-07 17:08:29 +0900402 return DeferOrRunPendingTask(pending_task);
darin@google.com981f3552008-08-16 12:09:05 +0900403}
404
405bool MessageLoop::DoIdleWork() {
406 if (ProcessNextDelayedNonNestableTask())
407 return true;
408
409 if (state_->quit_received)
410 pump_->Quit();
411
412 return false;
413}
414
415//------------------------------------------------------------------------------
416// MessageLoop::AutoRunState
417
418MessageLoop::AutoRunState::AutoRunState(MessageLoop* loop) : loop_(loop) {
419 // Make the loop reference us.
420 previous_state_ = loop_->state_;
421 if (previous_state_) {
422 run_depth = previous_state_->run_depth + 1;
darin@google.com6ddeb842008-08-15 16:31:20 +0900423 } else {
darin@google.com981f3552008-08-16 12:09:05 +0900424 run_depth = 1;
darin@google.com6ddeb842008-08-15 16:31:20 +0900425 }
darin@google.com981f3552008-08-16 12:09:05 +0900426 loop_->state_ = this;
427
428 // Initialize the other fields:
429 quit_received = false;
430#if defined(OS_WIN)
431 dispatcher = NULL;
432#endif
433}
434
435MessageLoop::AutoRunState::~AutoRunState() {
436 loop_->state_ = previous_state_;
darin@google.comee6fa722008-08-13 08:25:43 +0900437}
438
initial.commit3f4a7322008-07-27 06:49:38 +0900439//------------------------------------------------------------------------------
darin@google.combe165ae2008-09-07 17:08:29 +0900440// MessageLoop::PendingTask
initial.commit3f4a7322008-07-27 06:49:38 +0900441
darin@google.combe165ae2008-09-07 17:08:29 +0900442bool MessageLoop::PendingTask::operator<(const PendingTask& other) const {
443 // Since the top of a priority queue is defined as the "greatest" element, we
444 // need to invert the comparison here. We want the smaller time to be at the
445 // top of the heap.
initial.commit3f4a7322008-07-27 06:49:38 +0900446
darin@google.combe165ae2008-09-07 17:08:29 +0900447 if (delayed_run_time < other.delayed_run_time)
448 return false;
initial.commit3f4a7322008-07-27 06:49:38 +0900449
darin@google.combe165ae2008-09-07 17:08:29 +0900450 if (delayed_run_time > other.delayed_run_time)
451 return true;
initial.commit3f4a7322008-07-27 06:49:38 +0900452
darin@google.combe165ae2008-09-07 17:08:29 +0900453 // If the times happen to match, then we use the sequence number to decide.
454 // Compare the difference to support integer roll-over.
455 return (sequence_num - other.sequence_num) > 0;
initial.commit3f4a7322008-07-27 06:49:38 +0900456}
457
458//------------------------------------------------------------------------------
459// Method and data for histogramming events and actions taken by each instance
460// on each thread.
461
462// static
463bool MessageLoop::enable_histogrammer_ = false;
464
465// static
466void MessageLoop::EnableHistogrammer(bool enable) {
467 enable_histogrammer_ = enable;
468}
469
470void MessageLoop::StartHistogrammer() {
471 if (enable_histogrammer_ && !message_histogram_.get()
472 && StatisticsRecorder::WasStarted()) {
darin@google.com981f3552008-08-16 12:09:05 +0900473 DCHECK(!thread_name_.empty());
initial.commit3f4a7322008-07-27 06:49:38 +0900474 message_histogram_.reset(new LinearHistogram(
475 ASCIIToWide("MsgLoop:" + thread_name_).c_str(),
476 kLeastNonZeroMessageId,
477 kMaxMessageId,
478 kNumberOfDistinctMessagesDisplayed));
479 message_histogram_->SetFlags(message_histogram_->kHexRangePrintingFlag);
480 message_histogram_->SetRangeDescriptions(event_descriptions_);
481 }
482}
483
484void MessageLoop::HistogramEvent(int event) {
485 if (message_histogram_.get())
486 message_histogram_->Add(event);
487}
488
initial.commit3f4a7322008-07-27 06:49:38 +0900489// Provide a macro that takes an expression (such as a constant, or macro
490// constant) and creates a pair to initalize an array of pairs. In this case,
491// our pair consists of the expressions value, and the "stringized" version
492// of the expression (i.e., the exrpression put in quotes). For example, if
493// we have:
494// #define FOO 2
495// #define BAR 5
496// then the following:
497// VALUE_TO_NUMBER_AND_NAME(FOO + BAR)
498// will expand to:
499// {7, "FOO + BAR"}
500// We use the resulting array as an argument to our histogram, which reads the
501// number as a bucket identifier, and proceeds to use the corresponding name
502// in the pair (i.e., the quoted string) when printing out a histogram.
503#define VALUE_TO_NUMBER_AND_NAME(name) {name, #name},
504
initial.commit3f4a7322008-07-27 06:49:38 +0900505// static
506const LinearHistogram::DescriptionPair MessageLoop::event_descriptions_[] = {
initial.commit3f4a7322008-07-27 06:49:38 +0900507 // Provide some pretty print capability in our histogram for our internal
508 // messages.
509
initial.commit3f4a7322008-07-27 06:49:38 +0900510 // A few events we handle (kindred to messages), and used to profile actions.
511 VALUE_TO_NUMBER_AND_NAME(kTaskRunEvent)
initial.commit3f4a7322008-07-27 06:49:38 +0900512 VALUE_TO_NUMBER_AND_NAME(kTimerEvent)
513
514 {-1, NULL} // The list must be null terminated, per API to histogram.
515};
license.botf003cfe2008-08-24 09:55:55 +0900516
darin@google.comd936b5b2008-08-26 14:53:57 +0900517//------------------------------------------------------------------------------
518// MessageLoopForUI
519
520#if defined(OS_WIN)
521
522void MessageLoopForUI::Run(Dispatcher* dispatcher) {
523 AutoRunState save_state(this);
524 state_->dispatcher = dispatcher;
525 RunHandler();
526}
527
528void MessageLoopForUI::AddObserver(Observer* observer) {
529 pump_win()->AddObserver(observer);
530}
531
532void MessageLoopForUI::RemoveObserver(Observer* observer) {
533 pump_win()->RemoveObserver(observer);
534}
535
536void MessageLoopForUI::WillProcessMessage(const MSG& message) {
537 pump_win()->WillProcessMessage(message);
538}
539void MessageLoopForUI::DidProcessMessage(const MSG& message) {
540 pump_win()->DidProcessMessage(message);
541}
542void MessageLoopForUI::PumpOutPendingPaintMessages() {
543 pump_win()->PumpOutPendingPaintMessages();
544}
545
546#endif // defined(OS_WIN)
547
548//------------------------------------------------------------------------------
549// MessageLoopForIO
550
551#if defined(OS_WIN)
552
553void MessageLoopForIO::WatchObject(HANDLE object, Watcher* watcher) {
554 pump_win()->WatchObject(object, watcher);
555}
556
557#endif // defined(OS_WIN)