blob: 91c40d38a6a61ec20b803ec1ecabf1d4e0378f29 [file] [log] [blame]
license.botbf09a502008-08-24 00:55:551// 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.commitd7cae122008-07-26 21:49:384
[email protected]ea15e982008-08-15 07:31:205#include "base/message_loop.h"
6
[email protected]fc7fb6e2008-08-16 03:09:057#include <algorithm>
8
[email protected]f1ea2fa2008-08-21 22:26:069#include "base/compiler_specific.h"
[email protected]f886b7bf2008-09-10 10:54:0610#include "base/lazy_instance.h"
initial.commitd7cae122008-07-26 21:49:3811#include "base/logging.h"
[email protected]b16ef312008-08-19 18:36:2312#include "base/message_pump_default.h"
initial.commitd7cae122008-07-26 21:49:3813#include "base/string_util.h"
[email protected]f886b7bf2008-09-10 10:54:0614#include "base/thread_local.h"
initial.commitd7cae122008-07-26 21:49:3815
[email protected]96c9ea12008-09-23 21:08:2816#if defined(OS_MACOSX)
17#include "base/message_pump_mac.h"
18#endif
[email protected]36987e92008-09-18 18:46:2619#if defined(OS_POSIX)
20#include "base/message_pump_libevent.h"
21#endif
22
[email protected]f886b7bf2008-09-10 10:54:0623// A lazily created thread local storage for quick access to a thread's message
24// loop, if one exists. This should be safe and free of static constructors.
25static base::LazyInstance<base::ThreadLocalPointer<MessageLoop> > lazy_tls_ptr(
26 base::LINKER_INITIALIZED);
initial.commitd7cae122008-07-26 21:49:3827
28//------------------------------------------------------------------------------
29
initial.commitd7cae122008-07-26 21:49:3830// Logical events for Histogram profiling. Run with -message-loop-histogrammer
31// to get an accounting of messages and actions taken on each thread.
[email protected]fc7fb6e2008-08-16 03:09:0532static const int kTaskRunEvent = 0x1;
33static const int kTimerEvent = 0x2;
initial.commitd7cae122008-07-26 21:49:3834
35// Provide range of message IDs for use in histogramming and debug display.
36static const int kLeastNonZeroMessageId = 1;
37static const int kMaxMessageId = 1099;
38static const int kNumberOfDistinctMessagesDisplayed = 1100;
39
40//------------------------------------------------------------------------------
41
[email protected]fc7fb6e2008-08-16 03:09:0542#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:3843
initial.commitd7cae122008-07-26 21:49:3844// Upon a SEH exception in this thread, it restores the original unhandled
45// exception filter.
46static int SEHFilter(LPTOP_LEVEL_EXCEPTION_FILTER old_filter) {
47 ::SetUnhandledExceptionFilter(old_filter);
48 return EXCEPTION_CONTINUE_SEARCH;
49}
50
51// Retrieves a pointer to the current unhandled exception filter. There
52// is no standalone getter method.
53static LPTOP_LEVEL_EXCEPTION_FILTER GetTopSEHFilter() {
54 LPTOP_LEVEL_EXCEPTION_FILTER top_filter = NULL;
55 top_filter = ::SetUnhandledExceptionFilter(0);
56 ::SetUnhandledExceptionFilter(top_filter);
57 return top_filter;
58}
59
[email protected]fc7fb6e2008-08-16 03:09:0560#endif // defined(OS_WIN)
61
initial.commitd7cae122008-07-26 21:49:3862//------------------------------------------------------------------------------
63
[email protected]f886b7bf2008-09-10 10:54:0664// static
65MessageLoop* MessageLoop::current() {
66 // TODO(darin): sadly, we cannot enable this yet since people call us even
67 // when they have no intention of using us.
68 //DCHECK(loop) << "Ouch, did you forget to initialize me?";
69 return lazy_tls_ptr.Pointer()->Get();
70}
71
[email protected]4d9bdfaf2008-08-26 05:53:5772MessageLoop::MessageLoop(Type type)
73 : type_(type),
[email protected]a5b94a92008-08-12 23:25:4374 nestable_tasks_allowed_(true),
[email protected]b16ef312008-08-19 18:36:2375 exception_restoration_(false),
[email protected]752578562008-09-07 08:08:2976 state_(NULL),
77 next_sequence_num_(0) {
[email protected]f886b7bf2008-09-10 10:54:0678 DCHECK(!current()) << "should only have one message loop per thread";
79 lazy_tls_ptr.Pointer()->Set(this);
[email protected]4d9bdfaf2008-08-26 05:53:5780
[email protected]ef8963172008-08-26 06:52:4181 // TODO(darin): Choose the pump based on the requested type.
[email protected]fc7fb6e2008-08-16 03:09:0582#if defined(OS_WIN)
[email protected]9bcbf472008-08-30 00:22:4883 if (type_ == TYPE_DEFAULT) {
84 pump_ = new base::MessagePumpDefault();
85 } else {
86 pump_ = new base::MessagePumpWin();
87 }
[email protected]36987e92008-09-18 18:46:2688#elif defined(OS_POSIX)
[email protected]96c9ea12008-09-23 21:08:2889#if defined(OS_MACOSX)
90 if (type_ == TYPE_UI) {
91 pump_ = base::MessagePumpMac::Create();
92 } else
93#endif // OS_MACOSX
[email protected]36987e92008-09-18 18:46:2694 if (type_ == TYPE_IO) {
95 pump_ = new base::MessagePumpLibevent();
96 } else {
97 pump_ = new base::MessagePumpDefault();
98 }
[email protected]96c9ea12008-09-23 21:08:2899#else // OS_POSIX
[email protected]ef8963172008-08-26 06:52:41100 pump_ = new base::MessagePumpDefault();
[email protected]96c9ea12008-09-23 21:08:28101#endif // OS_POSIX
initial.commitd7cae122008-07-26 21:49:38102}
103
104MessageLoop::~MessageLoop() {
105 DCHECK(this == current());
[email protected]2a127252008-08-05 23:16:41106
107 // Let interested parties have one last shot at accessing this.
108 FOR_EACH_OBSERVER(DestructionObserver, destruction_observers_,
109 WillDestroyCurrentMessageLoop());
110
[email protected]08de3cde2008-09-09 05:55:35111 DCHECK(!state_);
112
[email protected]001747c2008-09-10 00:37:07113 // Clean up any unprocessed tasks, but take care: deleting a task could
114 // result in the addition of more tasks (e.g., via DeleteSoon). We set a
115 // limit on the number of times we will allow a deleted task to generate more
116 // tasks. Normally, we should only pass through this loop once or twice. If
117 // we end up hitting the loop limit, then it is probably due to one task that
118 // is being stubborn. Inspect the queues to see who is left.
119 bool did_work;
120 for (int i = 0; i < 100; ++i) {
121 DeletePendingTasks();
122 ReloadWorkQueue();
123 // If we end up with empty queues, then break out of the loop.
124 did_work = DeletePendingTasks();
125 if (!did_work)
126 break;
[email protected]08de3cde2008-09-09 05:55:35127 }
[email protected]001747c2008-09-10 00:37:07128 DCHECK(!did_work);
129
130 // OK, now make it so that no one can find us.
[email protected]2b89d22b22008-09-10 11:14:56131 lazy_tls_ptr.Pointer()->Set(NULL);
initial.commitd7cae122008-07-26 21:49:38132}
133
[email protected]2a127252008-08-05 23:16:41134void MessageLoop::AddDestructionObserver(DestructionObserver *obs) {
135 DCHECK(this == current());
136 destruction_observers_.AddObserver(obs);
137}
138
139void MessageLoop::RemoveDestructionObserver(DestructionObserver *obs) {
140 DCHECK(this == current());
141 destruction_observers_.RemoveObserver(obs);
142}
143
[email protected]ea15e982008-08-15 07:31:20144void MessageLoop::Run() {
[email protected]fc7fb6e2008-08-16 03:09:05145 AutoRunState save_state(this);
146 RunHandler();
[email protected]ea15e982008-08-15 07:31:20147}
148
[email protected]7e0e8762008-07-31 13:10:20149void MessageLoop::RunAllPending() {
[email protected]fc7fb6e2008-08-16 03:09:05150 AutoRunState save_state(this);
151 state_->quit_received = true; // Means run until we would otherwise block.
152 RunHandler();
initial.commitd7cae122008-07-26 21:49:38153}
154
155// Runs the loop in two different SEH modes:
156// enable_SEH_restoration_ = false : any unhandled exception goes to the last
157// one that calls SetUnhandledExceptionFilter().
158// enable_SEH_restoration_ = true : any unhandled exception goes to the filter
159// that was existed before the loop was run.
[email protected]fc7fb6e2008-08-16 03:09:05160void MessageLoop::RunHandler() {
161#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:38162 if (exception_restoration_) {
163 LPTOP_LEVEL_EXCEPTION_FILTER current_filter = GetTopSEHFilter();
164 __try {
[email protected]fc7fb6e2008-08-16 03:09:05165 RunInternal();
initial.commitd7cae122008-07-26 21:49:38166 } __except(SEHFilter(current_filter)) {
167 }
[email protected]fc7fb6e2008-08-16 03:09:05168 return;
initial.commitd7cae122008-07-26 21:49:38169 }
[email protected]fc7fb6e2008-08-16 03:09:05170#endif
171
172 RunInternal();
initial.commitd7cae122008-07-26 21:49:38173}
174
175//------------------------------------------------------------------------------
initial.commitd7cae122008-07-26 21:49:38176
[email protected]fc7fb6e2008-08-16 03:09:05177void MessageLoop::RunInternal() {
178 DCHECK(this == current());
179
initial.commitd7cae122008-07-26 21:49:38180 StartHistogrammer();
181
[email protected]fc7fb6e2008-08-16 03:09:05182#if defined(OS_WIN)
183 if (state_->dispatcher) {
184 pump_win()->RunWithDispatcher(this, state_->dispatcher);
185 return;
[email protected]7e0e8762008-07-31 13:10:20186 }
[email protected]fc7fb6e2008-08-16 03:09:05187#endif
188
189 pump_->Run(this);
[email protected]b8f2fe5d2008-07-30 07:50:53190}
[email protected]7622bd02008-07-30 06:58:56191
[email protected]3882c4332008-07-30 19:03:59192//------------------------------------------------------------------------------
193// Wrapper functions for use in above message loop framework.
194
initial.commitd7cae122008-07-26 21:49:38195bool MessageLoop::ProcessNextDelayedNonNestableTask() {
[email protected]fc7fb6e2008-08-16 03:09:05196 if (state_->run_depth != 1)
initial.commitd7cae122008-07-26 21:49:38197 return false;
198
[email protected]752578562008-09-07 08:08:29199 if (deferred_non_nestable_work_queue_.empty())
initial.commitd7cae122008-07-26 21:49:38200 return false;
[email protected]752578562008-09-07 08:08:29201
202 Task* task = deferred_non_nestable_work_queue_.front().task;
203 deferred_non_nestable_work_queue_.pop();
204
205 RunTask(task);
initial.commitd7cae122008-07-26 21:49:38206 return true;
207}
208
initial.commitd7cae122008-07-26 21:49:38209//------------------------------------------------------------------------------
210
211void MessageLoop::Quit() {
[email protected]fc7fb6e2008-08-16 03:09:05212 DCHECK(current() == this);
213 if (state_) {
214 state_->quit_received = true;
215 } else {
216 NOTREACHED() << "Must be inside Run to call Quit";
initial.commitd7cae122008-07-26 21:49:38217 }
initial.commitd7cae122008-07-26 21:49:38218}
219
[email protected]752578562008-09-07 08:08:29220void MessageLoop::PostTask(
221 const tracked_objects::Location& from_here, Task* task) {
222 PostTask_Helper(from_here, task, 0, true);
223}
224
225void MessageLoop::PostDelayedTask(
226 const tracked_objects::Location& from_here, Task* task, int delay_ms) {
227 PostTask_Helper(from_here, task, delay_ms, true);
228}
229
230void MessageLoop::PostNonNestableTask(
231 const tracked_objects::Location& from_here, Task* task) {
232 PostTask_Helper(from_here, task, 0, false);
233}
234
235void MessageLoop::PostNonNestableDelayedTask(
236 const tracked_objects::Location& from_here, Task* task, int delay_ms) {
237 PostTask_Helper(from_here, task, delay_ms, false);
238}
239
initial.commitd7cae122008-07-26 21:49:38240// Possibly called on a background thread!
[email protected]752578562008-09-07 08:08:29241void MessageLoop::PostTask_Helper(
242 const tracked_objects::Location& from_here, Task* task, int delay_ms,
243 bool nestable) {
initial.commitd7cae122008-07-26 21:49:38244 task->SetBirthPlace(from_here);
[email protected]9bcbf472008-08-30 00:22:48245
[email protected]752578562008-09-07 08:08:29246 PendingTask pending_task(task, nestable);
[email protected]9bcbf472008-08-30 00:22:48247
248 if (delay_ms > 0) {
[email protected]752578562008-09-07 08:08:29249 pending_task.delayed_run_time =
[email protected]9bcbf472008-08-30 00:22:48250 Time::Now() + TimeDelta::FromMilliseconds(delay_ms);
251 } else {
252 DCHECK(delay_ms == 0) << "delay should not be negative";
253 }
254
initial.commitd7cae122008-07-26 21:49:38255 // Warning: Don't try to short-circuit, and handle this thread's tasks more
256 // directly, as it could starve handling of foreign threads. Put every task
257 // into this queue.
258
[email protected]fc7fb6e2008-08-16 03:09:05259 scoped_refptr<base::MessagePump> pump;
initial.commitd7cae122008-07-26 21:49:38260 {
[email protected]fc7fb6e2008-08-16 03:09:05261 AutoLock locked(incoming_queue_lock_);
262
[email protected]752578562008-09-07 08:08:29263 bool was_empty = incoming_queue_.empty();
264 incoming_queue_.push(pending_task);
initial.commitd7cae122008-07-26 21:49:38265 if (!was_empty)
266 return; // Someone else should have started the sub-pump.
267
[email protected]fc7fb6e2008-08-16 03:09:05268 pump = pump_;
[email protected]ea15e982008-08-15 07:31:20269 }
[email protected]fc7fb6e2008-08-16 03:09:05270 // Since the incoming_queue_ may contain a task that destroys this message
271 // loop, we cannot exit incoming_queue_lock_ until we are done with |this|.
272 // We use a stack-based reference to the message pump so that we can call
273 // ScheduleWork outside of incoming_queue_lock_.
[email protected]ea15e982008-08-15 07:31:20274
[email protected]fc7fb6e2008-08-16 03:09:05275 pump->ScheduleWork();
initial.commitd7cae122008-07-26 21:49:38276}
277
278void MessageLoop::SetNestableTasksAllowed(bool allowed) {
[email protected]124a2bdf2008-08-09 00:14:09279 if (nestable_tasks_allowed_ != allowed) {
280 nestable_tasks_allowed_ = allowed;
281 if (!nestable_tasks_allowed_)
282 return;
283 // Start the native pump if we are not already pumping.
[email protected]fc7fb6e2008-08-16 03:09:05284 pump_->ScheduleWork();
[email protected]124a2bdf2008-08-09 00:14:09285 }
initial.commitd7cae122008-07-26 21:49:38286}
287
288bool MessageLoop::NestableTasksAllowed() const {
289 return nestable_tasks_allowed_;
290}
291
initial.commitd7cae122008-07-26 21:49:38292//------------------------------------------------------------------------------
initial.commitd7cae122008-07-26 21:49:38293
initial.commitd7cae122008-07-26 21:49:38294void MessageLoop::RunTask(Task* task) {
initial.commitd7cae122008-07-26 21:49:38295 DCHECK(nestable_tasks_allowed_);
296 // Execute the task and assume the worst: It is probably not reentrant.
297 nestable_tasks_allowed_ = false;
[email protected]752578562008-09-07 08:08:29298
299 HistogramEvent(kTaskRunEvent);
300 task->Run();
301 delete task;
302
303 nestable_tasks_allowed_ = true;
initial.commitd7cae122008-07-26 21:49:38304}
305
[email protected]752578562008-09-07 08:08:29306bool MessageLoop::DeferOrRunPendingTask(const PendingTask& pending_task) {
307 if (pending_task.nestable || state_->run_depth == 1) {
308 RunTask(pending_task.task);
309 // Show that we ran a task (Note: a new one might arrive as a
310 // consequence!).
311 return true;
312 }
313
314 // We couldn't run the task now because we're in a nested message loop
315 // and the task isn't nestable.
316 deferred_non_nestable_work_queue_.push(pending_task);
317 return false;
initial.commitd7cae122008-07-26 21:49:38318}
319
[email protected]001747c2008-09-10 00:37:07320void MessageLoop::AddToDelayedWorkQueue(const PendingTask& pending_task) {
321 // Move to the delayed work queue. Initialize the sequence number
322 // before inserting into the delayed_work_queue_. The sequence number
323 // is used to faciliate FIFO sorting when two tasks have the same
324 // delayed_run_time value.
325 PendingTask new_pending_task(pending_task);
326 new_pending_task.sequence_num = next_sequence_num_++;
327 delayed_work_queue_.push(new_pending_task);
328}
329
initial.commitd7cae122008-07-26 21:49:38330void MessageLoop::ReloadWorkQueue() {
331 // We can improve performance of our loading tasks from incoming_queue_ to
[email protected]fc7fb6e2008-08-16 03:09:05332 // work_queue_ by waiting until the last minute (work_queue_ is empty) to
333 // load. That reduces the number of locks-per-task significantly when our
[email protected]752578562008-09-07 08:08:29334 // queues get large.
335 if (!work_queue_.empty())
initial.commitd7cae122008-07-26 21:49:38336 return; // Wait till we *really* need to lock and load.
337
338 // Acquire all we can from the inter-thread queue with one lock acquisition.
initial.commitd7cae122008-07-26 21:49:38339 {
340 AutoLock lock(incoming_queue_lock_);
[email protected]752578562008-09-07 08:08:29341 if (incoming_queue_.empty())
initial.commitd7cae122008-07-26 21:49:38342 return;
[email protected]752578562008-09-07 08:08:29343 std::swap(incoming_queue_, work_queue_);
344 DCHECK(incoming_queue_.empty());
initial.commitd7cae122008-07-26 21:49:38345 }
346}
347
[email protected]001747c2008-09-10 00:37:07348bool MessageLoop::DeletePendingTasks() {
349 bool did_work = !work_queue_.empty();
350 while (!work_queue_.empty()) {
351 PendingTask pending_task = work_queue_.front();
352 work_queue_.pop();
353 if (!pending_task.delayed_run_time.is_null()) {
354 // We want to delete delayed tasks in the same order in which they would
355 // normally be deleted in case of any funny dependencies between delayed
356 // tasks.
357 AddToDelayedWorkQueue(pending_task);
358 } else {
359 // TODO(darin): Delete all tasks once it is safe to do so.
360 //delete task;
361 }
initial.commitd7cae122008-07-26 21:49:38362 }
[email protected]001747c2008-09-10 00:37:07363 did_work |= !deferred_non_nestable_work_queue_.empty();
364 while (!deferred_non_nestable_work_queue_.empty()) {
[email protected]001747c2008-09-10 00:37:07365 // TODO(darin): Delete all tasks once it is safe to do so.
[email protected]058bccb2008-09-10 01:16:17366 //Task* task = deferred_non_nestable_work_queue_.front().task;
367 deferred_non_nestable_work_queue_.pop();
[email protected]001747c2008-09-10 00:37:07368 //delete task;
initial.commitd7cae122008-07-26 21:49:38369 }
[email protected]001747c2008-09-10 00:37:07370 did_work |= !delayed_work_queue_.empty();
371 while (!delayed_work_queue_.empty()) {
372 Task* task = delayed_work_queue_.top().task;
373 delayed_work_queue_.pop();
374 delete task;
375 }
376 return did_work;
initial.commitd7cae122008-07-26 21:49:38377}
378
[email protected]fc7fb6e2008-08-16 03:09:05379bool MessageLoop::DoWork() {
[email protected]752578562008-09-07 08:08:29380 if (!nestable_tasks_allowed_) {
381 // Task can't be executed right now.
382 return false;
383 }
384
385 for (;;) {
386 ReloadWorkQueue();
387 if (work_queue_.empty())
388 break;
389
390 // Execute oldest task.
391 do {
392 PendingTask pending_task = work_queue_.front();
393 work_queue_.pop();
394 if (!pending_task.delayed_run_time.is_null()) {
[email protected]001747c2008-09-10 00:37:07395 AddToDelayedWorkQueue(pending_task);
[email protected]72deacd2008-09-23 19:19:20396 // If we changed the topmost task, then it is time to re-schedule.
397 if (delayed_work_queue_.top().task == pending_task.task)
[email protected]752578562008-09-07 08:08:29398 pump_->ScheduleDelayedWork(pending_task.delayed_run_time);
399 } else {
400 if (DeferOrRunPendingTask(pending_task))
401 return true;
402 }
403 } while (!work_queue_.empty());
404 }
405
406 // Nothing happened.
407 return false;
[email protected]fc7fb6e2008-08-16 03:09:05408}
409
[email protected]b24250fc2008-08-20 06:30:58410bool MessageLoop::DoDelayedWork(Time* next_delayed_work_time) {
[email protected]752578562008-09-07 08:08:29411 if (!nestable_tasks_allowed_ || delayed_work_queue_.empty()) {
412 *next_delayed_work_time = Time();
413 return false;
414 }
415
416 if (delayed_work_queue_.top().delayed_run_time > Time::Now()) {
417 *next_delayed_work_time = delayed_work_queue_.top().delayed_run_time;
418 return false;
419 }
[email protected]fc7fb6e2008-08-16 03:09:05420
[email protected]752578562008-09-07 08:08:29421 PendingTask pending_task = delayed_work_queue_.top();
422 delayed_work_queue_.pop();
423
424 if (!delayed_work_queue_.empty())
425 *next_delayed_work_time = delayed_work_queue_.top().delayed_run_time;
[email protected]fc7fb6e2008-08-16 03:09:05426
[email protected]752578562008-09-07 08:08:29427 return DeferOrRunPendingTask(pending_task);
[email protected]fc7fb6e2008-08-16 03:09:05428}
429
430bool MessageLoop::DoIdleWork() {
431 if (ProcessNextDelayedNonNestableTask())
432 return true;
433
434 if (state_->quit_received)
435 pump_->Quit();
436
437 return false;
438}
439
440//------------------------------------------------------------------------------
441// MessageLoop::AutoRunState
442
443MessageLoop::AutoRunState::AutoRunState(MessageLoop* loop) : loop_(loop) {
444 // Make the loop reference us.
445 previous_state_ = loop_->state_;
446 if (previous_state_) {
447 run_depth = previous_state_->run_depth + 1;
[email protected]ea15e982008-08-15 07:31:20448 } else {
[email protected]fc7fb6e2008-08-16 03:09:05449 run_depth = 1;
[email protected]ea15e982008-08-15 07:31:20450 }
[email protected]fc7fb6e2008-08-16 03:09:05451 loop_->state_ = this;
452
453 // Initialize the other fields:
454 quit_received = false;
455#if defined(OS_WIN)
456 dispatcher = NULL;
457#endif
458}
459
460MessageLoop::AutoRunState::~AutoRunState() {
461 loop_->state_ = previous_state_;
[email protected]a5b94a92008-08-12 23:25:43462}
463
initial.commitd7cae122008-07-26 21:49:38464//------------------------------------------------------------------------------
[email protected]752578562008-09-07 08:08:29465// MessageLoop::PendingTask
initial.commitd7cae122008-07-26 21:49:38466
[email protected]752578562008-09-07 08:08:29467bool MessageLoop::PendingTask::operator<(const PendingTask& other) const {
468 // Since the top of a priority queue is defined as the "greatest" element, we
469 // need to invert the comparison here. We want the smaller time to be at the
470 // top of the heap.
initial.commitd7cae122008-07-26 21:49:38471
[email protected]752578562008-09-07 08:08:29472 if (delayed_run_time < other.delayed_run_time)
473 return false;
initial.commitd7cae122008-07-26 21:49:38474
[email protected]752578562008-09-07 08:08:29475 if (delayed_run_time > other.delayed_run_time)
476 return true;
initial.commitd7cae122008-07-26 21:49:38477
[email protected]752578562008-09-07 08:08:29478 // If the times happen to match, then we use the sequence number to decide.
479 // Compare the difference to support integer roll-over.
480 return (sequence_num - other.sequence_num) > 0;
initial.commitd7cae122008-07-26 21:49:38481}
482
483//------------------------------------------------------------------------------
484// Method and data for histogramming events and actions taken by each instance
485// on each thread.
486
487// static
488bool MessageLoop::enable_histogrammer_ = false;
489
490// static
491void MessageLoop::EnableHistogrammer(bool enable) {
492 enable_histogrammer_ = enable;
493}
494
495void MessageLoop::StartHistogrammer() {
496 if (enable_histogrammer_ && !message_histogram_.get()
497 && StatisticsRecorder::WasStarted()) {
[email protected]fc7fb6e2008-08-16 03:09:05498 DCHECK(!thread_name_.empty());
initial.commitd7cae122008-07-26 21:49:38499 message_histogram_.reset(new LinearHistogram(
500 ASCIIToWide("MsgLoop:" + thread_name_).c_str(),
501 kLeastNonZeroMessageId,
502 kMaxMessageId,
503 kNumberOfDistinctMessagesDisplayed));
504 message_histogram_->SetFlags(message_histogram_->kHexRangePrintingFlag);
505 message_histogram_->SetRangeDescriptions(event_descriptions_);
506 }
507}
508
509void MessageLoop::HistogramEvent(int event) {
510 if (message_histogram_.get())
511 message_histogram_->Add(event);
512}
513
initial.commitd7cae122008-07-26 21:49:38514// Provide a macro that takes an expression (such as a constant, or macro
515// constant) and creates a pair to initalize an array of pairs. In this case,
516// our pair consists of the expressions value, and the "stringized" version
517// of the expression (i.e., the exrpression put in quotes). For example, if
518// we have:
519// #define FOO 2
520// #define BAR 5
521// then the following:
522// VALUE_TO_NUMBER_AND_NAME(FOO + BAR)
523// will expand to:
524// {7, "FOO + BAR"}
525// We use the resulting array as an argument to our histogram, which reads the
526// number as a bucket identifier, and proceeds to use the corresponding name
527// in the pair (i.e., the quoted string) when printing out a histogram.
528#define VALUE_TO_NUMBER_AND_NAME(name) {name, #name},
529
initial.commitd7cae122008-07-26 21:49:38530// static
531const LinearHistogram::DescriptionPair MessageLoop::event_descriptions_[] = {
initial.commitd7cae122008-07-26 21:49:38532 // Provide some pretty print capability in our histogram for our internal
533 // messages.
534
initial.commitd7cae122008-07-26 21:49:38535 // A few events we handle (kindred to messages), and used to profile actions.
536 VALUE_TO_NUMBER_AND_NAME(kTaskRunEvent)
initial.commitd7cae122008-07-26 21:49:38537 VALUE_TO_NUMBER_AND_NAME(kTimerEvent)
538
539 {-1, NULL} // The list must be null terminated, per API to histogram.
540};
license.botbf09a502008-08-24 00:55:55541
[email protected]4d9bdfaf2008-08-26 05:53:57542//------------------------------------------------------------------------------
543// MessageLoopForUI
544
545#if defined(OS_WIN)
546
547void MessageLoopForUI::Run(Dispatcher* dispatcher) {
548 AutoRunState save_state(this);
549 state_->dispatcher = dispatcher;
550 RunHandler();
551}
552
553void MessageLoopForUI::AddObserver(Observer* observer) {
554 pump_win()->AddObserver(observer);
555}
556
557void MessageLoopForUI::RemoveObserver(Observer* observer) {
558 pump_win()->RemoveObserver(observer);
559}
560
561void MessageLoopForUI::WillProcessMessage(const MSG& message) {
562 pump_win()->WillProcessMessage(message);
563}
564void MessageLoopForUI::DidProcessMessage(const MSG& message) {
565 pump_win()->DidProcessMessage(message);
566}
567void MessageLoopForUI::PumpOutPendingPaintMessages() {
568 pump_win()->PumpOutPendingPaintMessages();
569}
570
571#endif // defined(OS_WIN)
572
573//------------------------------------------------------------------------------
574// MessageLoopForIO
575
576#if defined(OS_WIN)
577
578void MessageLoopForIO::WatchObject(HANDLE object, Watcher* watcher) {
579 pump_win()->WatchObject(object, watcher);
580}
581
[email protected]36987e92008-09-18 18:46:26582#elif defined(OS_POSIX)
583
584void MessageLoopForIO::WatchSocket(int socket, short interest_mask,
585 struct event* e, Watcher* watcher) {
586 pump_libevent()->WatchSocket(socket, interest_mask, e, watcher);
587}
588
589void MessageLoopForIO::UnwatchSocket(struct event* e) {
590 pump_libevent()->UnwatchSocket(e);
591}
592#endif