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