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