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