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