Revert 63397 - Add a message pump for touchui=1
[chromium-blink-merge.git] / base / message_loop.cc
blob8f6c997b24afeecbf2597d863e45bfbda063f0f1
1 // Copyright (c) 2010 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.
5 #include "base/message_loop.h"
7 #include <algorithm>
9 #include "base/compiler_specific.h"
10 #include "base/lazy_instance.h"
11 #include "base/logging.h"
12 #include "base/message_pump_default.h"
13 #include "base/metrics/histogram.h"
14 #include "base/thread_local.h"
16 #if defined(OS_MACOSX)
17 #include "base/message_pump_mac.h"
18 #endif
19 #if defined(OS_POSIX)
20 #include "base/message_pump_libevent.h"
21 #include "base/third_party/valgrind/valgrind.h"
22 #endif
23 #if defined(OS_POSIX) && !defined(OS_MACOSX)
24 #include "base/message_pump_glib.h"
25 #endif
27 using base::Time;
28 using base::TimeDelta;
30 namespace {
32 // A lazily created thread local storage for quick access to a thread's message
33 // loop, if one exists. This should be safe and free of static constructors.
34 base::LazyInstance<base::ThreadLocalPointer<MessageLoop> > lazy_tls_ptr(
35 base::LINKER_INITIALIZED);
37 // Logical events for Histogram profiling. Run with -message-loop-histogrammer
38 // to get an accounting of messages and actions taken on each thread.
39 const int kTaskRunEvent = 0x1;
40 const int kTimerEvent = 0x2;
42 // Provide range of message IDs for use in histogramming and debug display.
43 const int kLeastNonZeroMessageId = 1;
44 const int kMaxMessageId = 1099;
45 const int kNumberOfDistinctMessagesDisplayed = 1100;
47 // Provide a macro that takes an expression (such as a constant, or macro
48 // constant) and creates a pair to initalize an array of pairs. In this case,
49 // our pair consists of the expressions value, and the "stringized" version
50 // of the expression (i.e., the exrpression put in quotes). For example, if
51 // we have:
52 // #define FOO 2
53 // #define BAR 5
54 // then the following:
55 // VALUE_TO_NUMBER_AND_NAME(FOO + BAR)
56 // will expand to:
57 // {7, "FOO + BAR"}
58 // We use the resulting array as an argument to our histogram, which reads the
59 // number as a bucket identifier, and proceeds to use the corresponding name
60 // in the pair (i.e., the quoted string) when printing out a histogram.
61 #define VALUE_TO_NUMBER_AND_NAME(name) {name, #name},
63 const base::LinearHistogram::DescriptionPair event_descriptions_[] = {
64 // Provide some pretty print capability in our histogram for our internal
65 // messages.
67 // A few events we handle (kindred to messages), and used to profile actions.
68 VALUE_TO_NUMBER_AND_NAME(kTaskRunEvent)
69 VALUE_TO_NUMBER_AND_NAME(kTimerEvent)
71 {-1, NULL} // The list must be null terminated, per API to histogram.
74 bool enable_histogrammer_ = false;
76 } // namespace
78 //------------------------------------------------------------------------------
80 #if defined(OS_WIN)
82 // Upon a SEH exception in this thread, it restores the original unhandled
83 // exception filter.
84 static int SEHFilter(LPTOP_LEVEL_EXCEPTION_FILTER old_filter) {
85 ::SetUnhandledExceptionFilter(old_filter);
86 return EXCEPTION_CONTINUE_SEARCH;
89 // Retrieves a pointer to the current unhandled exception filter. There
90 // is no standalone getter method.
91 static LPTOP_LEVEL_EXCEPTION_FILTER GetTopSEHFilter() {
92 LPTOP_LEVEL_EXCEPTION_FILTER top_filter = NULL;
93 top_filter = ::SetUnhandledExceptionFilter(0);
94 ::SetUnhandledExceptionFilter(top_filter);
95 return top_filter;
98 #endif // defined(OS_WIN)
100 //------------------------------------------------------------------------------
102 MessageLoop::TaskObserver::TaskObserver() {
105 MessageLoop::TaskObserver::~TaskObserver() {
108 MessageLoop::DestructionObserver::~DestructionObserver() {
111 //------------------------------------------------------------------------------
113 // static
114 MessageLoop* MessageLoop::current() {
115 // TODO(darin): sadly, we cannot enable this yet since people call us even
116 // when they have no intention of using us.
117 // DCHECK(loop) << "Ouch, did you forget to initialize me?";
118 return lazy_tls_ptr.Pointer()->Get();
121 MessageLoop::MessageLoop(Type type)
122 : type_(type),
123 nestable_tasks_allowed_(true),
124 exception_restoration_(false),
125 state_(NULL),
126 next_sequence_num_(0) {
127 DCHECK(!current()) << "should only have one message loop per thread";
128 lazy_tls_ptr.Pointer()->Set(this);
130 // TODO(rvargas): Get rid of the OS guards.
131 #if defined(OS_WIN)
132 #define MESSAGE_PUMP_UI new base::MessagePumpForUI()
133 #define MESSAGE_PUMP_IO new base::MessagePumpForIO()
134 #elif defined(OS_MACOSX)
135 #define MESSAGE_PUMP_UI base::MessagePumpMac::Create()
136 #define MESSAGE_PUMP_IO new base::MessagePumpLibevent()
137 #elif defined(OS_POSIX) // POSIX but not MACOSX.
138 #define MESSAGE_PUMP_UI new base::MessagePumpForUI()
139 #define MESSAGE_PUMP_IO new base::MessagePumpLibevent()
140 #else
141 #error Not implemented
142 #endif
144 if (type_ == TYPE_UI) {
145 pump_ = MESSAGE_PUMP_UI;
146 } else if (type_ == TYPE_IO) {
147 pump_ = MESSAGE_PUMP_IO;
148 } else {
149 DCHECK_EQ(TYPE_DEFAULT, type_);
150 pump_ = new base::MessagePumpDefault();
154 MessageLoop::~MessageLoop() {
155 DCHECK(this == current());
157 // Let interested parties have one last shot at accessing this.
158 FOR_EACH_OBSERVER(DestructionObserver, destruction_observers_,
159 WillDestroyCurrentMessageLoop());
161 DCHECK(!state_);
163 // Clean up any unprocessed tasks, but take care: deleting a task could
164 // result in the addition of more tasks (e.g., via DeleteSoon). We set a
165 // limit on the number of times we will allow a deleted task to generate more
166 // tasks. Normally, we should only pass through this loop once or twice. If
167 // we end up hitting the loop limit, then it is probably due to one task that
168 // is being stubborn. Inspect the queues to see who is left.
169 bool did_work;
170 for (int i = 0; i < 100; ++i) {
171 DeletePendingTasks();
172 ReloadWorkQueue();
173 // If we end up with empty queues, then break out of the loop.
174 did_work = DeletePendingTasks();
175 if (!did_work)
176 break;
178 DCHECK(!did_work);
180 // OK, now make it so that no one can find us.
181 lazy_tls_ptr.Pointer()->Set(NULL);
184 void MessageLoop::AddDestructionObserver(
185 DestructionObserver* destruction_observer) {
186 DCHECK(this == current());
187 destruction_observers_.AddObserver(destruction_observer);
190 void MessageLoop::RemoveDestructionObserver(
191 DestructionObserver* destruction_observer) {
192 DCHECK(this == current());
193 destruction_observers_.RemoveObserver(destruction_observer);
196 void MessageLoop::AddTaskObserver(TaskObserver* task_observer) {
197 DCHECK_EQ(this, current());
198 task_observers_.AddObserver(task_observer);
201 void MessageLoop::RemoveTaskObserver(TaskObserver* task_observer) {
202 DCHECK_EQ(this, current());
203 task_observers_.RemoveObserver(task_observer);
206 void MessageLoop::Run() {
207 AutoRunState save_state(this);
208 RunHandler();
211 void MessageLoop::RunAllPending() {
212 AutoRunState save_state(this);
213 state_->quit_received = true; // Means run until we would otherwise block.
214 RunHandler();
217 // Runs the loop in two different SEH modes:
218 // enable_SEH_restoration_ = false : any unhandled exception goes to the last
219 // one that calls SetUnhandledExceptionFilter().
220 // enable_SEH_restoration_ = true : any unhandled exception goes to the filter
221 // that was existed before the loop was run.
222 void MessageLoop::RunHandler() {
223 #if defined(OS_WIN)
224 if (exception_restoration_) {
225 RunInternalInSEHFrame();
226 return;
228 #endif
230 RunInternal();
232 //------------------------------------------------------------------------------
233 #if defined(OS_WIN)
234 __declspec(noinline) void MessageLoop::RunInternalInSEHFrame() {
235 LPTOP_LEVEL_EXCEPTION_FILTER current_filter = GetTopSEHFilter();
236 __try {
237 RunInternal();
238 } __except(SEHFilter(current_filter)) {
240 return;
242 #endif
243 //------------------------------------------------------------------------------
245 void MessageLoop::RunInternal() {
246 DCHECK(this == current());
248 StartHistogrammer();
250 #if !defined(OS_MACOSX)
251 if (state_->dispatcher && type() == TYPE_UI) {
252 static_cast<base::MessagePumpForUI*>(pump_.get())->
253 RunWithDispatcher(this, state_->dispatcher);
254 return;
256 #endif
258 pump_->Run(this);
261 //------------------------------------------------------------------------------
262 // Wrapper functions for use in above message loop framework.
264 bool MessageLoop::ProcessNextDelayedNonNestableTask() {
265 if (state_->run_depth != 1)
266 return false;
268 if (deferred_non_nestable_work_queue_.empty())
269 return false;
271 Task* task = deferred_non_nestable_work_queue_.front().task;
272 deferred_non_nestable_work_queue_.pop();
274 RunTask(task);
275 return true;
278 //------------------------------------------------------------------------------
280 void MessageLoop::Quit() {
281 DCHECK(current() == this);
282 if (state_) {
283 state_->quit_received = true;
284 } else {
285 NOTREACHED() << "Must be inside Run to call Quit";
289 void MessageLoop::QuitNow() {
290 DCHECK(current() == this);
291 if (state_) {
292 pump_->Quit();
293 } else {
294 NOTREACHED() << "Must be inside Run to call Quit";
298 void MessageLoop::PostTask(
299 const tracked_objects::Location& from_here, Task* task) {
300 PostTask_Helper(from_here, task, 0, true);
303 void MessageLoop::PostDelayedTask(
304 const tracked_objects::Location& from_here, Task* task, int64 delay_ms) {
305 PostTask_Helper(from_here, task, delay_ms, true);
308 void MessageLoop::PostNonNestableTask(
309 const tracked_objects::Location& from_here, Task* task) {
310 PostTask_Helper(from_here, task, 0, false);
313 void MessageLoop::PostNonNestableDelayedTask(
314 const tracked_objects::Location& from_here, Task* task, int64 delay_ms) {
315 PostTask_Helper(from_here, task, delay_ms, false);
318 // Possibly called on a background thread!
319 void MessageLoop::PostTask_Helper(
320 const tracked_objects::Location& from_here, Task* task, int64 delay_ms,
321 bool nestable) {
322 task->SetBirthPlace(from_here);
324 PendingTask pending_task(task, nestable);
326 if (delay_ms > 0) {
327 pending_task.delayed_run_time =
328 Time::Now() + TimeDelta::FromMilliseconds(delay_ms);
330 #if defined(OS_WIN)
331 if (high_resolution_timer_expiration_.is_null()) {
332 // Windows timers are granular to 15.6ms. If we only set high-res
333 // timers for those under 15.6ms, then a 18ms timer ticks at ~32ms,
334 // which as a percentage is pretty inaccurate. So enable high
335 // res timers for any timer which is within 2x of the granularity.
336 // This is a tradeoff between accuracy and power management.
337 bool needs_high_res_timers =
338 delay_ms < (2 * Time::kMinLowResolutionThresholdMs);
339 if (needs_high_res_timers) {
340 Time::ActivateHighResolutionTimer(true);
341 high_resolution_timer_expiration_ = base::TimeTicks::Now() +
342 TimeDelta::FromMilliseconds(kHighResolutionTimerModeLeaseTimeMs);
345 #endif
346 } else {
347 DCHECK_EQ(delay_ms, 0) << "delay should not be negative";
350 #if defined(OS_WIN)
351 if (!high_resolution_timer_expiration_.is_null()) {
352 if (base::TimeTicks::Now() > high_resolution_timer_expiration_) {
353 Time::ActivateHighResolutionTimer(false);
354 high_resolution_timer_expiration_ = base::TimeTicks();
357 #endif
359 // Warning: Don't try to short-circuit, and handle this thread's tasks more
360 // directly, as it could starve handling of foreign threads. Put every task
361 // into this queue.
363 scoped_refptr<base::MessagePump> pump;
365 AutoLock locked(incoming_queue_lock_);
367 bool was_empty = incoming_queue_.empty();
368 incoming_queue_.push(pending_task);
369 if (!was_empty)
370 return; // Someone else should have started the sub-pump.
372 pump = pump_;
374 // Since the incoming_queue_ may contain a task that destroys this message
375 // loop, we cannot exit incoming_queue_lock_ until we are done with |this|.
376 // We use a stack-based reference to the message pump so that we can call
377 // ScheduleWork outside of incoming_queue_lock_.
379 pump->ScheduleWork();
382 void MessageLoop::SetNestableTasksAllowed(bool allowed) {
383 if (nestable_tasks_allowed_ != allowed) {
384 nestable_tasks_allowed_ = allowed;
385 if (!nestable_tasks_allowed_)
386 return;
387 // Start the native pump if we are not already pumping.
388 pump_->ScheduleWork();
392 bool MessageLoop::NestableTasksAllowed() const {
393 return nestable_tasks_allowed_;
396 bool MessageLoop::IsNested() {
397 return state_->run_depth > 1;
400 //------------------------------------------------------------------------------
402 void MessageLoop::RunTask(Task* task) {
403 DCHECK(nestable_tasks_allowed_);
404 // Execute the task and assume the worst: It is probably not reentrant.
405 nestable_tasks_allowed_ = false;
407 HistogramEvent(kTaskRunEvent);
408 FOR_EACH_OBSERVER(TaskObserver, task_observers_,
409 WillProcessTask(task->tracked_birth_time()));
410 task->Run();
411 FOR_EACH_OBSERVER(TaskObserver, task_observers_, DidProcessTask());
412 delete task;
414 nestable_tasks_allowed_ = true;
417 bool MessageLoop::DeferOrRunPendingTask(const PendingTask& pending_task) {
418 if (pending_task.nestable || state_->run_depth == 1) {
419 RunTask(pending_task.task);
420 // Show that we ran a task (Note: a new one might arrive as a
421 // consequence!).
422 return true;
425 // We couldn't run the task now because we're in a nested message loop
426 // and the task isn't nestable.
427 deferred_non_nestable_work_queue_.push(pending_task);
428 return false;
431 void MessageLoop::AddToDelayedWorkQueue(const PendingTask& pending_task) {
432 // Move to the delayed work queue. Initialize the sequence number
433 // before inserting into the delayed_work_queue_. The sequence number
434 // is used to faciliate FIFO sorting when two tasks have the same
435 // delayed_run_time value.
436 PendingTask new_pending_task(pending_task);
437 new_pending_task.sequence_num = next_sequence_num_++;
438 delayed_work_queue_.push(new_pending_task);
441 void MessageLoop::ReloadWorkQueue() {
442 // We can improve performance of our loading tasks from incoming_queue_ to
443 // work_queue_ by waiting until the last minute (work_queue_ is empty) to
444 // load. That reduces the number of locks-per-task significantly when our
445 // queues get large.
446 if (!work_queue_.empty())
447 return; // Wait till we *really* need to lock and load.
449 // Acquire all we can from the inter-thread queue with one lock acquisition.
451 AutoLock lock(incoming_queue_lock_);
452 if (incoming_queue_.empty())
453 return;
454 incoming_queue_.Swap(&work_queue_); // Constant time
455 DCHECK(incoming_queue_.empty());
459 bool MessageLoop::DeletePendingTasks() {
460 bool did_work = !work_queue_.empty();
461 while (!work_queue_.empty()) {
462 PendingTask pending_task = work_queue_.front();
463 work_queue_.pop();
464 if (!pending_task.delayed_run_time.is_null()) {
465 // We want to delete delayed tasks in the same order in which they would
466 // normally be deleted in case of any funny dependencies between delayed
467 // tasks.
468 AddToDelayedWorkQueue(pending_task);
469 } else {
470 // TODO(darin): Delete all tasks once it is safe to do so.
471 // Until it is totally safe, just do it when running Purify or
472 // Valgrind.
473 #if defined(PURIFY)
474 delete pending_task.task;
475 #elif defined(OS_POSIX)
476 if (RUNNING_ON_VALGRIND)
477 delete pending_task.task;
478 #endif // defined(OS_POSIX)
481 did_work |= !deferred_non_nestable_work_queue_.empty();
482 while (!deferred_non_nestable_work_queue_.empty()) {
483 // TODO(darin): Delete all tasks once it is safe to do so.
484 // Until it is totaly safe, only delete them under Purify and Valgrind.
485 Task* task = NULL;
486 #if defined(PURIFY)
487 task = deferred_non_nestable_work_queue_.front().task;
488 #elif defined(OS_POSIX)
489 if (RUNNING_ON_VALGRIND)
490 task = deferred_non_nestable_work_queue_.front().task;
491 #endif
492 deferred_non_nestable_work_queue_.pop();
493 if (task)
494 delete task;
496 did_work |= !delayed_work_queue_.empty();
497 while (!delayed_work_queue_.empty()) {
498 Task* task = delayed_work_queue_.top().task;
499 delayed_work_queue_.pop();
500 delete task;
502 return did_work;
505 bool MessageLoop::DoWork() {
506 if (!nestable_tasks_allowed_) {
507 // Task can't be executed right now.
508 return false;
511 for (;;) {
512 ReloadWorkQueue();
513 if (work_queue_.empty())
514 break;
516 // Execute oldest task.
517 do {
518 PendingTask pending_task = work_queue_.front();
519 work_queue_.pop();
520 if (!pending_task.delayed_run_time.is_null()) {
521 AddToDelayedWorkQueue(pending_task);
522 // If we changed the topmost task, then it is time to re-schedule.
523 if (delayed_work_queue_.top().task == pending_task.task)
524 pump_->ScheduleDelayedWork(pending_task.delayed_run_time);
525 } else {
526 if (DeferOrRunPendingTask(pending_task))
527 return true;
529 } while (!work_queue_.empty());
532 // Nothing happened.
533 return false;
536 bool MessageLoop::DoDelayedWork(Time* next_delayed_work_time) {
537 if (!nestable_tasks_allowed_ || delayed_work_queue_.empty()) {
538 *next_delayed_work_time = Time();
539 return false;
542 if (delayed_work_queue_.top().delayed_run_time > Time::Now()) {
543 *next_delayed_work_time = delayed_work_queue_.top().delayed_run_time;
544 return false;
547 PendingTask pending_task = delayed_work_queue_.top();
548 delayed_work_queue_.pop();
550 if (!delayed_work_queue_.empty())
551 *next_delayed_work_time = delayed_work_queue_.top().delayed_run_time;
553 return DeferOrRunPendingTask(pending_task);
556 bool MessageLoop::DoIdleWork() {
557 if (ProcessNextDelayedNonNestableTask())
558 return true;
560 if (state_->quit_received)
561 pump_->Quit();
563 return false;
566 //------------------------------------------------------------------------------
567 // MessageLoop::AutoRunState
569 MessageLoop::AutoRunState::AutoRunState(MessageLoop* loop) : loop_(loop) {
570 // Make the loop reference us.
571 previous_state_ = loop_->state_;
572 if (previous_state_) {
573 run_depth = previous_state_->run_depth + 1;
574 } else {
575 run_depth = 1;
577 loop_->state_ = this;
579 // Initialize the other fields:
580 quit_received = false;
581 #if !defined(OS_MACOSX)
582 dispatcher = NULL;
583 #endif
586 MessageLoop::AutoRunState::~AutoRunState() {
587 loop_->state_ = previous_state_;
590 //------------------------------------------------------------------------------
591 // MessageLoop::PendingTask
593 bool MessageLoop::PendingTask::operator<(const PendingTask& other) const {
594 // Since the top of a priority queue is defined as the "greatest" element, we
595 // need to invert the comparison here. We want the smaller time to be at the
596 // top of the heap.
598 if (delayed_run_time < other.delayed_run_time)
599 return false;
601 if (delayed_run_time > other.delayed_run_time)
602 return true;
604 // If the times happen to match, then we use the sequence number to decide.
605 // Compare the difference to support integer roll-over.
606 return (sequence_num - other.sequence_num) > 0;
609 //------------------------------------------------------------------------------
610 // Method and data for histogramming events and actions taken by each instance
611 // on each thread.
613 // static
614 void MessageLoop::EnableHistogrammer(bool enable) {
615 enable_histogrammer_ = enable;
618 void MessageLoop::StartHistogrammer() {
619 if (enable_histogrammer_ && !message_histogram_.get()
620 && base::StatisticsRecorder::WasStarted()) {
621 DCHECK(!thread_name_.empty());
622 message_histogram_ = base::LinearHistogram::FactoryGet(
623 "MsgLoop:" + thread_name_,
624 kLeastNonZeroMessageId, kMaxMessageId,
625 kNumberOfDistinctMessagesDisplayed,
626 message_histogram_->kHexRangePrintingFlag);
627 message_histogram_->SetRangeDescriptions(event_descriptions_);
631 void MessageLoop::HistogramEvent(int event) {
632 if (message_histogram_.get())
633 message_histogram_->Add(event);
636 //------------------------------------------------------------------------------
637 // MessageLoopForUI
639 #if defined(OS_WIN)
640 void MessageLoopForUI::DidProcessMessage(const MSG& message) {
641 pump_win()->DidProcessMessage(message);
643 #endif // defined(OS_WIN)
645 #if !defined(OS_MACOSX)
646 void MessageLoopForUI::AddObserver(Observer* observer) {
647 pump_ui()->AddObserver(observer);
650 void MessageLoopForUI::RemoveObserver(Observer* observer) {
651 pump_ui()->RemoveObserver(observer);
654 void MessageLoopForUI::Run(Dispatcher* dispatcher) {
655 AutoRunState save_state(this);
656 state_->dispatcher = dispatcher;
657 RunHandler();
659 #endif // !defined(OS_MACOSX)
661 //------------------------------------------------------------------------------
662 // MessageLoopForIO
664 #if defined(OS_WIN)
666 void MessageLoopForIO::RegisterIOHandler(HANDLE file, IOHandler* handler) {
667 pump_io()->RegisterIOHandler(file, handler);
670 bool MessageLoopForIO::WaitForIOCompletion(DWORD timeout, IOHandler* filter) {
671 return pump_io()->WaitForIOCompletion(timeout, filter);
674 #elif defined(OS_POSIX)
676 bool MessageLoopForIO::WatchFileDescriptor(int fd,
677 bool persistent,
678 Mode mode,
679 FileDescriptorWatcher *controller,
680 Watcher *delegate) {
681 return pump_libevent()->WatchFileDescriptor(
683 persistent,
684 static_cast<base::MessagePumpLibevent::Mode>(mode),
685 controller,
686 delegate);
689 #endif