blob: 3abf0c8f05f855e731b40b0cd44cd08fe54c9fba [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
deanm@chromium.orgcd1ce302008-09-10 19:54:06 +090093 // OK, now make it so that no one can find us.
94 lazy_tls_ptr.Pointer()->Set(NULL);
95
darin@google.com0e500502008-09-09 14:55:35 +090096 DCHECK(!state_);
97
darin@chromium.orge3af17f2008-09-10 09:37:07 +090098 // Clean up any unprocessed tasks, but take care: deleting a task could
99 // result in the addition of more tasks (e.g., via DeleteSoon). We set a
100 // limit on the number of times we will allow a deleted task to generate more
101 // tasks. Normally, we should only pass through this loop once or twice. If
102 // we end up hitting the loop limit, then it is probably due to one task that
103 // is being stubborn. Inspect the queues to see who is left.
104 bool did_work;
105 for (int i = 0; i < 100; ++i) {
106 DeletePendingTasks();
107 ReloadWorkQueue();
108 // If we end up with empty queues, then break out of the loop.
109 did_work = DeletePendingTasks();
110 if (!did_work)
111 break;
darin@google.com0e500502008-09-09 14:55:35 +0900112 }
darin@chromium.orge3af17f2008-09-10 09:37:07 +0900113 DCHECK(!did_work);
114
115 // OK, now make it so that no one can find us.
116 tls_index_.Set(NULL);
initial.commit3f4a7322008-07-27 06:49:38 +0900117}
118
darin@google.com965e5342008-08-06 08:16:41 +0900119void MessageLoop::AddDestructionObserver(DestructionObserver *obs) {
120 DCHECK(this == current());
121 destruction_observers_.AddObserver(obs);
122}
123
124void MessageLoop::RemoveDestructionObserver(DestructionObserver *obs) {
125 DCHECK(this == current());
126 destruction_observers_.RemoveObserver(obs);
127}
128
darin@google.com6ddeb842008-08-15 16:31:20 +0900129void MessageLoop::Run() {
darin@google.com981f3552008-08-16 12:09:05 +0900130 AutoRunState save_state(this);
131 RunHandler();
darin@google.com6ddeb842008-08-15 16:31:20 +0900132}
133
jar@google.com9239e022008-07-31 22:10:20 +0900134void MessageLoop::RunAllPending() {
darin@google.com981f3552008-08-16 12:09:05 +0900135 AutoRunState save_state(this);
136 state_->quit_received = true; // Means run until we would otherwise block.
137 RunHandler();
initial.commit3f4a7322008-07-27 06:49:38 +0900138}
139
140// Runs the loop in two different SEH modes:
141// enable_SEH_restoration_ = false : any unhandled exception goes to the last
142// one that calls SetUnhandledExceptionFilter().
143// enable_SEH_restoration_ = true : any unhandled exception goes to the filter
144// that was existed before the loop was run.
darin@google.com981f3552008-08-16 12:09:05 +0900145void MessageLoop::RunHandler() {
146#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900147 if (exception_restoration_) {
148 LPTOP_LEVEL_EXCEPTION_FILTER current_filter = GetTopSEHFilter();
149 __try {
darin@google.com981f3552008-08-16 12:09:05 +0900150 RunInternal();
initial.commit3f4a7322008-07-27 06:49:38 +0900151 } __except(SEHFilter(current_filter)) {
152 }
darin@google.com981f3552008-08-16 12:09:05 +0900153 return;
initial.commit3f4a7322008-07-27 06:49:38 +0900154 }
darin@google.com981f3552008-08-16 12:09:05 +0900155#endif
156
157 RunInternal();
initial.commit3f4a7322008-07-27 06:49:38 +0900158}
159
160//------------------------------------------------------------------------------
initial.commit3f4a7322008-07-27 06:49:38 +0900161
darin@google.com981f3552008-08-16 12:09:05 +0900162void MessageLoop::RunInternal() {
163 DCHECK(this == current());
164
initial.commit3f4a7322008-07-27 06:49:38 +0900165 StartHistogrammer();
166
darin@google.com981f3552008-08-16 12:09:05 +0900167#if defined(OS_WIN)
168 if (state_->dispatcher) {
169 pump_win()->RunWithDispatcher(this, state_->dispatcher);
170 return;
jar@google.com9239e022008-07-31 22:10:20 +0900171 }
darin@google.com981f3552008-08-16 12:09:05 +0900172#endif
173
174 pump_->Run(this);
jar@google.comfbaaf692008-07-30 16:50:53 +0900175}
jar@google.com7ff36e62008-07-30 15:58:56 +0900176
jar@google.comb4d1bff2008-07-31 04:03:59 +0900177//------------------------------------------------------------------------------
178// Wrapper functions for use in above message loop framework.
179
initial.commit3f4a7322008-07-27 06:49:38 +0900180bool MessageLoop::ProcessNextDelayedNonNestableTask() {
darin@google.com981f3552008-08-16 12:09:05 +0900181 if (state_->run_depth != 1)
initial.commit3f4a7322008-07-27 06:49:38 +0900182 return false;
183
darin@google.combe165ae2008-09-07 17:08:29 +0900184 if (deferred_non_nestable_work_queue_.empty())
initial.commit3f4a7322008-07-27 06:49:38 +0900185 return false;
darin@google.combe165ae2008-09-07 17:08:29 +0900186
187 Task* task = deferred_non_nestable_work_queue_.front().task;
188 deferred_non_nestable_work_queue_.pop();
189
190 RunTask(task);
initial.commit3f4a7322008-07-27 06:49:38 +0900191 return true;
192}
193
initial.commit3f4a7322008-07-27 06:49:38 +0900194//------------------------------------------------------------------------------
195
196void MessageLoop::Quit() {
darin@google.com981f3552008-08-16 12:09:05 +0900197 DCHECK(current() == this);
198 if (state_) {
199 state_->quit_received = true;
200 } else {
201 NOTREACHED() << "Must be inside Run to call Quit";
initial.commit3f4a7322008-07-27 06:49:38 +0900202 }
initial.commit3f4a7322008-07-27 06:49:38 +0900203}
204
darin@google.combe165ae2008-09-07 17:08:29 +0900205void MessageLoop::PostTask(
206 const tracked_objects::Location& from_here, Task* task) {
207 PostTask_Helper(from_here, task, 0, true);
208}
209
210void MessageLoop::PostDelayedTask(
211 const tracked_objects::Location& from_here, Task* task, int delay_ms) {
212 PostTask_Helper(from_here, task, delay_ms, true);
213}
214
215void MessageLoop::PostNonNestableTask(
216 const tracked_objects::Location& from_here, Task* task) {
217 PostTask_Helper(from_here, task, 0, false);
218}
219
220void MessageLoop::PostNonNestableDelayedTask(
221 const tracked_objects::Location& from_here, Task* task, int delay_ms) {
222 PostTask_Helper(from_here, task, delay_ms, false);
223}
224
initial.commit3f4a7322008-07-27 06:49:38 +0900225// Possibly called on a background thread!
darin@google.combe165ae2008-09-07 17:08:29 +0900226void MessageLoop::PostTask_Helper(
227 const tracked_objects::Location& from_here, Task* task, int delay_ms,
228 bool nestable) {
initial.commit3f4a7322008-07-27 06:49:38 +0900229 task->SetBirthPlace(from_here);
darin@google.com0795f572008-08-30 09:22:48 +0900230
darin@google.combe165ae2008-09-07 17:08:29 +0900231 PendingTask pending_task(task, nestable);
darin@google.com0795f572008-08-30 09:22:48 +0900232
233 if (delay_ms > 0) {
darin@google.combe165ae2008-09-07 17:08:29 +0900234 pending_task.delayed_run_time =
darin@google.com0795f572008-08-30 09:22:48 +0900235 Time::Now() + TimeDelta::FromMilliseconds(delay_ms);
236 } else {
237 DCHECK(delay_ms == 0) << "delay should not be negative";
238 }
239
initial.commit3f4a7322008-07-27 06:49:38 +0900240 // Warning: Don't try to short-circuit, and handle this thread's tasks more
241 // directly, as it could starve handling of foreign threads. Put every task
242 // into this queue.
243
darin@google.com981f3552008-08-16 12:09:05 +0900244 scoped_refptr<base::MessagePump> pump;
initial.commit3f4a7322008-07-27 06:49:38 +0900245 {
darin@google.com981f3552008-08-16 12:09:05 +0900246 AutoLock locked(incoming_queue_lock_);
247
darin@google.combe165ae2008-09-07 17:08:29 +0900248 bool was_empty = incoming_queue_.empty();
249 incoming_queue_.push(pending_task);
initial.commit3f4a7322008-07-27 06:49:38 +0900250 if (!was_empty)
251 return; // Someone else should have started the sub-pump.
252
darin@google.com981f3552008-08-16 12:09:05 +0900253 pump = pump_;
darin@google.com6ddeb842008-08-15 16:31:20 +0900254 }
darin@google.com981f3552008-08-16 12:09:05 +0900255 // Since the incoming_queue_ may contain a task that destroys this message
256 // loop, we cannot exit incoming_queue_lock_ until we are done with |this|.
257 // We use a stack-based reference to the message pump so that we can call
258 // ScheduleWork outside of incoming_queue_lock_.
darin@google.com6ddeb842008-08-15 16:31:20 +0900259
darin@google.com981f3552008-08-16 12:09:05 +0900260 pump->ScheduleWork();
initial.commit3f4a7322008-07-27 06:49:38 +0900261}
262
263void MessageLoop::SetNestableTasksAllowed(bool allowed) {
mpcomplete@google.com989d5f82008-08-09 09:14:09 +0900264 if (nestable_tasks_allowed_ != allowed) {
265 nestable_tasks_allowed_ = allowed;
266 if (!nestable_tasks_allowed_)
267 return;
268 // Start the native pump if we are not already pumping.
darin@google.com981f3552008-08-16 12:09:05 +0900269 pump_->ScheduleWork();
mpcomplete@google.com989d5f82008-08-09 09:14:09 +0900270 }
initial.commit3f4a7322008-07-27 06:49:38 +0900271}
272
273bool MessageLoop::NestableTasksAllowed() const {
274 return nestable_tasks_allowed_;
275}
276
initial.commit3f4a7322008-07-27 06:49:38 +0900277//------------------------------------------------------------------------------
initial.commit3f4a7322008-07-27 06:49:38 +0900278
initial.commit3f4a7322008-07-27 06:49:38 +0900279void MessageLoop::RunTask(Task* task) {
initial.commit3f4a7322008-07-27 06:49:38 +0900280 DCHECK(nestable_tasks_allowed_);
281 // Execute the task and assume the worst: It is probably not reentrant.
282 nestable_tasks_allowed_ = false;
darin@google.combe165ae2008-09-07 17:08:29 +0900283
284 HistogramEvent(kTaskRunEvent);
285 task->Run();
286 delete task;
287
288 nestable_tasks_allowed_ = true;
initial.commit3f4a7322008-07-27 06:49:38 +0900289}
290
darin@google.combe165ae2008-09-07 17:08:29 +0900291bool MessageLoop::DeferOrRunPendingTask(const PendingTask& pending_task) {
292 if (pending_task.nestable || state_->run_depth == 1) {
293 RunTask(pending_task.task);
294 // Show that we ran a task (Note: a new one might arrive as a
295 // consequence!).
296 return true;
297 }
298
299 // We couldn't run the task now because we're in a nested message loop
300 // and the task isn't nestable.
301 deferred_non_nestable_work_queue_.push(pending_task);
302 return false;
initial.commit3f4a7322008-07-27 06:49:38 +0900303}
304
darin@chromium.orge3af17f2008-09-10 09:37:07 +0900305void MessageLoop::AddToDelayedWorkQueue(const PendingTask& pending_task) {
306 // Move to the delayed work queue. Initialize the sequence number
307 // before inserting into the delayed_work_queue_. The sequence number
308 // is used to faciliate FIFO sorting when two tasks have the same
309 // delayed_run_time value.
310 PendingTask new_pending_task(pending_task);
311 new_pending_task.sequence_num = next_sequence_num_++;
312 delayed_work_queue_.push(new_pending_task);
313}
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@chromium.orge3af17f2008-09-10 09:37:07 +0900333bool MessageLoop::DeletePendingTasks() {
334 bool did_work = !work_queue_.empty();
335 while (!work_queue_.empty()) {
336 PendingTask pending_task = work_queue_.front();
337 work_queue_.pop();
338 if (!pending_task.delayed_run_time.is_null()) {
339 // We want to delete delayed tasks in the same order in which they would
340 // normally be deleted in case of any funny dependencies between delayed
341 // tasks.
342 AddToDelayedWorkQueue(pending_task);
343 } else {
344 // TODO(darin): Delete all tasks once it is safe to do so.
345 //delete task;
346 }
initial.commit3f4a7322008-07-27 06:49:38 +0900347 }
darin@chromium.orge3af17f2008-09-10 09:37:07 +0900348 did_work |= !deferred_non_nestable_work_queue_.empty();
349 while (!deferred_non_nestable_work_queue_.empty()) {
darin@chromium.orge3af17f2008-09-10 09:37:07 +0900350 // TODO(darin): Delete all tasks once it is safe to do so.
darin@chromium.orgfea7a862008-09-10 10:16:17 +0900351 //Task* task = deferred_non_nestable_work_queue_.front().task;
352 deferred_non_nestable_work_queue_.pop();
darin@chromium.orge3af17f2008-09-10 09:37:07 +0900353 //delete task;
initial.commit3f4a7322008-07-27 06:49:38 +0900354 }
darin@chromium.orge3af17f2008-09-10 09:37:07 +0900355 did_work |= !delayed_work_queue_.empty();
356 while (!delayed_work_queue_.empty()) {
357 Task* task = delayed_work_queue_.top().task;
358 delayed_work_queue_.pop();
359 delete task;
360 }
361 return did_work;
initial.commit3f4a7322008-07-27 06:49:38 +0900362}
363
darin@google.com981f3552008-08-16 12:09:05 +0900364bool MessageLoop::DoWork() {
darin@google.combe165ae2008-09-07 17:08:29 +0900365 if (!nestable_tasks_allowed_) {
366 // Task can't be executed right now.
367 return false;
368 }
369
370 for (;;) {
371 ReloadWorkQueue();
372 if (work_queue_.empty())
373 break;
374
375 // Execute oldest task.
376 do {
377 PendingTask pending_task = work_queue_.front();
378 work_queue_.pop();
379 if (!pending_task.delayed_run_time.is_null()) {
380 bool was_empty = delayed_work_queue_.empty();
darin@chromium.orge3af17f2008-09-10 09:37:07 +0900381 AddToDelayedWorkQueue(pending_task);
darin@google.combe165ae2008-09-07 17:08:29 +0900382 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)