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