blob: 4b955320010dcb53210b73af3ea5236ed61f1701 [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"
deanm@chromium.orgcd1ce302008-09-10 19:54:06 +090010#include "base/lazy_instance.h"
initial.commit3f4a7322008-07-27 06:49:38 +090011#include "base/logging.h"
darin@google.com12d40bb2008-08-20 03:36:23 +090012#include "base/message_pump_default.h"
initial.commit3f4a7322008-07-27 06:49:38 +090013#include "base/string_util.h"
deanm@chromium.orgcd1ce302008-09-10 19:54:06 +090014#include "base/thread_local.h"
initial.commit3f4a7322008-07-27 06:49:38 +090015
deanm@chromium.orgcd1ce302008-09-10 19:54:06 +090016// A lazily created thread local storage for quick access to a thread's message
17// loop, if one exists. This should be safe and free of static constructors.
18static base::LazyInstance<base::ThreadLocalPointer<MessageLoop> > lazy_tls_ptr(
19 base::LINKER_INITIALIZED);
initial.commit3f4a7322008-07-27 06:49:38 +090020
21//------------------------------------------------------------------------------
22
initial.commit3f4a7322008-07-27 06:49:38 +090023// Logical events for Histogram profiling. Run with -message-loop-histogrammer
24// to get an accounting of messages and actions taken on each thread.
darin@google.com981f3552008-08-16 12:09:05 +090025static const int kTaskRunEvent = 0x1;
26static const int kTimerEvent = 0x2;
initial.commit3f4a7322008-07-27 06:49:38 +090027
28// Provide range of message IDs for use in histogramming and debug display.
29static const int kLeastNonZeroMessageId = 1;
30static const int kMaxMessageId = 1099;
31static const int kNumberOfDistinctMessagesDisplayed = 1100;
32
33//------------------------------------------------------------------------------
34
darin@google.com981f3552008-08-16 12:09:05 +090035#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +090036
initial.commit3f4a7322008-07-27 06:49:38 +090037// Upon a SEH exception in this thread, it restores the original unhandled
38// exception filter.
39static int SEHFilter(LPTOP_LEVEL_EXCEPTION_FILTER old_filter) {
40 ::SetUnhandledExceptionFilter(old_filter);
41 return EXCEPTION_CONTINUE_SEARCH;
42}
43
44// Retrieves a pointer to the current unhandled exception filter. There
45// is no standalone getter method.
46static LPTOP_LEVEL_EXCEPTION_FILTER GetTopSEHFilter() {
47 LPTOP_LEVEL_EXCEPTION_FILTER top_filter = NULL;
48 top_filter = ::SetUnhandledExceptionFilter(0);
49 ::SetUnhandledExceptionFilter(top_filter);
50 return top_filter;
51}
52
darin@google.com981f3552008-08-16 12:09:05 +090053#endif // defined(OS_WIN)
54
initial.commit3f4a7322008-07-27 06:49:38 +090055//------------------------------------------------------------------------------
56
deanm@chromium.orgcd1ce302008-09-10 19:54:06 +090057// static
58MessageLoop* MessageLoop::current() {
59 // TODO(darin): sadly, we cannot enable this yet since people call us even
60 // when they have no intention of using us.
61 //DCHECK(loop) << "Ouch, did you forget to initialize me?";
62 return lazy_tls_ptr.Pointer()->Get();
63}
64
darin@google.comd936b5b2008-08-26 14:53:57 +090065MessageLoop::MessageLoop(Type type)
66 : type_(type),
darin@google.comee6fa722008-08-13 08:25:43 +090067 nestable_tasks_allowed_(true),
darin@google.com12d40bb2008-08-20 03:36:23 +090068 exception_restoration_(false),
darin@google.combe165ae2008-09-07 17:08:29 +090069 state_(NULL),
70 next_sequence_num_(0) {
deanm@chromium.orgcd1ce302008-09-10 19:54:06 +090071 DCHECK(!current()) << "should only have one message loop per thread";
72 lazy_tls_ptr.Pointer()->Set(this);
darin@google.comd936b5b2008-08-26 14:53:57 +090073
darin@google.com132072e2008-08-26 15:52:41 +090074 // TODO(darin): Choose the pump based on the requested type.
darin@google.com981f3552008-08-16 12:09:05 +090075#if defined(OS_WIN)
darin@google.com0795f572008-08-30 09:22:48 +090076 if (type_ == TYPE_DEFAULT) {
77 pump_ = new base::MessagePumpDefault();
78 } else {
79 pump_ = new base::MessagePumpWin();
80 }
darin@google.com12d40bb2008-08-20 03:36:23 +090081#else
darin@google.com132072e2008-08-26 15:52:41 +090082 pump_ = new base::MessagePumpDefault();
darin@google.com981f3552008-08-16 12:09:05 +090083#endif
initial.commit3f4a7322008-07-27 06:49:38 +090084}
85
86MessageLoop::~MessageLoop() {
87 DCHECK(this == current());
darin@google.com965e5342008-08-06 08:16:41 +090088
89 // Let interested parties have one last shot at accessing this.
90 FOR_EACH_OBSERVER(DestructionObserver, destruction_observers_,
91 WillDestroyCurrentMessageLoop());
92
darin@google.com0e500502008-09-09 14:55:35 +090093 DCHECK(!state_);
94
darin@chromium.orge3af17f2008-09-10 09:37:07 +090095 // Clean up any unprocessed tasks, but take care: deleting a task could
96 // result in the addition of more tasks (e.g., via DeleteSoon). We set a
97 // limit on the number of times we will allow a deleted task to generate more
98 // tasks. Normally, we should only pass through this loop once or twice. If
99 // we end up hitting the loop limit, then it is probably due to one task that
100 // is being stubborn. Inspect the queues to see who is left.
101 bool did_work;
102 for (int i = 0; i < 100; ++i) {
103 DeletePendingTasks();
104 ReloadWorkQueue();
105 // If we end up with empty queues, then break out of the loop.
106 did_work = DeletePendingTasks();
107 if (!did_work)
108 break;
darin@google.com0e500502008-09-09 14:55:35 +0900109 }
darin@chromium.orge3af17f2008-09-10 09:37:07 +0900110 DCHECK(!did_work);
111
112 // OK, now make it so that no one can find us.
deanm@chromium.orge4cc5922008-09-10 20:14:56 +0900113 lazy_tls_ptr.Pointer()->Set(NULL);
initial.commit3f4a7322008-07-27 06:49:38 +0900114}
115
darin@google.com965e5342008-08-06 08:16:41 +0900116void MessageLoop::AddDestructionObserver(DestructionObserver *obs) {
117 DCHECK(this == current());
118 destruction_observers_.AddObserver(obs);
119}
120
121void MessageLoop::RemoveDestructionObserver(DestructionObserver *obs) {
122 DCHECK(this == current());
123 destruction_observers_.RemoveObserver(obs);
124}
125
darin@google.com6ddeb842008-08-15 16:31:20 +0900126void MessageLoop::Run() {
darin@google.com981f3552008-08-16 12:09:05 +0900127 AutoRunState save_state(this);
128 RunHandler();
darin@google.com6ddeb842008-08-15 16:31:20 +0900129}
130
jar@google.com9239e022008-07-31 22:10:20 +0900131void MessageLoop::RunAllPending() {
darin@google.com981f3552008-08-16 12:09:05 +0900132 AutoRunState save_state(this);
133 state_->quit_received = true; // Means run until we would otherwise block.
134 RunHandler();
initial.commit3f4a7322008-07-27 06:49:38 +0900135}
136
137// Runs the loop in two different SEH modes:
138// enable_SEH_restoration_ = false : any unhandled exception goes to the last
139// one that calls SetUnhandledExceptionFilter().
140// enable_SEH_restoration_ = true : any unhandled exception goes to the filter
141// that was existed before the loop was run.
darin@google.com981f3552008-08-16 12:09:05 +0900142void MessageLoop::RunHandler() {
143#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900144 if (exception_restoration_) {
145 LPTOP_LEVEL_EXCEPTION_FILTER current_filter = GetTopSEHFilter();
146 __try {
darin@google.com981f3552008-08-16 12:09:05 +0900147 RunInternal();
initial.commit3f4a7322008-07-27 06:49:38 +0900148 } __except(SEHFilter(current_filter)) {
149 }
darin@google.com981f3552008-08-16 12:09:05 +0900150 return;
initial.commit3f4a7322008-07-27 06:49:38 +0900151 }
darin@google.com981f3552008-08-16 12:09:05 +0900152#endif
153
154 RunInternal();
initial.commit3f4a7322008-07-27 06:49:38 +0900155}
156
157//------------------------------------------------------------------------------
initial.commit3f4a7322008-07-27 06:49:38 +0900158
darin@google.com981f3552008-08-16 12:09:05 +0900159void MessageLoop::RunInternal() {
160 DCHECK(this == current());
161
initial.commit3f4a7322008-07-27 06:49:38 +0900162 StartHistogrammer();
163
darin@google.com981f3552008-08-16 12:09:05 +0900164#if defined(OS_WIN)
165 if (state_->dispatcher) {
166 pump_win()->RunWithDispatcher(this, state_->dispatcher);
167 return;
jar@google.com9239e022008-07-31 22:10:20 +0900168 }
darin@google.com981f3552008-08-16 12:09:05 +0900169#endif
170
171 pump_->Run(this);
jar@google.comfbaaf692008-07-30 16:50:53 +0900172}
jar@google.com7ff36e62008-07-30 15:58:56 +0900173
jar@google.comb4d1bff2008-07-31 04:03:59 +0900174//------------------------------------------------------------------------------
175// Wrapper functions for use in above message loop framework.
176
initial.commit3f4a7322008-07-27 06:49:38 +0900177bool MessageLoop::ProcessNextDelayedNonNestableTask() {
darin@google.com981f3552008-08-16 12:09:05 +0900178 if (state_->run_depth != 1)
initial.commit3f4a7322008-07-27 06:49:38 +0900179 return false;
180
darin@google.combe165ae2008-09-07 17:08:29 +0900181 if (deferred_non_nestable_work_queue_.empty())
initial.commit3f4a7322008-07-27 06:49:38 +0900182 return false;
darin@google.combe165ae2008-09-07 17:08:29 +0900183
184 Task* task = deferred_non_nestable_work_queue_.front().task;
185 deferred_non_nestable_work_queue_.pop();
186
187 RunTask(task);
initial.commit3f4a7322008-07-27 06:49:38 +0900188 return true;
189}
190
initial.commit3f4a7322008-07-27 06:49:38 +0900191//------------------------------------------------------------------------------
192
193void MessageLoop::Quit() {
darin@google.com981f3552008-08-16 12:09:05 +0900194 DCHECK(current() == this);
195 if (state_) {
196 state_->quit_received = true;
197 } else {
198 NOTREACHED() << "Must be inside Run to call Quit";
initial.commit3f4a7322008-07-27 06:49:38 +0900199 }
initial.commit3f4a7322008-07-27 06:49:38 +0900200}
201
darin@google.combe165ae2008-09-07 17:08:29 +0900202void MessageLoop::PostTask(
203 const tracked_objects::Location& from_here, Task* task) {
204 PostTask_Helper(from_here, task, 0, true);
205}
206
207void MessageLoop::PostDelayedTask(
208 const tracked_objects::Location& from_here, Task* task, int delay_ms) {
209 PostTask_Helper(from_here, task, delay_ms, true);
210}
211
212void MessageLoop::PostNonNestableTask(
213 const tracked_objects::Location& from_here, Task* task) {
214 PostTask_Helper(from_here, task, 0, false);
215}
216
217void MessageLoop::PostNonNestableDelayedTask(
218 const tracked_objects::Location& from_here, Task* task, int delay_ms) {
219 PostTask_Helper(from_here, task, delay_ms, false);
220}
221
initial.commit3f4a7322008-07-27 06:49:38 +0900222// Possibly called on a background thread!
darin@google.combe165ae2008-09-07 17:08:29 +0900223void MessageLoop::PostTask_Helper(
224 const tracked_objects::Location& from_here, Task* task, int delay_ms,
225 bool nestable) {
initial.commit3f4a7322008-07-27 06:49:38 +0900226 task->SetBirthPlace(from_here);
darin@google.com0795f572008-08-30 09:22:48 +0900227
darin@google.combe165ae2008-09-07 17:08:29 +0900228 PendingTask pending_task(task, nestable);
darin@google.com0795f572008-08-30 09:22:48 +0900229
230 if (delay_ms > 0) {
darin@google.combe165ae2008-09-07 17:08:29 +0900231 pending_task.delayed_run_time =
darin@google.com0795f572008-08-30 09:22:48 +0900232 Time::Now() + TimeDelta::FromMilliseconds(delay_ms);
233 } else {
234 DCHECK(delay_ms == 0) << "delay should not be negative";
235 }
236
initial.commit3f4a7322008-07-27 06:49:38 +0900237 // Warning: Don't try to short-circuit, and handle this thread's tasks more
238 // directly, as it could starve handling of foreign threads. Put every task
239 // into this queue.
240
darin@google.com981f3552008-08-16 12:09:05 +0900241 scoped_refptr<base::MessagePump> pump;
initial.commit3f4a7322008-07-27 06:49:38 +0900242 {
darin@google.com981f3552008-08-16 12:09:05 +0900243 AutoLock locked(incoming_queue_lock_);
244
darin@google.combe165ae2008-09-07 17:08:29 +0900245 bool was_empty = incoming_queue_.empty();
246 incoming_queue_.push(pending_task);
initial.commit3f4a7322008-07-27 06:49:38 +0900247 if (!was_empty)
248 return; // Someone else should have started the sub-pump.
249
darin@google.com981f3552008-08-16 12:09:05 +0900250 pump = pump_;
darin@google.com6ddeb842008-08-15 16:31:20 +0900251 }
darin@google.com981f3552008-08-16 12:09:05 +0900252 // Since the incoming_queue_ may contain a task that destroys this message
253 // loop, we cannot exit incoming_queue_lock_ until we are done with |this|.
254 // We use a stack-based reference to the message pump so that we can call
255 // ScheduleWork outside of incoming_queue_lock_.
darin@google.com6ddeb842008-08-15 16:31:20 +0900256
darin@google.com981f3552008-08-16 12:09:05 +0900257 pump->ScheduleWork();
initial.commit3f4a7322008-07-27 06:49:38 +0900258}
259
260void MessageLoop::SetNestableTasksAllowed(bool allowed) {
mpcomplete@google.com989d5f82008-08-09 09:14:09 +0900261 if (nestable_tasks_allowed_ != allowed) {
262 nestable_tasks_allowed_ = allowed;
263 if (!nestable_tasks_allowed_)
264 return;
265 // Start the native pump if we are not already pumping.
darin@google.com981f3552008-08-16 12:09:05 +0900266 pump_->ScheduleWork();
mpcomplete@google.com989d5f82008-08-09 09:14:09 +0900267 }
initial.commit3f4a7322008-07-27 06:49:38 +0900268}
269
270bool MessageLoop::NestableTasksAllowed() const {
271 return nestable_tasks_allowed_;
272}
273
initial.commit3f4a7322008-07-27 06:49:38 +0900274//------------------------------------------------------------------------------
initial.commit3f4a7322008-07-27 06:49:38 +0900275
initial.commit3f4a7322008-07-27 06:49:38 +0900276void MessageLoop::RunTask(Task* task) {
initial.commit3f4a7322008-07-27 06:49:38 +0900277 DCHECK(nestable_tasks_allowed_);
278 // Execute the task and assume the worst: It is probably not reentrant.
279 nestable_tasks_allowed_ = false;
darin@google.combe165ae2008-09-07 17:08:29 +0900280
281 HistogramEvent(kTaskRunEvent);
282 task->Run();
283 delete task;
284
285 nestable_tasks_allowed_ = true;
initial.commit3f4a7322008-07-27 06:49:38 +0900286}
287
darin@google.combe165ae2008-09-07 17:08:29 +0900288bool MessageLoop::DeferOrRunPendingTask(const PendingTask& pending_task) {
289 if (pending_task.nestable || state_->run_depth == 1) {
290 RunTask(pending_task.task);
291 // Show that we ran a task (Note: a new one might arrive as a
292 // consequence!).
293 return true;
294 }
295
296 // We couldn't run the task now because we're in a nested message loop
297 // and the task isn't nestable.
298 deferred_non_nestable_work_queue_.push(pending_task);
299 return false;
initial.commit3f4a7322008-07-27 06:49:38 +0900300}
301
darin@chromium.orge3af17f2008-09-10 09:37:07 +0900302void MessageLoop::AddToDelayedWorkQueue(const PendingTask& pending_task) {
303 // Move to the delayed work queue. Initialize the sequence number
304 // before inserting into the delayed_work_queue_. The sequence number
305 // is used to faciliate FIFO sorting when two tasks have the same
306 // delayed_run_time value.
307 PendingTask new_pending_task(pending_task);
308 new_pending_task.sequence_num = next_sequence_num_++;
309 delayed_work_queue_.push(new_pending_task);
310}
311
initial.commit3f4a7322008-07-27 06:49:38 +0900312void MessageLoop::ReloadWorkQueue() {
313 // We can improve performance of our loading tasks from incoming_queue_ to
darin@google.com981f3552008-08-16 12:09:05 +0900314 // work_queue_ by waiting until the last minute (work_queue_ is empty) to
315 // load. That reduces the number of locks-per-task significantly when our
darin@google.combe165ae2008-09-07 17:08:29 +0900316 // queues get large.
317 if (!work_queue_.empty())
initial.commit3f4a7322008-07-27 06:49:38 +0900318 return; // Wait till we *really* need to lock and load.
319
320 // Acquire all we can from the inter-thread queue with one lock acquisition.
initial.commit3f4a7322008-07-27 06:49:38 +0900321 {
322 AutoLock lock(incoming_queue_lock_);
darin@google.combe165ae2008-09-07 17:08:29 +0900323 if (incoming_queue_.empty())
initial.commit3f4a7322008-07-27 06:49:38 +0900324 return;
darin@google.combe165ae2008-09-07 17:08:29 +0900325 std::swap(incoming_queue_, work_queue_);
326 DCHECK(incoming_queue_.empty());
initial.commit3f4a7322008-07-27 06:49:38 +0900327 }
328}
329
darin@chromium.orge3af17f2008-09-10 09:37:07 +0900330bool MessageLoop::DeletePendingTasks() {
331 bool did_work = !work_queue_.empty();
332 while (!work_queue_.empty()) {
333 PendingTask pending_task = work_queue_.front();
334 work_queue_.pop();
335 if (!pending_task.delayed_run_time.is_null()) {
336 // We want to delete delayed tasks in the same order in which they would
337 // normally be deleted in case of any funny dependencies between delayed
338 // tasks.
339 AddToDelayedWorkQueue(pending_task);
340 } else {
341 // TODO(darin): Delete all tasks once it is safe to do so.
342 //delete task;
343 }
initial.commit3f4a7322008-07-27 06:49:38 +0900344 }
darin@chromium.orge3af17f2008-09-10 09:37:07 +0900345 did_work |= !deferred_non_nestable_work_queue_.empty();
346 while (!deferred_non_nestable_work_queue_.empty()) {
darin@chromium.orge3af17f2008-09-10 09:37:07 +0900347 // TODO(darin): Delete all tasks once it is safe to do so.
darin@chromium.orgfea7a862008-09-10 10:16:17 +0900348 //Task* task = deferred_non_nestable_work_queue_.front().task;
349 deferred_non_nestable_work_queue_.pop();
darin@chromium.orge3af17f2008-09-10 09:37:07 +0900350 //delete task;
initial.commit3f4a7322008-07-27 06:49:38 +0900351 }
darin@chromium.orge3af17f2008-09-10 09:37:07 +0900352 did_work |= !delayed_work_queue_.empty();
353 while (!delayed_work_queue_.empty()) {
354 Task* task = delayed_work_queue_.top().task;
355 delayed_work_queue_.pop();
356 delete task;
357 }
358 return did_work;
initial.commit3f4a7322008-07-27 06:49:38 +0900359}
360
darin@google.com981f3552008-08-16 12:09:05 +0900361bool MessageLoop::DoWork() {
darin@google.combe165ae2008-09-07 17:08:29 +0900362 if (!nestable_tasks_allowed_) {
363 // Task can't be executed right now.
364 return false;
365 }
366
367 for (;;) {
368 ReloadWorkQueue();
369 if (work_queue_.empty())
370 break;
371
372 // Execute oldest task.
373 do {
374 PendingTask pending_task = work_queue_.front();
375 work_queue_.pop();
376 if (!pending_task.delayed_run_time.is_null()) {
377 bool was_empty = delayed_work_queue_.empty();
darin@chromium.orge3af17f2008-09-10 09:37:07 +0900378 AddToDelayedWorkQueue(pending_task);
darin@google.combe165ae2008-09-07 17:08:29 +0900379 if (was_empty) // We only schedule the next delayed work item.
380 pump_->ScheduleDelayedWork(pending_task.delayed_run_time);
381 } else {
382 if (DeferOrRunPendingTask(pending_task))
383 return true;
384 }
385 } while (!work_queue_.empty());
386 }
387
388 // Nothing happened.
389 return false;
darin@google.com981f3552008-08-16 12:09:05 +0900390}
391
darin@google.com6393bed2008-08-20 15:30:58 +0900392bool MessageLoop::DoDelayedWork(Time* next_delayed_work_time) {
darin@google.combe165ae2008-09-07 17:08:29 +0900393 if (!nestable_tasks_allowed_ || delayed_work_queue_.empty()) {
394 *next_delayed_work_time = Time();
395 return false;
396 }
397
398 if (delayed_work_queue_.top().delayed_run_time > Time::Now()) {
399 *next_delayed_work_time = delayed_work_queue_.top().delayed_run_time;
400 return false;
401 }
darin@google.com981f3552008-08-16 12:09:05 +0900402
darin@google.combe165ae2008-09-07 17:08:29 +0900403 PendingTask pending_task = delayed_work_queue_.top();
404 delayed_work_queue_.pop();
405
406 if (!delayed_work_queue_.empty())
407 *next_delayed_work_time = delayed_work_queue_.top().delayed_run_time;
darin@google.com981f3552008-08-16 12:09:05 +0900408
darin@google.combe165ae2008-09-07 17:08:29 +0900409 return DeferOrRunPendingTask(pending_task);
darin@google.com981f3552008-08-16 12:09:05 +0900410}
411
412bool MessageLoop::DoIdleWork() {
413 if (ProcessNextDelayedNonNestableTask())
414 return true;
415
416 if (state_->quit_received)
417 pump_->Quit();
418
419 return false;
420}
421
422//------------------------------------------------------------------------------
423// MessageLoop::AutoRunState
424
425MessageLoop::AutoRunState::AutoRunState(MessageLoop* loop) : loop_(loop) {
426 // Make the loop reference us.
427 previous_state_ = loop_->state_;
428 if (previous_state_) {
429 run_depth = previous_state_->run_depth + 1;
darin@google.com6ddeb842008-08-15 16:31:20 +0900430 } else {
darin@google.com981f3552008-08-16 12:09:05 +0900431 run_depth = 1;
darin@google.com6ddeb842008-08-15 16:31:20 +0900432 }
darin@google.com981f3552008-08-16 12:09:05 +0900433 loop_->state_ = this;
434
435 // Initialize the other fields:
436 quit_received = false;
437#if defined(OS_WIN)
438 dispatcher = NULL;
439#endif
440}
441
442MessageLoop::AutoRunState::~AutoRunState() {
443 loop_->state_ = previous_state_;
darin@google.comee6fa722008-08-13 08:25:43 +0900444}
445
initial.commit3f4a7322008-07-27 06:49:38 +0900446//------------------------------------------------------------------------------
darin@google.combe165ae2008-09-07 17:08:29 +0900447// MessageLoop::PendingTask
initial.commit3f4a7322008-07-27 06:49:38 +0900448
darin@google.combe165ae2008-09-07 17:08:29 +0900449bool MessageLoop::PendingTask::operator<(const PendingTask& other) const {
450 // Since the top of a priority queue is defined as the "greatest" element, we
451 // need to invert the comparison here. We want the smaller time to be at the
452 // top of the heap.
initial.commit3f4a7322008-07-27 06:49:38 +0900453
darin@google.combe165ae2008-09-07 17:08:29 +0900454 if (delayed_run_time < other.delayed_run_time)
455 return false;
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 true;
initial.commit3f4a7322008-07-27 06:49:38 +0900459
darin@google.combe165ae2008-09-07 17:08:29 +0900460 // If the times happen to match, then we use the sequence number to decide.
461 // Compare the difference to support integer roll-over.
462 return (sequence_num - other.sequence_num) > 0;
initial.commit3f4a7322008-07-27 06:49:38 +0900463}
464
465//------------------------------------------------------------------------------
466// Method and data for histogramming events and actions taken by each instance
467// on each thread.
468
469// static
470bool MessageLoop::enable_histogrammer_ = false;
471
472// static
473void MessageLoop::EnableHistogrammer(bool enable) {
474 enable_histogrammer_ = enable;
475}
476
477void MessageLoop::StartHistogrammer() {
478 if (enable_histogrammer_ && !message_histogram_.get()
479 && StatisticsRecorder::WasStarted()) {
darin@google.com981f3552008-08-16 12:09:05 +0900480 DCHECK(!thread_name_.empty());
initial.commit3f4a7322008-07-27 06:49:38 +0900481 message_histogram_.reset(new LinearHistogram(
482 ASCIIToWide("MsgLoop:" + thread_name_).c_str(),
483 kLeastNonZeroMessageId,
484 kMaxMessageId,
485 kNumberOfDistinctMessagesDisplayed));
486 message_histogram_->SetFlags(message_histogram_->kHexRangePrintingFlag);
487 message_histogram_->SetRangeDescriptions(event_descriptions_);
488 }
489}
490
491void MessageLoop::HistogramEvent(int event) {
492 if (message_histogram_.get())
493 message_histogram_->Add(event);
494}
495
initial.commit3f4a7322008-07-27 06:49:38 +0900496// Provide a macro that takes an expression (such as a constant, or macro
497// constant) and creates a pair to initalize an array of pairs. In this case,
498// our pair consists of the expressions value, and the "stringized" version
499// of the expression (i.e., the exrpression put in quotes). For example, if
500// we have:
501// #define FOO 2
502// #define BAR 5
503// then the following:
504// VALUE_TO_NUMBER_AND_NAME(FOO + BAR)
505// will expand to:
506// {7, "FOO + BAR"}
507// We use the resulting array as an argument to our histogram, which reads the
508// number as a bucket identifier, and proceeds to use the corresponding name
509// in the pair (i.e., the quoted string) when printing out a histogram.
510#define VALUE_TO_NUMBER_AND_NAME(name) {name, #name},
511
initial.commit3f4a7322008-07-27 06:49:38 +0900512// static
513const LinearHistogram::DescriptionPair MessageLoop::event_descriptions_[] = {
initial.commit3f4a7322008-07-27 06:49:38 +0900514 // Provide some pretty print capability in our histogram for our internal
515 // messages.
516
initial.commit3f4a7322008-07-27 06:49:38 +0900517 // A few events we handle (kindred to messages), and used to profile actions.
518 VALUE_TO_NUMBER_AND_NAME(kTaskRunEvent)
initial.commit3f4a7322008-07-27 06:49:38 +0900519 VALUE_TO_NUMBER_AND_NAME(kTimerEvent)
520
521 {-1, NULL} // The list must be null terminated, per API to histogram.
522};
license.botf003cfe2008-08-24 09:55:55 +0900523
darin@google.comd936b5b2008-08-26 14:53:57 +0900524//------------------------------------------------------------------------------
525// MessageLoopForUI
526
527#if defined(OS_WIN)
528
529void MessageLoopForUI::Run(Dispatcher* dispatcher) {
530 AutoRunState save_state(this);
531 state_->dispatcher = dispatcher;
532 RunHandler();
533}
534
535void MessageLoopForUI::AddObserver(Observer* observer) {
536 pump_win()->AddObserver(observer);
537}
538
539void MessageLoopForUI::RemoveObserver(Observer* observer) {
540 pump_win()->RemoveObserver(observer);
541}
542
543void MessageLoopForUI::WillProcessMessage(const MSG& message) {
544 pump_win()->WillProcessMessage(message);
545}
546void MessageLoopForUI::DidProcessMessage(const MSG& message) {
547 pump_win()->DidProcessMessage(message);
548}
549void MessageLoopForUI::PumpOutPendingPaintMessages() {
550 pump_win()->PumpOutPendingPaintMessages();
551}
552
553#endif // defined(OS_WIN)
554
555//------------------------------------------------------------------------------
556// MessageLoopForIO
557
558#if defined(OS_WIN)
559
560void MessageLoopForIO::WatchObject(HANDLE object, Watcher* watcher) {
561 pump_win()->WatchObject(object, watcher);
562}
563
564#endif // defined(OS_WIN)