blob: 48e05eb270cce716bbe106e9c1d4e9e67afbdfe7 [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]36987e92008-09-18 18:46:2616#if defined(OS_POSIX)
17#include "base/message_pump_libevent.h"
18#endif
19
[email protected]f886b7bf2008-09-10 10:54:0620// A lazily created thread local storage for quick access to a thread's message
21// loop, if one exists. This should be safe and free of static constructors.
22static base::LazyInstance<base::ThreadLocalPointer<MessageLoop> > lazy_tls_ptr(
23 base::LINKER_INITIALIZED);
initial.commitd7cae122008-07-26 21:49:3824
25//------------------------------------------------------------------------------
26
initial.commitd7cae122008-07-26 21:49:3827// Logical events for Histogram profiling. Run with -message-loop-histogrammer
28// to get an accounting of messages and actions taken on each thread.
[email protected]fc7fb6e2008-08-16 03:09:0529static const int kTaskRunEvent = 0x1;
30static const int kTimerEvent = 0x2;
initial.commitd7cae122008-07-26 21:49:3831
32// Provide range of message IDs for use in histogramming and debug display.
33static const int kLeastNonZeroMessageId = 1;
34static const int kMaxMessageId = 1099;
35static const int kNumberOfDistinctMessagesDisplayed = 1100;
36
37//------------------------------------------------------------------------------
38
[email protected]fc7fb6e2008-08-16 03:09:0539#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:3840
initial.commitd7cae122008-07-26 21:49:3841// Upon a SEH exception in this thread, it restores the original unhandled
42// exception filter.
43static int SEHFilter(LPTOP_LEVEL_EXCEPTION_FILTER old_filter) {
44 ::SetUnhandledExceptionFilter(old_filter);
45 return EXCEPTION_CONTINUE_SEARCH;
46}
47
48// Retrieves a pointer to the current unhandled exception filter. There
49// is no standalone getter method.
50static LPTOP_LEVEL_EXCEPTION_FILTER GetTopSEHFilter() {
51 LPTOP_LEVEL_EXCEPTION_FILTER top_filter = NULL;
52 top_filter = ::SetUnhandledExceptionFilter(0);
53 ::SetUnhandledExceptionFilter(top_filter);
54 return top_filter;
55}
56
[email protected]fc7fb6e2008-08-16 03:09:0557#endif // defined(OS_WIN)
58
initial.commitd7cae122008-07-26 21:49:3859//------------------------------------------------------------------------------
60
[email protected]f886b7bf2008-09-10 10:54:0661// static
62MessageLoop* MessageLoop::current() {
63 // TODO(darin): sadly, we cannot enable this yet since people call us even
64 // when they have no intention of using us.
65 //DCHECK(loop) << "Ouch, did you forget to initialize me?";
66 return lazy_tls_ptr.Pointer()->Get();
67}
68
[email protected]4d9bdfaf2008-08-26 05:53:5769MessageLoop::MessageLoop(Type type)
70 : type_(type),
[email protected]a5b94a92008-08-12 23:25:4371 nestable_tasks_allowed_(true),
[email protected]b16ef312008-08-19 18:36:2372 exception_restoration_(false),
[email protected]752578562008-09-07 08:08:2973 state_(NULL),
74 next_sequence_num_(0) {
[email protected]f886b7bf2008-09-10 10:54:0675 DCHECK(!current()) << "should only have one message loop per thread";
76 lazy_tls_ptr.Pointer()->Set(this);
[email protected]4d9bdfaf2008-08-26 05:53:5777
[email protected]ef8963172008-08-26 06:52:4178 // TODO(darin): Choose the pump based on the requested type.
[email protected]fc7fb6e2008-08-16 03:09:0579#if defined(OS_WIN)
[email protected]9bcbf472008-08-30 00:22:4880 if (type_ == TYPE_DEFAULT) {
81 pump_ = new base::MessagePumpDefault();
82 } else {
83 pump_ = new base::MessagePumpWin();
84 }
[email protected]36987e92008-09-18 18:46:2685#elif defined(OS_POSIX)
86 if (type_ == TYPE_IO) {
87 pump_ = new base::MessagePumpLibevent();
88 } else {
89 pump_ = new base::MessagePumpDefault();
90 }
[email protected]b16ef312008-08-19 18:36:2391#else
[email protected]ef8963172008-08-26 06:52:4192 pump_ = new base::MessagePumpDefault();
[email protected]fc7fb6e2008-08-16 03:09:0593#endif
initial.commitd7cae122008-07-26 21:49:3894}
95
96MessageLoop::~MessageLoop() {
97 DCHECK(this == current());
[email protected]2a127252008-08-05 23:16:4198
99 // Let interested parties have one last shot at accessing this.
100 FOR_EACH_OBSERVER(DestructionObserver, destruction_observers_,
101 WillDestroyCurrentMessageLoop());
102
[email protected]08de3cde2008-09-09 05:55:35103 DCHECK(!state_);
104
[email protected]001747c2008-09-10 00:37:07105 // Clean up any unprocessed tasks, but take care: deleting a task could
106 // result in the addition of more tasks (e.g., via DeleteSoon). We set a
107 // limit on the number of times we will allow a deleted task to generate more
108 // tasks. Normally, we should only pass through this loop once or twice. If
109 // we end up hitting the loop limit, then it is probably due to one task that
110 // is being stubborn. Inspect the queues to see who is left.
111 bool did_work;
112 for (int i = 0; i < 100; ++i) {
113 DeletePendingTasks();
114 ReloadWorkQueue();
115 // If we end up with empty queues, then break out of the loop.
116 did_work = DeletePendingTasks();
117 if (!did_work)
118 break;
[email protected]08de3cde2008-09-09 05:55:35119 }
[email protected]001747c2008-09-10 00:37:07120 DCHECK(!did_work);
121
122 // OK, now make it so that no one can find us.
[email protected]2b89d22b22008-09-10 11:14:56123 lazy_tls_ptr.Pointer()->Set(NULL);
initial.commitd7cae122008-07-26 21:49:38124}
125
[email protected]2a127252008-08-05 23:16:41126void MessageLoop::AddDestructionObserver(DestructionObserver *obs) {
127 DCHECK(this == current());
128 destruction_observers_.AddObserver(obs);
129}
130
131void MessageLoop::RemoveDestructionObserver(DestructionObserver *obs) {
132 DCHECK(this == current());
133 destruction_observers_.RemoveObserver(obs);
134}
135
[email protected]ea15e982008-08-15 07:31:20136void MessageLoop::Run() {
[email protected]fc7fb6e2008-08-16 03:09:05137 AutoRunState save_state(this);
138 RunHandler();
[email protected]ea15e982008-08-15 07:31:20139}
140
[email protected]7e0e8762008-07-31 13:10:20141void MessageLoop::RunAllPending() {
[email protected]fc7fb6e2008-08-16 03:09:05142 AutoRunState save_state(this);
143 state_->quit_received = true; // Means run until we would otherwise block.
144 RunHandler();
initial.commitd7cae122008-07-26 21:49:38145}
146
147// Runs the loop in two different SEH modes:
148// enable_SEH_restoration_ = false : any unhandled exception goes to the last
149// one that calls SetUnhandledExceptionFilter().
150// enable_SEH_restoration_ = true : any unhandled exception goes to the filter
151// that was existed before the loop was run.
[email protected]fc7fb6e2008-08-16 03:09:05152void MessageLoop::RunHandler() {
153#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:38154 if (exception_restoration_) {
155 LPTOP_LEVEL_EXCEPTION_FILTER current_filter = GetTopSEHFilter();
156 __try {
[email protected]fc7fb6e2008-08-16 03:09:05157 RunInternal();
initial.commitd7cae122008-07-26 21:49:38158 } __except(SEHFilter(current_filter)) {
159 }
[email protected]fc7fb6e2008-08-16 03:09:05160 return;
initial.commitd7cae122008-07-26 21:49:38161 }
[email protected]fc7fb6e2008-08-16 03:09:05162#endif
163
164 RunInternal();
initial.commitd7cae122008-07-26 21:49:38165}
166
167//------------------------------------------------------------------------------
initial.commitd7cae122008-07-26 21:49:38168
[email protected]fc7fb6e2008-08-16 03:09:05169void MessageLoop::RunInternal() {
170 DCHECK(this == current());
171
initial.commitd7cae122008-07-26 21:49:38172 StartHistogrammer();
173
[email protected]fc7fb6e2008-08-16 03:09:05174#if defined(OS_WIN)
175 if (state_->dispatcher) {
176 pump_win()->RunWithDispatcher(this, state_->dispatcher);
177 return;
[email protected]7e0e8762008-07-31 13:10:20178 }
[email protected]fc7fb6e2008-08-16 03:09:05179#endif
180
181 pump_->Run(this);
[email protected]b8f2fe5d2008-07-30 07:50:53182}
[email protected]7622bd02008-07-30 06:58:56183
[email protected]3882c4332008-07-30 19:03:59184//------------------------------------------------------------------------------
185// Wrapper functions for use in above message loop framework.
186
initial.commitd7cae122008-07-26 21:49:38187bool MessageLoop::ProcessNextDelayedNonNestableTask() {
[email protected]fc7fb6e2008-08-16 03:09:05188 if (state_->run_depth != 1)
initial.commitd7cae122008-07-26 21:49:38189 return false;
190
[email protected]752578562008-09-07 08:08:29191 if (deferred_non_nestable_work_queue_.empty())
initial.commitd7cae122008-07-26 21:49:38192 return false;
[email protected]752578562008-09-07 08:08:29193
194 Task* task = deferred_non_nestable_work_queue_.front().task;
195 deferred_non_nestable_work_queue_.pop();
196
197 RunTask(task);
initial.commitd7cae122008-07-26 21:49:38198 return true;
199}
200
initial.commitd7cae122008-07-26 21:49:38201//------------------------------------------------------------------------------
202
203void MessageLoop::Quit() {
[email protected]fc7fb6e2008-08-16 03:09:05204 DCHECK(current() == this);
205 if (state_) {
206 state_->quit_received = true;
207 } else {
208 NOTREACHED() << "Must be inside Run to call Quit";
initial.commitd7cae122008-07-26 21:49:38209 }
initial.commitd7cae122008-07-26 21:49:38210}
211
[email protected]752578562008-09-07 08:08:29212void MessageLoop::PostTask(
213 const tracked_objects::Location& from_here, Task* task) {
214 PostTask_Helper(from_here, task, 0, true);
215}
216
217void MessageLoop::PostDelayedTask(
218 const tracked_objects::Location& from_here, Task* task, int delay_ms) {
219 PostTask_Helper(from_here, task, delay_ms, true);
220}
221
222void MessageLoop::PostNonNestableTask(
223 const tracked_objects::Location& from_here, Task* task) {
224 PostTask_Helper(from_here, task, 0, false);
225}
226
227void MessageLoop::PostNonNestableDelayedTask(
228 const tracked_objects::Location& from_here, Task* task, int delay_ms) {
229 PostTask_Helper(from_here, task, delay_ms, false);
230}
231
initial.commitd7cae122008-07-26 21:49:38232// Possibly called on a background thread!
[email protected]752578562008-09-07 08:08:29233void MessageLoop::PostTask_Helper(
234 const tracked_objects::Location& from_here, Task* task, int delay_ms,
235 bool nestable) {
initial.commitd7cae122008-07-26 21:49:38236 task->SetBirthPlace(from_here);
[email protected]9bcbf472008-08-30 00:22:48237
[email protected]752578562008-09-07 08:08:29238 PendingTask pending_task(task, nestable);
[email protected]9bcbf472008-08-30 00:22:48239
240 if (delay_ms > 0) {
[email protected]752578562008-09-07 08:08:29241 pending_task.delayed_run_time =
[email protected]9bcbf472008-08-30 00:22:48242 Time::Now() + TimeDelta::FromMilliseconds(delay_ms);
243 } else {
244 DCHECK(delay_ms == 0) << "delay should not be negative";
245 }
246
initial.commitd7cae122008-07-26 21:49:38247 // Warning: Don't try to short-circuit, and handle this thread's tasks more
248 // directly, as it could starve handling of foreign threads. Put every task
249 // into this queue.
250
[email protected]fc7fb6e2008-08-16 03:09:05251 scoped_refptr<base::MessagePump> pump;
initial.commitd7cae122008-07-26 21:49:38252 {
[email protected]fc7fb6e2008-08-16 03:09:05253 AutoLock locked(incoming_queue_lock_);
254
[email protected]752578562008-09-07 08:08:29255 bool was_empty = incoming_queue_.empty();
256 incoming_queue_.push(pending_task);
initial.commitd7cae122008-07-26 21:49:38257 if (!was_empty)
258 return; // Someone else should have started the sub-pump.
259
[email protected]fc7fb6e2008-08-16 03:09:05260 pump = pump_;
[email protected]ea15e982008-08-15 07:31:20261 }
[email protected]fc7fb6e2008-08-16 03:09:05262 // Since the incoming_queue_ may contain a task that destroys this message
263 // loop, we cannot exit incoming_queue_lock_ until we are done with |this|.
264 // We use a stack-based reference to the message pump so that we can call
265 // ScheduleWork outside of incoming_queue_lock_.
[email protected]ea15e982008-08-15 07:31:20266
[email protected]fc7fb6e2008-08-16 03:09:05267 pump->ScheduleWork();
initial.commitd7cae122008-07-26 21:49:38268}
269
270void MessageLoop::SetNestableTasksAllowed(bool allowed) {
[email protected]124a2bdf2008-08-09 00:14:09271 if (nestable_tasks_allowed_ != allowed) {
272 nestable_tasks_allowed_ = allowed;
273 if (!nestable_tasks_allowed_)
274 return;
275 // Start the native pump if we are not already pumping.
[email protected]fc7fb6e2008-08-16 03:09:05276 pump_->ScheduleWork();
[email protected]124a2bdf2008-08-09 00:14:09277 }
initial.commitd7cae122008-07-26 21:49:38278}
279
280bool MessageLoop::NestableTasksAllowed() const {
281 return nestable_tasks_allowed_;
282}
283
initial.commitd7cae122008-07-26 21:49:38284//------------------------------------------------------------------------------
initial.commitd7cae122008-07-26 21:49:38285
initial.commitd7cae122008-07-26 21:49:38286void MessageLoop::RunTask(Task* task) {
initial.commitd7cae122008-07-26 21:49:38287 DCHECK(nestable_tasks_allowed_);
288 // Execute the task and assume the worst: It is probably not reentrant.
289 nestable_tasks_allowed_ = false;
[email protected]752578562008-09-07 08:08:29290
291 HistogramEvent(kTaskRunEvent);
292 task->Run();
293 delete task;
294
295 nestable_tasks_allowed_ = true;
initial.commitd7cae122008-07-26 21:49:38296}
297
[email protected]752578562008-09-07 08:08:29298bool MessageLoop::DeferOrRunPendingTask(const PendingTask& pending_task) {
299 if (pending_task.nestable || state_->run_depth == 1) {
300 RunTask(pending_task.task);
301 // Show that we ran a task (Note: a new one might arrive as a
302 // consequence!).
303 return true;
304 }
305
306 // We couldn't run the task now because we're in a nested message loop
307 // and the task isn't nestable.
308 deferred_non_nestable_work_queue_.push(pending_task);
309 return false;
initial.commitd7cae122008-07-26 21:49:38310}
311
[email protected]001747c2008-09-10 00:37:07312void MessageLoop::AddToDelayedWorkQueue(const PendingTask& pending_task) {
313 // Move to the delayed work queue. Initialize the sequence number
314 // before inserting into the delayed_work_queue_. The sequence number
315 // is used to faciliate FIFO sorting when two tasks have the same
316 // delayed_run_time value.
317 PendingTask new_pending_task(pending_task);
318 new_pending_task.sequence_num = next_sequence_num_++;
319 delayed_work_queue_.push(new_pending_task);
320}
321
initial.commitd7cae122008-07-26 21:49:38322void MessageLoop::ReloadWorkQueue() {
323 // We can improve performance of our loading tasks from incoming_queue_ to
[email protected]fc7fb6e2008-08-16 03:09:05324 // work_queue_ by waiting until the last minute (work_queue_ is empty) to
325 // load. That reduces the number of locks-per-task significantly when our
[email protected]752578562008-09-07 08:08:29326 // queues get large.
327 if (!work_queue_.empty())
initial.commitd7cae122008-07-26 21:49:38328 return; // Wait till we *really* need to lock and load.
329
330 // Acquire all we can from the inter-thread queue with one lock acquisition.
initial.commitd7cae122008-07-26 21:49:38331 {
332 AutoLock lock(incoming_queue_lock_);
[email protected]752578562008-09-07 08:08:29333 if (incoming_queue_.empty())
initial.commitd7cae122008-07-26 21:49:38334 return;
[email protected]752578562008-09-07 08:08:29335 std::swap(incoming_queue_, work_queue_);
336 DCHECK(incoming_queue_.empty());
initial.commitd7cae122008-07-26 21:49:38337 }
338}
339
[email protected]001747c2008-09-10 00:37:07340bool MessageLoop::DeletePendingTasks() {
341 bool did_work = !work_queue_.empty();
342 while (!work_queue_.empty()) {
343 PendingTask pending_task = work_queue_.front();
344 work_queue_.pop();
345 if (!pending_task.delayed_run_time.is_null()) {
346 // We want to delete delayed tasks in the same order in which they would
347 // normally be deleted in case of any funny dependencies between delayed
348 // tasks.
349 AddToDelayedWorkQueue(pending_task);
350 } else {
351 // TODO(darin): Delete all tasks once it is safe to do so.
352 //delete task;
353 }
initial.commitd7cae122008-07-26 21:49:38354 }
[email protected]001747c2008-09-10 00:37:07355 did_work |= !deferred_non_nestable_work_queue_.empty();
356 while (!deferred_non_nestable_work_queue_.empty()) {
[email protected]001747c2008-09-10 00:37:07357 // TODO(darin): Delete all tasks once it is safe to do so.
[email protected]058bccb2008-09-10 01:16:17358 //Task* task = deferred_non_nestable_work_queue_.front().task;
359 deferred_non_nestable_work_queue_.pop();
[email protected]001747c2008-09-10 00:37:07360 //delete task;
initial.commitd7cae122008-07-26 21:49:38361 }
[email protected]001747c2008-09-10 00:37:07362 did_work |= !delayed_work_queue_.empty();
363 while (!delayed_work_queue_.empty()) {
364 Task* task = delayed_work_queue_.top().task;
365 delayed_work_queue_.pop();
366 delete task;
367 }
368 return did_work;
initial.commitd7cae122008-07-26 21:49:38369}
370
[email protected]fc7fb6e2008-08-16 03:09:05371bool MessageLoop::DoWork() {
[email protected]752578562008-09-07 08:08:29372 if (!nestable_tasks_allowed_) {
373 // Task can't be executed right now.
374 return false;
375 }
376
377 for (;;) {
378 ReloadWorkQueue();
379 if (work_queue_.empty())
380 break;
381
382 // Execute oldest task.
383 do {
384 PendingTask pending_task = work_queue_.front();
385 work_queue_.pop();
386 if (!pending_task.delayed_run_time.is_null()) {
387 bool was_empty = delayed_work_queue_.empty();
[email protected]001747c2008-09-10 00:37:07388 AddToDelayedWorkQueue(pending_task);
[email protected]752578562008-09-07 08:08:29389 if (was_empty) // We only schedule the next delayed work item.
390 pump_->ScheduleDelayedWork(pending_task.delayed_run_time);
391 } else {
392 if (DeferOrRunPendingTask(pending_task))
393 return true;
394 }
395 } while (!work_queue_.empty());
396 }
397
398 // Nothing happened.
399 return false;
[email protected]fc7fb6e2008-08-16 03:09:05400}
401
[email protected]b24250fc2008-08-20 06:30:58402bool MessageLoop::DoDelayedWork(Time* next_delayed_work_time) {
[email protected]752578562008-09-07 08:08:29403 if (!nestable_tasks_allowed_ || delayed_work_queue_.empty()) {
404 *next_delayed_work_time = Time();
405 return false;
406 }
407
408 if (delayed_work_queue_.top().delayed_run_time > Time::Now()) {
409 *next_delayed_work_time = delayed_work_queue_.top().delayed_run_time;
410 return false;
411 }
[email protected]fc7fb6e2008-08-16 03:09:05412
[email protected]752578562008-09-07 08:08:29413 PendingTask pending_task = delayed_work_queue_.top();
414 delayed_work_queue_.pop();
415
416 if (!delayed_work_queue_.empty())
417 *next_delayed_work_time = delayed_work_queue_.top().delayed_run_time;
[email protected]fc7fb6e2008-08-16 03:09:05418
[email protected]752578562008-09-07 08:08:29419 return DeferOrRunPendingTask(pending_task);
[email protected]fc7fb6e2008-08-16 03:09:05420}
421
422bool MessageLoop::DoIdleWork() {
423 if (ProcessNextDelayedNonNestableTask())
424 return true;
425
426 if (state_->quit_received)
427 pump_->Quit();
428
429 return false;
430}
431
432//------------------------------------------------------------------------------
433// MessageLoop::AutoRunState
434
435MessageLoop::AutoRunState::AutoRunState(MessageLoop* loop) : loop_(loop) {
436 // Make the loop reference us.
437 previous_state_ = loop_->state_;
438 if (previous_state_) {
439 run_depth = previous_state_->run_depth + 1;
[email protected]ea15e982008-08-15 07:31:20440 } else {
[email protected]fc7fb6e2008-08-16 03:09:05441 run_depth = 1;
[email protected]ea15e982008-08-15 07:31:20442 }
[email protected]fc7fb6e2008-08-16 03:09:05443 loop_->state_ = this;
444
445 // Initialize the other fields:
446 quit_received = false;
447#if defined(OS_WIN)
448 dispatcher = NULL;
449#endif
450}
451
452MessageLoop::AutoRunState::~AutoRunState() {
453 loop_->state_ = previous_state_;
[email protected]a5b94a92008-08-12 23:25:43454}
455
initial.commitd7cae122008-07-26 21:49:38456//------------------------------------------------------------------------------
[email protected]752578562008-09-07 08:08:29457// MessageLoop::PendingTask
initial.commitd7cae122008-07-26 21:49:38458
[email protected]752578562008-09-07 08:08:29459bool MessageLoop::PendingTask::operator<(const PendingTask& other) const {
460 // Since the top of a priority queue is defined as the "greatest" element, we
461 // need to invert the comparison here. We want the smaller time to be at the
462 // top of the heap.
initial.commitd7cae122008-07-26 21:49:38463
[email protected]752578562008-09-07 08:08:29464 if (delayed_run_time < other.delayed_run_time)
465 return false;
initial.commitd7cae122008-07-26 21:49:38466
[email protected]752578562008-09-07 08:08:29467 if (delayed_run_time > other.delayed_run_time)
468 return true;
initial.commitd7cae122008-07-26 21:49:38469
[email protected]752578562008-09-07 08:08:29470 // If the times happen to match, then we use the sequence number to decide.
471 // Compare the difference to support integer roll-over.
472 return (sequence_num - other.sequence_num) > 0;
initial.commitd7cae122008-07-26 21:49:38473}
474
475//------------------------------------------------------------------------------
476// Method and data for histogramming events and actions taken by each instance
477// on each thread.
478
479// static
480bool MessageLoop::enable_histogrammer_ = false;
481
482// static
483void MessageLoop::EnableHistogrammer(bool enable) {
484 enable_histogrammer_ = enable;
485}
486
487void MessageLoop::StartHistogrammer() {
488 if (enable_histogrammer_ && !message_histogram_.get()
489 && StatisticsRecorder::WasStarted()) {
[email protected]fc7fb6e2008-08-16 03:09:05490 DCHECK(!thread_name_.empty());
initial.commitd7cae122008-07-26 21:49:38491 message_histogram_.reset(new LinearHistogram(
492 ASCIIToWide("MsgLoop:" + thread_name_).c_str(),
493 kLeastNonZeroMessageId,
494 kMaxMessageId,
495 kNumberOfDistinctMessagesDisplayed));
496 message_histogram_->SetFlags(message_histogram_->kHexRangePrintingFlag);
497 message_histogram_->SetRangeDescriptions(event_descriptions_);
498 }
499}
500
501void MessageLoop::HistogramEvent(int event) {
502 if (message_histogram_.get())
503 message_histogram_->Add(event);
504}
505
initial.commitd7cae122008-07-26 21:49:38506// Provide a macro that takes an expression (such as a constant, or macro
507// constant) and creates a pair to initalize an array of pairs. In this case,
508// our pair consists of the expressions value, and the "stringized" version
509// of the expression (i.e., the exrpression put in quotes). For example, if
510// we have:
511// #define FOO 2
512// #define BAR 5
513// then the following:
514// VALUE_TO_NUMBER_AND_NAME(FOO + BAR)
515// will expand to:
516// {7, "FOO + BAR"}
517// We use the resulting array as an argument to our histogram, which reads the
518// number as a bucket identifier, and proceeds to use the corresponding name
519// in the pair (i.e., the quoted string) when printing out a histogram.
520#define VALUE_TO_NUMBER_AND_NAME(name) {name, #name},
521
initial.commitd7cae122008-07-26 21:49:38522// static
523const LinearHistogram::DescriptionPair MessageLoop::event_descriptions_[] = {
initial.commitd7cae122008-07-26 21:49:38524 // Provide some pretty print capability in our histogram for our internal
525 // messages.
526
initial.commitd7cae122008-07-26 21:49:38527 // A few events we handle (kindred to messages), and used to profile actions.
528 VALUE_TO_NUMBER_AND_NAME(kTaskRunEvent)
initial.commitd7cae122008-07-26 21:49:38529 VALUE_TO_NUMBER_AND_NAME(kTimerEvent)
530
531 {-1, NULL} // The list must be null terminated, per API to histogram.
532};
license.botbf09a502008-08-24 00:55:55533
[email protected]4d9bdfaf2008-08-26 05:53:57534//------------------------------------------------------------------------------
535// MessageLoopForUI
536
537#if defined(OS_WIN)
538
539void MessageLoopForUI::Run(Dispatcher* dispatcher) {
540 AutoRunState save_state(this);
541 state_->dispatcher = dispatcher;
542 RunHandler();
543}
544
545void MessageLoopForUI::AddObserver(Observer* observer) {
546 pump_win()->AddObserver(observer);
547}
548
549void MessageLoopForUI::RemoveObserver(Observer* observer) {
550 pump_win()->RemoveObserver(observer);
551}
552
553void MessageLoopForUI::WillProcessMessage(const MSG& message) {
554 pump_win()->WillProcessMessage(message);
555}
556void MessageLoopForUI::DidProcessMessage(const MSG& message) {
557 pump_win()->DidProcessMessage(message);
558}
559void MessageLoopForUI::PumpOutPendingPaintMessages() {
560 pump_win()->PumpOutPendingPaintMessages();
561}
562
563#endif // defined(OS_WIN)
564
565//------------------------------------------------------------------------------
566// MessageLoopForIO
567
568#if defined(OS_WIN)
569
570void MessageLoopForIO::WatchObject(HANDLE object, Watcher* watcher) {
571 pump_win()->WatchObject(object, watcher);
572}
573
[email protected]36987e92008-09-18 18:46:26574#elif defined(OS_POSIX)
575
576void MessageLoopForIO::WatchSocket(int socket, short interest_mask,
577 struct event* e, Watcher* watcher) {
578 pump_libevent()->WatchSocket(socket, interest_mask, e, watcher);
579}
580
581void MessageLoopForIO::UnwatchSocket(struct event* e) {
582 pump_libevent()->UnwatchSocket(e);
583}
584#endif