license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 1 | // 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.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 4 | |
[email protected] | ea15e98 | 2008-08-15 07:31:20 | [diff] [blame] | 5 | #include "base/message_loop.h" |
| 6 | |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 7 | #include <algorithm> |
| 8 | |
[email protected] | f1ea2fa | 2008-08-21 22:26:06 | [diff] [blame] | 9 | #include "base/compiler_specific.h" |
[email protected] | f886b7bf | 2008-09-10 10:54:06 | [diff] [blame] | 10 | #include "base/lazy_instance.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 11 | #include "base/logging.h" |
[email protected] | b16ef31 | 2008-08-19 18:36:23 | [diff] [blame] | 12 | #include "base/message_pump_default.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 13 | #include "base/string_util.h" |
[email protected] | f886b7bf | 2008-09-10 10:54:06 | [diff] [blame] | 14 | #include "base/thread_local.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 15 | |
[email protected] | 96c9ea1 | 2008-09-23 21:08:28 | [diff] [blame] | 16 | #if defined(OS_MACOSX) |
| 17 | #include "base/message_pump_mac.h" |
| 18 | #endif |
[email protected] | 36987e9 | 2008-09-18 18:46:26 | [diff] [blame] | 19 | #if defined(OS_POSIX) |
| 20 | #include "base/message_pump_libevent.h" |
| 21 | #endif |
[email protected] | 8fc3a48 | 2008-10-03 16:52:59 | [diff] [blame] | 22 | #if defined(OS_LINUX) |
| 23 | #include "base/message_pump_glib.h" |
| 24 | #endif |
[email protected] | 36987e9 | 2008-09-18 18:46:26 | [diff] [blame] | 25 | |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 26 | using base::Time; |
| 27 | using base::TimeDelta; |
| 28 | |
[email protected] | f886b7bf | 2008-09-10 10:54:06 | [diff] [blame] | 29 | // A lazily created thread local storage for quick access to a thread's message |
| 30 | // loop, if one exists. This should be safe and free of static constructors. |
| 31 | static base::LazyInstance<base::ThreadLocalPointer<MessageLoop> > lazy_tls_ptr( |
| 32 | base::LINKER_INITIALIZED); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 33 | |
| 34 | //------------------------------------------------------------------------------ |
| 35 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 36 | // Logical events for Histogram profiling. Run with -message-loop-histogrammer |
| 37 | // to get an accounting of messages and actions taken on each thread. |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 38 | static const int kTaskRunEvent = 0x1; |
| 39 | static const int kTimerEvent = 0x2; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 40 | |
| 41 | // Provide range of message IDs for use in histogramming and debug display. |
| 42 | static const int kLeastNonZeroMessageId = 1; |
| 43 | static const int kMaxMessageId = 1099; |
| 44 | static const int kNumberOfDistinctMessagesDisplayed = 1100; |
| 45 | |
| 46 | //------------------------------------------------------------------------------ |
| 47 | |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 48 | #if defined(OS_WIN) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 49 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 50 | // Upon a SEH exception in this thread, it restores the original unhandled |
| 51 | // exception filter. |
| 52 | static int SEHFilter(LPTOP_LEVEL_EXCEPTION_FILTER old_filter) { |
| 53 | ::SetUnhandledExceptionFilter(old_filter); |
| 54 | return EXCEPTION_CONTINUE_SEARCH; |
| 55 | } |
| 56 | |
| 57 | // Retrieves a pointer to the current unhandled exception filter. There |
| 58 | // is no standalone getter method. |
| 59 | static LPTOP_LEVEL_EXCEPTION_FILTER GetTopSEHFilter() { |
| 60 | LPTOP_LEVEL_EXCEPTION_FILTER top_filter = NULL; |
| 61 | top_filter = ::SetUnhandledExceptionFilter(0); |
| 62 | ::SetUnhandledExceptionFilter(top_filter); |
| 63 | return top_filter; |
| 64 | } |
| 65 | |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 66 | #endif // defined(OS_WIN) |
| 67 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 68 | //------------------------------------------------------------------------------ |
| 69 | |
[email protected] | f886b7bf | 2008-09-10 10:54:06 | [diff] [blame] | 70 | // static |
| 71 | MessageLoop* MessageLoop::current() { |
| 72 | // TODO(darin): sadly, we cannot enable this yet since people call us even |
| 73 | // when they have no intention of using us. |
| 74 | //DCHECK(loop) << "Ouch, did you forget to initialize me?"; |
| 75 | return lazy_tls_ptr.Pointer()->Get(); |
| 76 | } |
| 77 | |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 78 | MessageLoop::MessageLoop(Type type) |
| 79 | : type_(type), |
[email protected] | a5b94a9 | 2008-08-12 23:25:43 | [diff] [blame] | 80 | nestable_tasks_allowed_(true), |
[email protected] | b16ef31 | 2008-08-19 18:36:23 | [diff] [blame] | 81 | exception_restoration_(false), |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 82 | state_(NULL), |
| 83 | next_sequence_num_(0) { |
[email protected] | f886b7bf | 2008-09-10 10:54:06 | [diff] [blame] | 84 | DCHECK(!current()) << "should only have one message loop per thread"; |
| 85 | lazy_tls_ptr.Pointer()->Set(this); |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 86 | |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 87 | #if defined(OS_WIN) |
[email protected] | 1a8f5d1d | 2008-09-25 20:33:04 | [diff] [blame] | 88 | // TODO(rvargas): Get rid of the OS guards. |
[email protected] | 9bcbf47 | 2008-08-30 00:22:48 | [diff] [blame] | 89 | if (type_ == TYPE_DEFAULT) { |
| 90 | pump_ = new base::MessagePumpDefault(); |
[email protected] | 1a8f5d1d | 2008-09-25 20:33:04 | [diff] [blame] | 91 | } else if (type_ == TYPE_IO) { |
| 92 | pump_ = new base::MessagePumpForIO(); |
[email protected] | 9bcbf47 | 2008-08-30 00:22:48 | [diff] [blame] | 93 | } else { |
[email protected] | 1a8f5d1d | 2008-09-25 20:33:04 | [diff] [blame] | 94 | DCHECK(type_ == TYPE_UI); |
| 95 | pump_ = new base::MessagePumpForUI(); |
[email protected] | 9bcbf47 | 2008-08-30 00:22:48 | [diff] [blame] | 96 | } |
[email protected] | 36987e9 | 2008-09-18 18:46:26 | [diff] [blame] | 97 | #elif defined(OS_POSIX) |
[email protected] | 96c9ea1 | 2008-09-23 21:08:28 | [diff] [blame] | 98 | if (type_ == TYPE_UI) { |
[email protected] | 8fc3a48 | 2008-10-03 16:52:59 | [diff] [blame] | 99 | #if defined(OS_MACOSX) |
[email protected] | 96c9ea1 | 2008-09-23 21:08:28 | [diff] [blame] | 100 | pump_ = base::MessagePumpMac::Create(); |
[email protected] | 8fc3a48 | 2008-10-03 16:52:59 | [diff] [blame] | 101 | #elif defined(OS_LINUX) |
| 102 | pump_ = new base::MessagePumpForUI(); |
| 103 | #endif // OS_LINUX |
| 104 | } else if (type_ == TYPE_IO) { |
[email protected] | 36987e9 | 2008-09-18 18:46:26 | [diff] [blame] | 105 | pump_ = new base::MessagePumpLibevent(); |
| 106 | } else { |
| 107 | pump_ = new base::MessagePumpDefault(); |
| 108 | } |
[email protected] | 96c9ea1 | 2008-09-23 21:08:28 | [diff] [blame] | 109 | #endif // OS_POSIX |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | MessageLoop::~MessageLoop() { |
| 113 | DCHECK(this == current()); |
[email protected] | 2a12725 | 2008-08-05 23:16:41 | [diff] [blame] | 114 | |
| 115 | // Let interested parties have one last shot at accessing this. |
| 116 | FOR_EACH_OBSERVER(DestructionObserver, destruction_observers_, |
| 117 | WillDestroyCurrentMessageLoop()); |
| 118 | |
[email protected] | 08de3cde | 2008-09-09 05:55:35 | [diff] [blame] | 119 | DCHECK(!state_); |
| 120 | |
[email protected] | 001747c | 2008-09-10 00:37:07 | [diff] [blame] | 121 | // Clean up any unprocessed tasks, but take care: deleting a task could |
| 122 | // result in the addition of more tasks (e.g., via DeleteSoon). We set a |
| 123 | // limit on the number of times we will allow a deleted task to generate more |
| 124 | // tasks. Normally, we should only pass through this loop once or twice. If |
| 125 | // we end up hitting the loop limit, then it is probably due to one task that |
| 126 | // is being stubborn. Inspect the queues to see who is left. |
| 127 | bool did_work; |
| 128 | for (int i = 0; i < 100; ++i) { |
| 129 | DeletePendingTasks(); |
| 130 | ReloadWorkQueue(); |
| 131 | // If we end up with empty queues, then break out of the loop. |
| 132 | did_work = DeletePendingTasks(); |
| 133 | if (!did_work) |
| 134 | break; |
[email protected] | 08de3cde | 2008-09-09 05:55:35 | [diff] [blame] | 135 | } |
[email protected] | 001747c | 2008-09-10 00:37:07 | [diff] [blame] | 136 | DCHECK(!did_work); |
| 137 | |
| 138 | // OK, now make it so that no one can find us. |
[email protected] | 2b89d22b2 | 2008-09-10 11:14:56 | [diff] [blame] | 139 | lazy_tls_ptr.Pointer()->Set(NULL); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 140 | } |
| 141 | |
[email protected] | 2a12725 | 2008-08-05 23:16:41 | [diff] [blame] | 142 | void MessageLoop::AddDestructionObserver(DestructionObserver *obs) { |
| 143 | DCHECK(this == current()); |
| 144 | destruction_observers_.AddObserver(obs); |
| 145 | } |
| 146 | |
| 147 | void MessageLoop::RemoveDestructionObserver(DestructionObserver *obs) { |
| 148 | DCHECK(this == current()); |
| 149 | destruction_observers_.RemoveObserver(obs); |
| 150 | } |
| 151 | |
[email protected] | ea15e98 | 2008-08-15 07:31:20 | [diff] [blame] | 152 | void MessageLoop::Run() { |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 153 | AutoRunState save_state(this); |
| 154 | RunHandler(); |
[email protected] | ea15e98 | 2008-08-15 07:31:20 | [diff] [blame] | 155 | } |
| 156 | |
[email protected] | 7e0e876 | 2008-07-31 13:10:20 | [diff] [blame] | 157 | void MessageLoop::RunAllPending() { |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 158 | AutoRunState save_state(this); |
| 159 | state_->quit_received = true; // Means run until we would otherwise block. |
| 160 | RunHandler(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | // Runs the loop in two different SEH modes: |
| 164 | // enable_SEH_restoration_ = false : any unhandled exception goes to the last |
| 165 | // one that calls SetUnhandledExceptionFilter(). |
| 166 | // enable_SEH_restoration_ = true : any unhandled exception goes to the filter |
| 167 | // that was existed before the loop was run. |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 168 | void MessageLoop::RunHandler() { |
| 169 | #if defined(OS_WIN) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 170 | if (exception_restoration_) { |
| 171 | LPTOP_LEVEL_EXCEPTION_FILTER current_filter = GetTopSEHFilter(); |
| 172 | __try { |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 173 | RunInternal(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 174 | } __except(SEHFilter(current_filter)) { |
| 175 | } |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 176 | return; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 177 | } |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 178 | #endif |
| 179 | |
| 180 | RunInternal(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | //------------------------------------------------------------------------------ |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 184 | |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 185 | void MessageLoop::RunInternal() { |
| 186 | DCHECK(this == current()); |
| 187 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 188 | StartHistogrammer(); |
| 189 | |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 190 | #if defined(OS_WIN) |
| 191 | if (state_->dispatcher) { |
| 192 | pump_win()->RunWithDispatcher(this, state_->dispatcher); |
| 193 | return; |
[email protected] | 7e0e876 | 2008-07-31 13:10:20 | [diff] [blame] | 194 | } |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 195 | #endif |
[email protected] | 1d2eb13 | 2008-12-08 17:36:06 | [diff] [blame] | 196 | |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 197 | pump_->Run(this); |
[email protected] | b8f2fe5d | 2008-07-30 07:50:53 | [diff] [blame] | 198 | } |
[email protected] | 7622bd0 | 2008-07-30 06:58:56 | [diff] [blame] | 199 | |
[email protected] | 3882c433 | 2008-07-30 19:03:59 | [diff] [blame] | 200 | //------------------------------------------------------------------------------ |
| 201 | // Wrapper functions for use in above message loop framework. |
| 202 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 203 | bool MessageLoop::ProcessNextDelayedNonNestableTask() { |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 204 | if (state_->run_depth != 1) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 205 | return false; |
| 206 | |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 207 | if (deferred_non_nestable_work_queue_.empty()) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 208 | return false; |
[email protected] | 1d2eb13 | 2008-12-08 17:36:06 | [diff] [blame] | 209 | |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 210 | Task* task = deferred_non_nestable_work_queue_.front().task; |
| 211 | deferred_non_nestable_work_queue_.pop(); |
[email protected] | 1d2eb13 | 2008-12-08 17:36:06 | [diff] [blame] | 212 | |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 213 | RunTask(task); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 214 | return true; |
| 215 | } |
| 216 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 217 | //------------------------------------------------------------------------------ |
| 218 | |
| 219 | void MessageLoop::Quit() { |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 220 | DCHECK(current() == this); |
| 221 | if (state_) { |
| 222 | state_->quit_received = true; |
| 223 | } else { |
| 224 | NOTREACHED() << "Must be inside Run to call Quit"; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 225 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 226 | } |
| 227 | |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 228 | void MessageLoop::PostTask( |
| 229 | const tracked_objects::Location& from_here, Task* task) { |
| 230 | PostTask_Helper(from_here, task, 0, true); |
| 231 | } |
| 232 | |
| 233 | void MessageLoop::PostDelayedTask( |
| 234 | const tracked_objects::Location& from_here, Task* task, int delay_ms) { |
| 235 | PostTask_Helper(from_here, task, delay_ms, true); |
| 236 | } |
| 237 | |
| 238 | void MessageLoop::PostNonNestableTask( |
| 239 | const tracked_objects::Location& from_here, Task* task) { |
| 240 | PostTask_Helper(from_here, task, 0, false); |
| 241 | } |
| 242 | |
| 243 | void MessageLoop::PostNonNestableDelayedTask( |
| 244 | const tracked_objects::Location& from_here, Task* task, int delay_ms) { |
| 245 | PostTask_Helper(from_here, task, delay_ms, false); |
| 246 | } |
| 247 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 248 | // Possibly called on a background thread! |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 249 | void MessageLoop::PostTask_Helper( |
| 250 | const tracked_objects::Location& from_here, Task* task, int delay_ms, |
| 251 | bool nestable) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 252 | task->SetBirthPlace(from_here); |
[email protected] | 9bcbf47 | 2008-08-30 00:22:48 | [diff] [blame] | 253 | |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 254 | PendingTask pending_task(task, nestable); |
[email protected] | 9bcbf47 | 2008-08-30 00:22:48 | [diff] [blame] | 255 | |
| 256 | if (delay_ms > 0) { |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 257 | pending_task.delayed_run_time = |
[email protected] | 9bcbf47 | 2008-08-30 00:22:48 | [diff] [blame] | 258 | Time::Now() + TimeDelta::FromMilliseconds(delay_ms); |
| 259 | } else { |
| 260 | DCHECK(delay_ms == 0) << "delay should not be negative"; |
| 261 | } |
| 262 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 263 | // Warning: Don't try to short-circuit, and handle this thread's tasks more |
| 264 | // directly, as it could starve handling of foreign threads. Put every task |
| 265 | // into this queue. |
| 266 | |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 267 | scoped_refptr<base::MessagePump> pump; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 268 | { |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 269 | AutoLock locked(incoming_queue_lock_); |
| 270 | |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 271 | bool was_empty = incoming_queue_.empty(); |
| 272 | incoming_queue_.push(pending_task); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 273 | if (!was_empty) |
| 274 | return; // Someone else should have started the sub-pump. |
| 275 | |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 276 | pump = pump_; |
[email protected] | ea15e98 | 2008-08-15 07:31:20 | [diff] [blame] | 277 | } |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 278 | // Since the incoming_queue_ may contain a task that destroys this message |
| 279 | // loop, we cannot exit incoming_queue_lock_ until we are done with |this|. |
| 280 | // We use a stack-based reference to the message pump so that we can call |
| 281 | // ScheduleWork outside of incoming_queue_lock_. |
[email protected] | ea15e98 | 2008-08-15 07:31:20 | [diff] [blame] | 282 | |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 283 | pump->ScheduleWork(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | void MessageLoop::SetNestableTasksAllowed(bool allowed) { |
[email protected] | 124a2bdf | 2008-08-09 00:14:09 | [diff] [blame] | 287 | if (nestable_tasks_allowed_ != allowed) { |
| 288 | nestable_tasks_allowed_ = allowed; |
| 289 | if (!nestable_tasks_allowed_) |
| 290 | return; |
| 291 | // Start the native pump if we are not already pumping. |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 292 | pump_->ScheduleWork(); |
[email protected] | 124a2bdf | 2008-08-09 00:14:09 | [diff] [blame] | 293 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | bool MessageLoop::NestableTasksAllowed() const { |
| 297 | return nestable_tasks_allowed_; |
| 298 | } |
| 299 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 300 | //------------------------------------------------------------------------------ |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 301 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 302 | void MessageLoop::RunTask(Task* task) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 303 | DCHECK(nestable_tasks_allowed_); |
| 304 | // Execute the task and assume the worst: It is probably not reentrant. |
| 305 | nestable_tasks_allowed_ = false; |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 306 | |
| 307 | HistogramEvent(kTaskRunEvent); |
| 308 | task->Run(); |
| 309 | delete task; |
| 310 | |
| 311 | nestable_tasks_allowed_ = true; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 312 | } |
| 313 | |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 314 | bool MessageLoop::DeferOrRunPendingTask(const PendingTask& pending_task) { |
| 315 | if (pending_task.nestable || state_->run_depth == 1) { |
| 316 | RunTask(pending_task.task); |
| 317 | // Show that we ran a task (Note: a new one might arrive as a |
| 318 | // consequence!). |
| 319 | return true; |
| 320 | } |
| 321 | |
| 322 | // We couldn't run the task now because we're in a nested message loop |
| 323 | // and the task isn't nestable. |
| 324 | deferred_non_nestable_work_queue_.push(pending_task); |
| 325 | return false; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 326 | } |
| 327 | |
[email protected] | 001747c | 2008-09-10 00:37:07 | [diff] [blame] | 328 | void MessageLoop::AddToDelayedWorkQueue(const PendingTask& pending_task) { |
| 329 | // Move to the delayed work queue. Initialize the sequence number |
| 330 | // before inserting into the delayed_work_queue_. The sequence number |
| 331 | // is used to faciliate FIFO sorting when two tasks have the same |
| 332 | // delayed_run_time value. |
| 333 | PendingTask new_pending_task(pending_task); |
| 334 | new_pending_task.sequence_num = next_sequence_num_++; |
| 335 | delayed_work_queue_.push(new_pending_task); |
| 336 | } |
| 337 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 338 | void MessageLoop::ReloadWorkQueue() { |
| 339 | // We can improve performance of our loading tasks from incoming_queue_ to |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 340 | // work_queue_ by waiting until the last minute (work_queue_ is empty) to |
| 341 | // load. That reduces the number of locks-per-task significantly when our |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 342 | // queues get large. |
| 343 | if (!work_queue_.empty()) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 344 | return; // Wait till we *really* need to lock and load. |
| 345 | |
| 346 | // Acquire all we can from the inter-thread queue with one lock acquisition. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 347 | { |
| 348 | AutoLock lock(incoming_queue_lock_); |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 349 | if (incoming_queue_.empty()) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 350 | return; |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 351 | std::swap(incoming_queue_, work_queue_); |
| 352 | DCHECK(incoming_queue_.empty()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 353 | } |
| 354 | } |
| 355 | |
[email protected] | 001747c | 2008-09-10 00:37:07 | [diff] [blame] | 356 | bool MessageLoop::DeletePendingTasks() { |
| 357 | bool did_work = !work_queue_.empty(); |
| 358 | while (!work_queue_.empty()) { |
| 359 | PendingTask pending_task = work_queue_.front(); |
| 360 | work_queue_.pop(); |
| 361 | if (!pending_task.delayed_run_time.is_null()) { |
| 362 | // We want to delete delayed tasks in the same order in which they would |
| 363 | // normally be deleted in case of any funny dependencies between delayed |
| 364 | // tasks. |
| 365 | AddToDelayedWorkQueue(pending_task); |
| 366 | } else { |
| 367 | // TODO(darin): Delete all tasks once it is safe to do so. |
[email protected] | 2047283 | 2009-03-11 19:46:41 | [diff] [blame] | 368 | // Until it is totally safe, just do it when running purify. |
| 369 | #ifdef PURIFY |
| 370 | delete task; |
| 371 | #endif // PURIFY |
[email protected] | 001747c | 2008-09-10 00:37:07 | [diff] [blame] | 372 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 373 | } |
[email protected] | 001747c | 2008-09-10 00:37:07 | [diff] [blame] | 374 | did_work |= !deferred_non_nestable_work_queue_.empty(); |
| 375 | while (!deferred_non_nestable_work_queue_.empty()) { |
[email protected] | 2047283 | 2009-03-11 19:46:41 | [diff] [blame] | 376 | // TODO(darin): Delete all tasks once it is safe to do so. |
| 377 | // Until it is totaly safe, just delete them to keep purify happy. |
| 378 | #ifdef PURIFY |
[email protected] | 8df21d3 | 2009-03-11 19:53:50 | [diff] [blame^] | 379 | Task* task = deferred_non_nestable_work_queue_.front().task; |
| 380 | #endif |
| 381 | deferred_non_nestable_work_queue_.pop(); |
| 382 | #ifdef PURIFY |
[email protected] | 2047283 | 2009-03-11 19:46:41 | [diff] [blame] | 383 | delete task; |
| 384 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 385 | } |
[email protected] | 001747c | 2008-09-10 00:37:07 | [diff] [blame] | 386 | did_work |= !delayed_work_queue_.empty(); |
| 387 | while (!delayed_work_queue_.empty()) { |
| 388 | Task* task = delayed_work_queue_.top().task; |
| 389 | delayed_work_queue_.pop(); |
| 390 | delete task; |
| 391 | } |
| 392 | return did_work; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 393 | } |
| 394 | |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 395 | bool MessageLoop::DoWork() { |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 396 | if (!nestable_tasks_allowed_) { |
| 397 | // Task can't be executed right now. |
| 398 | return false; |
| 399 | } |
| 400 | |
| 401 | for (;;) { |
| 402 | ReloadWorkQueue(); |
| 403 | if (work_queue_.empty()) |
| 404 | break; |
| 405 | |
| 406 | // Execute oldest task. |
| 407 | do { |
| 408 | PendingTask pending_task = work_queue_.front(); |
| 409 | work_queue_.pop(); |
| 410 | if (!pending_task.delayed_run_time.is_null()) { |
[email protected] | 001747c | 2008-09-10 00:37:07 | [diff] [blame] | 411 | AddToDelayedWorkQueue(pending_task); |
[email protected] | 72deacd | 2008-09-23 19:19:20 | [diff] [blame] | 412 | // If we changed the topmost task, then it is time to re-schedule. |
| 413 | if (delayed_work_queue_.top().task == pending_task.task) |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 414 | pump_->ScheduleDelayedWork(pending_task.delayed_run_time); |
| 415 | } else { |
| 416 | if (DeferOrRunPendingTask(pending_task)) |
| 417 | return true; |
| 418 | } |
| 419 | } while (!work_queue_.empty()); |
| 420 | } |
| 421 | |
| 422 | // Nothing happened. |
| 423 | return false; |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 424 | } |
| 425 | |
[email protected] | b24250fc | 2008-08-20 06:30:58 | [diff] [blame] | 426 | bool MessageLoop::DoDelayedWork(Time* next_delayed_work_time) { |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 427 | if (!nestable_tasks_allowed_ || delayed_work_queue_.empty()) { |
| 428 | *next_delayed_work_time = Time(); |
| 429 | return false; |
| 430 | } |
[email protected] | 1d2eb13 | 2008-12-08 17:36:06 | [diff] [blame] | 431 | |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 432 | if (delayed_work_queue_.top().delayed_run_time > Time::Now()) { |
| 433 | *next_delayed_work_time = delayed_work_queue_.top().delayed_run_time; |
| 434 | return false; |
| 435 | } |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 436 | |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 437 | PendingTask pending_task = delayed_work_queue_.top(); |
| 438 | delayed_work_queue_.pop(); |
[email protected] | 1d2eb13 | 2008-12-08 17:36:06 | [diff] [blame] | 439 | |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 440 | if (!delayed_work_queue_.empty()) |
| 441 | *next_delayed_work_time = delayed_work_queue_.top().delayed_run_time; |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 442 | |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 443 | return DeferOrRunPendingTask(pending_task); |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | bool MessageLoop::DoIdleWork() { |
| 447 | if (ProcessNextDelayedNonNestableTask()) |
| 448 | return true; |
| 449 | |
| 450 | if (state_->quit_received) |
| 451 | pump_->Quit(); |
| 452 | |
| 453 | return false; |
| 454 | } |
| 455 | |
| 456 | //------------------------------------------------------------------------------ |
| 457 | // MessageLoop::AutoRunState |
| 458 | |
| 459 | MessageLoop::AutoRunState::AutoRunState(MessageLoop* loop) : loop_(loop) { |
| 460 | // Make the loop reference us. |
| 461 | previous_state_ = loop_->state_; |
| 462 | if (previous_state_) { |
| 463 | run_depth = previous_state_->run_depth + 1; |
[email protected] | ea15e98 | 2008-08-15 07:31:20 | [diff] [blame] | 464 | } else { |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 465 | run_depth = 1; |
[email protected] | ea15e98 | 2008-08-15 07:31:20 | [diff] [blame] | 466 | } |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 467 | loop_->state_ = this; |
| 468 | |
| 469 | // Initialize the other fields: |
| 470 | quit_received = false; |
| 471 | #if defined(OS_WIN) |
| 472 | dispatcher = NULL; |
| 473 | #endif |
| 474 | } |
| 475 | |
| 476 | MessageLoop::AutoRunState::~AutoRunState() { |
| 477 | loop_->state_ = previous_state_; |
[email protected] | a5b94a9 | 2008-08-12 23:25:43 | [diff] [blame] | 478 | } |
| 479 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 480 | //------------------------------------------------------------------------------ |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 481 | // MessageLoop::PendingTask |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 482 | |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 483 | bool MessageLoop::PendingTask::operator<(const PendingTask& other) const { |
| 484 | // Since the top of a priority queue is defined as the "greatest" element, we |
| 485 | // need to invert the comparison here. We want the smaller time to be at the |
| 486 | // top of the heap. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 487 | |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 488 | if (delayed_run_time < other.delayed_run_time) |
| 489 | return false; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 490 | |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 491 | if (delayed_run_time > other.delayed_run_time) |
| 492 | return true; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 493 | |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 494 | // If the times happen to match, then we use the sequence number to decide. |
| 495 | // Compare the difference to support integer roll-over. |
| 496 | return (sequence_num - other.sequence_num) > 0; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | //------------------------------------------------------------------------------ |
| 500 | // Method and data for histogramming events and actions taken by each instance |
| 501 | // on each thread. |
| 502 | |
| 503 | // static |
| 504 | bool MessageLoop::enable_histogrammer_ = false; |
| 505 | |
| 506 | // static |
| 507 | void MessageLoop::EnableHistogrammer(bool enable) { |
| 508 | enable_histogrammer_ = enable; |
| 509 | } |
| 510 | |
| 511 | void MessageLoop::StartHistogrammer() { |
| 512 | if (enable_histogrammer_ && !message_histogram_.get() |
| 513 | && StatisticsRecorder::WasStarted()) { |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 514 | DCHECK(!thread_name_.empty()); |
[email protected] | 553dba6 | 2009-02-24 19:08:23 | [diff] [blame] | 515 | message_histogram_.reset( |
| 516 | new LinearHistogram(("MsgLoop:" + thread_name_).c_str(), |
| 517 | kLeastNonZeroMessageId, |
| 518 | kMaxMessageId, |
| 519 | kNumberOfDistinctMessagesDisplayed)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 520 | message_histogram_->SetFlags(message_histogram_->kHexRangePrintingFlag); |
| 521 | message_histogram_->SetRangeDescriptions(event_descriptions_); |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | void MessageLoop::HistogramEvent(int event) { |
| 526 | if (message_histogram_.get()) |
| 527 | message_histogram_->Add(event); |
| 528 | } |
| 529 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 530 | // Provide a macro that takes an expression (such as a constant, or macro |
| 531 | // constant) and creates a pair to initalize an array of pairs. In this case, |
| 532 | // our pair consists of the expressions value, and the "stringized" version |
| 533 | // of the expression (i.e., the exrpression put in quotes). For example, if |
| 534 | // we have: |
| 535 | // #define FOO 2 |
| 536 | // #define BAR 5 |
| 537 | // then the following: |
| 538 | // VALUE_TO_NUMBER_AND_NAME(FOO + BAR) |
| 539 | // will expand to: |
| 540 | // {7, "FOO + BAR"} |
| 541 | // We use the resulting array as an argument to our histogram, which reads the |
| 542 | // number as a bucket identifier, and proceeds to use the corresponding name |
| 543 | // in the pair (i.e., the quoted string) when printing out a histogram. |
| 544 | #define VALUE_TO_NUMBER_AND_NAME(name) {name, #name}, |
| 545 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 546 | // static |
| 547 | const LinearHistogram::DescriptionPair MessageLoop::event_descriptions_[] = { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 548 | // Provide some pretty print capability in our histogram for our internal |
| 549 | // messages. |
| 550 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 551 | // A few events we handle (kindred to messages), and used to profile actions. |
| 552 | VALUE_TO_NUMBER_AND_NAME(kTaskRunEvent) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 553 | VALUE_TO_NUMBER_AND_NAME(kTimerEvent) |
| 554 | |
| 555 | {-1, NULL} // The list must be null terminated, per API to histogram. |
| 556 | }; |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 557 | |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 558 | //------------------------------------------------------------------------------ |
| 559 | // MessageLoopForUI |
| 560 | |
| 561 | #if defined(OS_WIN) |
| 562 | |
| 563 | void MessageLoopForUI::Run(Dispatcher* dispatcher) { |
| 564 | AutoRunState save_state(this); |
| 565 | state_->dispatcher = dispatcher; |
| 566 | RunHandler(); |
| 567 | } |
| 568 | |
| 569 | void MessageLoopForUI::AddObserver(Observer* observer) { |
| 570 | pump_win()->AddObserver(observer); |
| 571 | } |
| 572 | |
| 573 | void MessageLoopForUI::RemoveObserver(Observer* observer) { |
| 574 | pump_win()->RemoveObserver(observer); |
| 575 | } |
| 576 | |
| 577 | void MessageLoopForUI::WillProcessMessage(const MSG& message) { |
| 578 | pump_win()->WillProcessMessage(message); |
| 579 | } |
| 580 | void MessageLoopForUI::DidProcessMessage(const MSG& message) { |
| 581 | pump_win()->DidProcessMessage(message); |
| 582 | } |
| 583 | void MessageLoopForUI::PumpOutPendingPaintMessages() { |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 584 | pump_ui()->PumpOutPendingPaintMessages(); |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | #endif // defined(OS_WIN) |
| 588 | |
| 589 | //------------------------------------------------------------------------------ |
| 590 | // MessageLoopForIO |
| 591 | |
| 592 | #if defined(OS_WIN) |
| 593 | |
[email protected] | 32cda29d | 2008-10-09 23:58:43 | [diff] [blame] | 594 | void MessageLoopForIO::RegisterIOHandler(HANDLE file, IOHandler* handler) { |
| 595 | pump_io()->RegisterIOHandler(file, handler); |
| 596 | } |
| 597 | |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 598 | bool MessageLoopForIO::WaitForIOCompletion(DWORD timeout, IOHandler* filter) { |
| 599 | return pump_io()->WaitForIOCompletion(timeout, filter); |
[email protected] | 32cda29d | 2008-10-09 23:58:43 | [diff] [blame] | 600 | } |
| 601 | |
[email protected] | 36987e9 | 2008-09-18 18:46:26 | [diff] [blame] | 602 | #elif defined(OS_POSIX) |
| 603 | |
[email protected] | e45e6c0 | 2008-12-15 22:02:17 | [diff] [blame] | 604 | bool MessageLoopForIO::WatchFileDescriptor(int fd, |
| 605 | bool persistent, |
| 606 | Mode mode, |
| 607 | FileDescriptorWatcher *controller, |
| 608 | Watcher *delegate) { |
| 609 | return pump_libevent()->WatchFileDescriptor( |
| 610 | fd, |
| 611 | persistent, |
| 612 | static_cast<base::MessagePumpLibevent::Mode>(mode), |
| 613 | controller, |
| 614 | delegate); |
[email protected] | 36987e9 | 2008-09-18 18:46:26 | [diff] [blame] | 615 | } |
| 616 | |
[email protected] | 36987e9 | 2008-09-18 18:46:26 | [diff] [blame] | 617 | #endif |