[email protected] | e6e55fb | 2010-04-15 01:04:29 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 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" |
[email protected] | 835d7c8 | 2010-10-14 04:38:38 | [diff] [blame] | 13 | #include "base/metrics/histogram.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" |
[email protected] | d680c5c | 2009-05-14 16:44:26 | [diff] [blame] | 21 | #include "base/third_party/valgrind/valgrind.h" |
[email protected] | 36987e9 | 2008-09-18 18:46:26 | [diff] [blame] | 22 | #endif |
[email protected] | e43eddf1 | 2009-12-29 00:32:52 | [diff] [blame] | 23 | #if defined(OS_POSIX) && !defined(OS_MACOSX) |
[email protected] | 8fc3a48 | 2008-10-03 16:52:59 | [diff] [blame] | 24 | #include "base/message_pump_glib.h" |
| 25 | #endif |
[email protected] | 71ad9c6f | 2010-10-22 16:17:47 | [diff] [blame] | 26 | #if defined(TOUCH_UI) |
| 27 | #include "base/message_pump_glib_x.h" |
| 28 | #endif |
[email protected] | 36987e9 | 2008-09-18 18:46:26 | [diff] [blame] | 29 | |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 30 | using base::Time; |
| 31 | using base::TimeDelta; |
[email protected] | 7e7fab4 | 2010-11-06 22:23:29 | [diff] [blame] | 32 | using base::TimeTicks; |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 33 | |
[email protected] | 5097dc8 | 2010-07-15 17:23:23 | [diff] [blame] | 34 | namespace { |
| 35 | |
[email protected] | f886b7bf | 2008-09-10 10:54:06 | [diff] [blame] | 36 | // A lazily created thread local storage for quick access to a thread's message |
| 37 | // loop, if one exists. This should be safe and free of static constructors. |
[email protected] | 5097dc8 | 2010-07-15 17:23:23 | [diff] [blame] | 38 | base::LazyInstance<base::ThreadLocalPointer<MessageLoop> > lazy_tls_ptr( |
[email protected] | f886b7bf | 2008-09-10 10:54:06 | [diff] [blame] | 39 | base::LINKER_INITIALIZED); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 40 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 41 | // Logical events for Histogram profiling. Run with -message-loop-histogrammer |
| 42 | // to get an accounting of messages and actions taken on each thread. |
[email protected] | 5097dc8 | 2010-07-15 17:23:23 | [diff] [blame] | 43 | const int kTaskRunEvent = 0x1; |
| 44 | const int kTimerEvent = 0x2; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 45 | |
| 46 | // Provide range of message IDs for use in histogramming and debug display. |
[email protected] | 5097dc8 | 2010-07-15 17:23:23 | [diff] [blame] | 47 | const int kLeastNonZeroMessageId = 1; |
| 48 | const int kMaxMessageId = 1099; |
| 49 | const int kNumberOfDistinctMessagesDisplayed = 1100; |
| 50 | |
| 51 | // Provide a macro that takes an expression (such as a constant, or macro |
| 52 | // constant) and creates a pair to initalize an array of pairs. In this case, |
| 53 | // our pair consists of the expressions value, and the "stringized" version |
| 54 | // of the expression (i.e., the exrpression put in quotes). For example, if |
| 55 | // we have: |
| 56 | // #define FOO 2 |
| 57 | // #define BAR 5 |
| 58 | // then the following: |
| 59 | // VALUE_TO_NUMBER_AND_NAME(FOO + BAR) |
| 60 | // will expand to: |
| 61 | // {7, "FOO + BAR"} |
| 62 | // We use the resulting array as an argument to our histogram, which reads the |
| 63 | // number as a bucket identifier, and proceeds to use the corresponding name |
| 64 | // in the pair (i.e., the quoted string) when printing out a histogram. |
| 65 | #define VALUE_TO_NUMBER_AND_NAME(name) {name, #name}, |
| 66 | |
[email protected] | 835d7c8 | 2010-10-14 04:38:38 | [diff] [blame] | 67 | const base::LinearHistogram::DescriptionPair event_descriptions_[] = { |
[email protected] | 5097dc8 | 2010-07-15 17:23:23 | [diff] [blame] | 68 | // Provide some pretty print capability in our histogram for our internal |
| 69 | // messages. |
| 70 | |
| 71 | // A few events we handle (kindred to messages), and used to profile actions. |
| 72 | VALUE_TO_NUMBER_AND_NAME(kTaskRunEvent) |
| 73 | VALUE_TO_NUMBER_AND_NAME(kTimerEvent) |
| 74 | |
| 75 | {-1, NULL} // The list must be null terminated, per API to histogram. |
| 76 | }; |
| 77 | |
| 78 | bool enable_histogrammer_ = false; |
| 79 | |
| 80 | } // namespace |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 81 | |
| 82 | //------------------------------------------------------------------------------ |
| 83 | |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 84 | #if defined(OS_WIN) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 85 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 86 | // Upon a SEH exception in this thread, it restores the original unhandled |
| 87 | // exception filter. |
| 88 | static int SEHFilter(LPTOP_LEVEL_EXCEPTION_FILTER old_filter) { |
| 89 | ::SetUnhandledExceptionFilter(old_filter); |
| 90 | return EXCEPTION_CONTINUE_SEARCH; |
| 91 | } |
| 92 | |
| 93 | // Retrieves a pointer to the current unhandled exception filter. There |
| 94 | // is no standalone getter method. |
| 95 | static LPTOP_LEVEL_EXCEPTION_FILTER GetTopSEHFilter() { |
| 96 | LPTOP_LEVEL_EXCEPTION_FILTER top_filter = NULL; |
| 97 | top_filter = ::SetUnhandledExceptionFilter(0); |
| 98 | ::SetUnhandledExceptionFilter(top_filter); |
| 99 | return top_filter; |
| 100 | } |
| 101 | |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 102 | #endif // defined(OS_WIN) |
| 103 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 104 | //------------------------------------------------------------------------------ |
| 105 | |
[email protected] | 3a3d4747 | 2010-07-15 21:03:54 | [diff] [blame] | 106 | MessageLoop::TaskObserver::TaskObserver() { |
| 107 | } |
| 108 | |
| 109 | MessageLoop::TaskObserver::~TaskObserver() { |
| 110 | } |
| 111 | |
| 112 | MessageLoop::DestructionObserver::~DestructionObserver() { |
| 113 | } |
| 114 | |
| 115 | //------------------------------------------------------------------------------ |
| 116 | |
[email protected] | f886b7bf | 2008-09-10 10:54:06 | [diff] [blame] | 117 | // static |
| 118 | MessageLoop* MessageLoop::current() { |
| 119 | // TODO(darin): sadly, we cannot enable this yet since people call us even |
| 120 | // when they have no intention of using us. |
[email protected] | 2fdc86a | 2010-01-26 23:08:02 | [diff] [blame] | 121 | // DCHECK(loop) << "Ouch, did you forget to initialize me?"; |
[email protected] | f886b7bf | 2008-09-10 10:54:06 | [diff] [blame] | 122 | return lazy_tls_ptr.Pointer()->Get(); |
| 123 | } |
| 124 | |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 125 | MessageLoop::MessageLoop(Type type) |
| 126 | : type_(type), |
[email protected] | a5b94a9 | 2008-08-12 23:25:43 | [diff] [blame] | 127 | nestable_tasks_allowed_(true), |
[email protected] | b16ef31 | 2008-08-19 18:36:23 | [diff] [blame] | 128 | exception_restoration_(false), |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 129 | state_(NULL), |
| 130 | next_sequence_num_(0) { |
[email protected] | f886b7bf | 2008-09-10 10:54:06 | [diff] [blame] | 131 | DCHECK(!current()) << "should only have one message loop per thread"; |
| 132 | lazy_tls_ptr.Pointer()->Set(this); |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 133 | |
[email protected] | e6e55fb | 2010-04-15 01:04:29 | [diff] [blame] | 134 | // TODO(rvargas): Get rid of the OS guards. |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 135 | #if defined(OS_WIN) |
[email protected] | e6e55fb | 2010-04-15 01:04:29 | [diff] [blame] | 136 | #define MESSAGE_PUMP_UI new base::MessagePumpForUI() |
| 137 | #define MESSAGE_PUMP_IO new base::MessagePumpForIO() |
| 138 | #elif defined(OS_MACOSX) |
| 139 | #define MESSAGE_PUMP_UI base::MessagePumpMac::Create() |
| 140 | #define MESSAGE_PUMP_IO new base::MessagePumpLibevent() |
[email protected] | 71ad9c6f | 2010-10-22 16:17:47 | [diff] [blame] | 141 | #elif defined(TOUCH_UI) |
[email protected] | f9c8d93 | 2010-11-03 22:11:41 | [diff] [blame] | 142 | // TODO(sadrul): enable the new message pump when ready |
| 143 | #define MESSAGE_PUMP_UI new base::MessagePumpForUI() |
[email protected] | 71ad9c6f | 2010-10-22 16:17:47 | [diff] [blame] | 144 | #define MESSAGE_PUMP_IO new base::MessagePumpLibevent() |
[email protected] | e6e55fb | 2010-04-15 01:04:29 | [diff] [blame] | 145 | #elif defined(OS_POSIX) // POSIX but not MACOSX. |
| 146 | #define MESSAGE_PUMP_UI new base::MessagePumpForUI() |
| 147 | #define MESSAGE_PUMP_IO new base::MessagePumpLibevent() |
[email protected] | e43eddf1 | 2009-12-29 00:32:52 | [diff] [blame] | 148 | #else |
[email protected] | e6e55fb | 2010-04-15 01:04:29 | [diff] [blame] | 149 | #error Not implemented |
[email protected] | e43eddf1 | 2009-12-29 00:32:52 | [diff] [blame] | 150 | #endif |
[email protected] | e6e55fb | 2010-04-15 01:04:29 | [diff] [blame] | 151 | |
| 152 | if (type_ == TYPE_UI) { |
| 153 | pump_ = MESSAGE_PUMP_UI; |
[email protected] | 8fc3a48 | 2008-10-03 16:52:59 | [diff] [blame] | 154 | } else if (type_ == TYPE_IO) { |
[email protected] | e6e55fb | 2010-04-15 01:04:29 | [diff] [blame] | 155 | pump_ = MESSAGE_PUMP_IO; |
[email protected] | 36987e9 | 2008-09-18 18:46:26 | [diff] [blame] | 156 | } else { |
[email protected] | e6e55fb | 2010-04-15 01:04:29 | [diff] [blame] | 157 | DCHECK_EQ(TYPE_DEFAULT, type_); |
[email protected] | 36987e9 | 2008-09-18 18:46:26 | [diff] [blame] | 158 | pump_ = new base::MessagePumpDefault(); |
| 159 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | MessageLoop::~MessageLoop() { |
[email protected] | c3bf698 | 2010-11-10 20:28:06 | [diff] [blame^] | 163 | DCHECK_EQ(this, current()); |
[email protected] | 2a12725 | 2008-08-05 23:16:41 | [diff] [blame] | 164 | |
| 165 | // Let interested parties have one last shot at accessing this. |
| 166 | FOR_EACH_OBSERVER(DestructionObserver, destruction_observers_, |
| 167 | WillDestroyCurrentMessageLoop()); |
| 168 | |
[email protected] | 08de3cde | 2008-09-09 05:55:35 | [diff] [blame] | 169 | DCHECK(!state_); |
| 170 | |
[email protected] | 001747c | 2008-09-10 00:37:07 | [diff] [blame] | 171 | // Clean up any unprocessed tasks, but take care: deleting a task could |
| 172 | // result in the addition of more tasks (e.g., via DeleteSoon). We set a |
| 173 | // limit on the number of times we will allow a deleted task to generate more |
| 174 | // tasks. Normally, we should only pass through this loop once or twice. If |
| 175 | // we end up hitting the loop limit, then it is probably due to one task that |
| 176 | // is being stubborn. Inspect the queues to see who is left. |
| 177 | bool did_work; |
| 178 | for (int i = 0; i < 100; ++i) { |
| 179 | DeletePendingTasks(); |
| 180 | ReloadWorkQueue(); |
| 181 | // If we end up with empty queues, then break out of the loop. |
| 182 | did_work = DeletePendingTasks(); |
| 183 | if (!did_work) |
| 184 | break; |
[email protected] | 08de3cde | 2008-09-09 05:55:35 | [diff] [blame] | 185 | } |
[email protected] | 001747c | 2008-09-10 00:37:07 | [diff] [blame] | 186 | DCHECK(!did_work); |
| 187 | |
| 188 | // OK, now make it so that no one can find us. |
[email protected] | 2b89d22b2 | 2008-09-10 11:14:56 | [diff] [blame] | 189 | lazy_tls_ptr.Pointer()->Set(NULL); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 190 | } |
| 191 | |
[email protected] | 23c386b | 2010-09-15 22:14:36 | [diff] [blame] | 192 | void MessageLoop::AddDestructionObserver( |
| 193 | DestructionObserver* destruction_observer) { |
[email protected] | c3bf698 | 2010-11-10 20:28:06 | [diff] [blame^] | 194 | DCHECK_EQ(this, current()); |
[email protected] | 23c386b | 2010-09-15 22:14:36 | [diff] [blame] | 195 | destruction_observers_.AddObserver(destruction_observer); |
[email protected] | 2a12725 | 2008-08-05 23:16:41 | [diff] [blame] | 196 | } |
| 197 | |
[email protected] | 23c386b | 2010-09-15 22:14:36 | [diff] [blame] | 198 | void MessageLoop::RemoveDestructionObserver( |
| 199 | DestructionObserver* destruction_observer) { |
[email protected] | c3bf698 | 2010-11-10 20:28:06 | [diff] [blame^] | 200 | DCHECK_EQ(this, current()); |
[email protected] | 23c386b | 2010-09-15 22:14:36 | [diff] [blame] | 201 | destruction_observers_.RemoveObserver(destruction_observer); |
[email protected] | 2a12725 | 2008-08-05 23:16:41 | [diff] [blame] | 202 | } |
| 203 | |
[email protected] | 23c386b | 2010-09-15 22:14:36 | [diff] [blame] | 204 | void MessageLoop::AddTaskObserver(TaskObserver* task_observer) { |
[email protected] | 9cfb89a | 2010-06-09 21:20:41 | [diff] [blame] | 205 | DCHECK_EQ(this, current()); |
[email protected] | 23c386b | 2010-09-15 22:14:36 | [diff] [blame] | 206 | task_observers_.AddObserver(task_observer); |
[email protected] | 9cfb89a | 2010-06-09 21:20:41 | [diff] [blame] | 207 | } |
| 208 | |
[email protected] | 23c386b | 2010-09-15 22:14:36 | [diff] [blame] | 209 | void MessageLoop::RemoveTaskObserver(TaskObserver* task_observer) { |
[email protected] | 9cfb89a | 2010-06-09 21:20:41 | [diff] [blame] | 210 | DCHECK_EQ(this, current()); |
[email protected] | 23c386b | 2010-09-15 22:14:36 | [diff] [blame] | 211 | task_observers_.RemoveObserver(task_observer); |
[email protected] | 9cfb89a | 2010-06-09 21:20:41 | [diff] [blame] | 212 | } |
| 213 | |
[email protected] | ea15e98 | 2008-08-15 07:31:20 | [diff] [blame] | 214 | void MessageLoop::Run() { |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 215 | AutoRunState save_state(this); |
| 216 | RunHandler(); |
[email protected] | ea15e98 | 2008-08-15 07:31:20 | [diff] [blame] | 217 | } |
| 218 | |
[email protected] | 7e0e876 | 2008-07-31 13:10:20 | [diff] [blame] | 219 | void MessageLoop::RunAllPending() { |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 220 | AutoRunState save_state(this); |
| 221 | state_->quit_received = true; // Means run until we would otherwise block. |
| 222 | RunHandler(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | // Runs the loop in two different SEH modes: |
| 226 | // enable_SEH_restoration_ = false : any unhandled exception goes to the last |
| 227 | // one that calls SetUnhandledExceptionFilter(). |
| 228 | // enable_SEH_restoration_ = true : any unhandled exception goes to the filter |
| 229 | // that was existed before the loop was run. |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 230 | void MessageLoop::RunHandler() { |
| 231 | #if defined(OS_WIN) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 232 | if (exception_restoration_) { |
[email protected] | aff8a13 | 2009-10-26 18:15:59 | [diff] [blame] | 233 | RunInternalInSEHFrame(); |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 234 | return; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 235 | } |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 236 | #endif |
| 237 | |
| 238 | RunInternal(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 239 | } |
[email protected] | aff8a13 | 2009-10-26 18:15:59 | [diff] [blame] | 240 | //------------------------------------------------------------------------------ |
| 241 | #if defined(OS_WIN) |
| 242 | __declspec(noinline) void MessageLoop::RunInternalInSEHFrame() { |
| 243 | LPTOP_LEVEL_EXCEPTION_FILTER current_filter = GetTopSEHFilter(); |
| 244 | __try { |
| 245 | RunInternal(); |
| 246 | } __except(SEHFilter(current_filter)) { |
| 247 | } |
| 248 | return; |
| 249 | } |
| 250 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 251 | //------------------------------------------------------------------------------ |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 252 | |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 253 | void MessageLoop::RunInternal() { |
[email protected] | c3bf698 | 2010-11-10 20:28:06 | [diff] [blame^] | 254 | DCHECK_EQ(this, current()); |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 255 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 256 | StartHistogrammer(); |
| 257 | |
[email protected] | e43eddf1 | 2009-12-29 00:32:52 | [diff] [blame] | 258 | #if !defined(OS_MACOSX) |
[email protected] | 148d105 | 2009-07-31 22:53:37 | [diff] [blame] | 259 | if (state_->dispatcher && type() == TYPE_UI) { |
| 260 | static_cast<base::MessagePumpForUI*>(pump_.get())-> |
| 261 | RunWithDispatcher(this, state_->dispatcher); |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 262 | return; |
[email protected] | 7e0e876 | 2008-07-31 13:10:20 | [diff] [blame] | 263 | } |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 264 | #endif |
[email protected] | 1d2eb13 | 2008-12-08 17:36:06 | [diff] [blame] | 265 | |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 266 | pump_->Run(this); |
[email protected] | b8f2fe5d | 2008-07-30 07:50:53 | [diff] [blame] | 267 | } |
[email protected] | 7622bd0 | 2008-07-30 06:58:56 | [diff] [blame] | 268 | |
[email protected] | 3882c433 | 2008-07-30 19:03:59 | [diff] [blame] | 269 | //------------------------------------------------------------------------------ |
| 270 | // Wrapper functions for use in above message loop framework. |
| 271 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 272 | bool MessageLoop::ProcessNextDelayedNonNestableTask() { |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 273 | if (state_->run_depth != 1) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 274 | return false; |
| 275 | |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 276 | if (deferred_non_nestable_work_queue_.empty()) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 277 | return false; |
[email protected] | 1d2eb13 | 2008-12-08 17:36:06 | [diff] [blame] | 278 | |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 279 | Task* task = deferred_non_nestable_work_queue_.front().task; |
| 280 | deferred_non_nestable_work_queue_.pop(); |
[email protected] | 1d2eb13 | 2008-12-08 17:36:06 | [diff] [blame] | 281 | |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 282 | RunTask(task); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 283 | return true; |
| 284 | } |
| 285 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 286 | //------------------------------------------------------------------------------ |
| 287 | |
| 288 | void MessageLoop::Quit() { |
[email protected] | c3bf698 | 2010-11-10 20:28:06 | [diff] [blame^] | 289 | DCHECK_EQ(this, current()); |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 290 | if (state_) { |
| 291 | state_->quit_received = true; |
| 292 | } else { |
| 293 | NOTREACHED() << "Must be inside Run to call Quit"; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 294 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 295 | } |
| 296 | |
[email protected] | 781a7ed | 2010-02-23 07:12:22 | [diff] [blame] | 297 | void MessageLoop::QuitNow() { |
[email protected] | c3bf698 | 2010-11-10 20:28:06 | [diff] [blame^] | 298 | DCHECK_EQ(this, current()); |
[email protected] | 781a7ed | 2010-02-23 07:12:22 | [diff] [blame] | 299 | if (state_) { |
| 300 | pump_->Quit(); |
| 301 | } else { |
| 302 | NOTREACHED() << "Must be inside Run to call Quit"; |
| 303 | } |
| 304 | } |
| 305 | |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 306 | void MessageLoop::PostTask( |
| 307 | const tracked_objects::Location& from_here, Task* task) { |
| 308 | PostTask_Helper(from_here, task, 0, true); |
| 309 | } |
| 310 | |
| 311 | void MessageLoop::PostDelayedTask( |
[email protected] | 743ace4 | 2009-06-17 17:23:51 | [diff] [blame] | 312 | const tracked_objects::Location& from_here, Task* task, int64 delay_ms) { |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 313 | PostTask_Helper(from_here, task, delay_ms, true); |
| 314 | } |
| 315 | |
| 316 | void MessageLoop::PostNonNestableTask( |
| 317 | const tracked_objects::Location& from_here, Task* task) { |
| 318 | PostTask_Helper(from_here, task, 0, false); |
| 319 | } |
| 320 | |
| 321 | void MessageLoop::PostNonNestableDelayedTask( |
[email protected] | 743ace4 | 2009-06-17 17:23:51 | [diff] [blame] | 322 | const tracked_objects::Location& from_here, Task* task, int64 delay_ms) { |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 323 | PostTask_Helper(from_here, task, delay_ms, false); |
| 324 | } |
| 325 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 326 | // Possibly called on a background thread! |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 327 | void MessageLoop::PostTask_Helper( |
[email protected] | 743ace4 | 2009-06-17 17:23:51 | [diff] [blame] | 328 | const tracked_objects::Location& from_here, Task* task, int64 delay_ms, |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 329 | bool nestable) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 330 | task->SetBirthPlace(from_here); |
[email protected] | 9bcbf47 | 2008-08-30 00:22:48 | [diff] [blame] | 331 | |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 332 | PendingTask pending_task(task, nestable); |
[email protected] | 9bcbf47 | 2008-08-30 00:22:48 | [diff] [blame] | 333 | |
| 334 | if (delay_ms > 0) { |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 335 | pending_task.delayed_run_time = |
[email protected] | 7e7fab4 | 2010-11-06 22:23:29 | [diff] [blame] | 336 | TimeTicks::Now() + TimeDelta::FromMilliseconds(delay_ms); |
[email protected] | 57f030a | 2010-06-29 04:58:15 | [diff] [blame] | 337 | |
| 338 | #if defined(OS_WIN) |
| 339 | if (high_resolution_timer_expiration_.is_null()) { |
| 340 | // Windows timers are granular to 15.6ms. If we only set high-res |
| 341 | // timers for those under 15.6ms, then a 18ms timer ticks at ~32ms, |
| 342 | // which as a percentage is pretty inaccurate. So enable high |
| 343 | // res timers for any timer which is within 2x of the granularity. |
| 344 | // This is a tradeoff between accuracy and power management. |
| 345 | bool needs_high_res_timers = |
| 346 | delay_ms < (2 * Time::kMinLowResolutionThresholdMs); |
| 347 | if (needs_high_res_timers) { |
[email protected] | 21766d75 | 2010-10-20 10:50:38 | [diff] [blame] | 348 | Time::ActivateHighResolutionTimer(true); |
[email protected] | 7e7fab4 | 2010-11-06 22:23:29 | [diff] [blame] | 349 | high_resolution_timer_expiration_ = TimeTicks::Now() + |
[email protected] | 21766d75 | 2010-10-20 10:50:38 | [diff] [blame] | 350 | TimeDelta::FromMilliseconds(kHighResolutionTimerModeLeaseTimeMs); |
[email protected] | 57f030a | 2010-06-29 04:58:15 | [diff] [blame] | 351 | } |
| 352 | } |
| 353 | #endif |
[email protected] | 9bcbf47 | 2008-08-30 00:22:48 | [diff] [blame] | 354 | } else { |
[email protected] | 2753b39 | 2009-12-28 06:59:52 | [diff] [blame] | 355 | DCHECK_EQ(delay_ms, 0) << "delay should not be negative"; |
[email protected] | 9bcbf47 | 2008-08-30 00:22:48 | [diff] [blame] | 356 | } |
| 357 | |
[email protected] | 57f030a | 2010-06-29 04:58:15 | [diff] [blame] | 358 | #if defined(OS_WIN) |
| 359 | if (!high_resolution_timer_expiration_.is_null()) { |
[email protected] | 7e7fab4 | 2010-11-06 22:23:29 | [diff] [blame] | 360 | if (TimeTicks::Now() > high_resolution_timer_expiration_) { |
[email protected] | 57f030a | 2010-06-29 04:58:15 | [diff] [blame] | 361 | Time::ActivateHighResolutionTimer(false); |
[email protected] | 7e7fab4 | 2010-11-06 22:23:29 | [diff] [blame] | 362 | high_resolution_timer_expiration_ = TimeTicks(); |
[email protected] | 57f030a | 2010-06-29 04:58:15 | [diff] [blame] | 363 | } |
| 364 | } |
| 365 | #endif |
| 366 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 367 | // Warning: Don't try to short-circuit, and handle this thread's tasks more |
| 368 | // directly, as it could starve handling of foreign threads. Put every task |
| 369 | // into this queue. |
| 370 | |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 371 | scoped_refptr<base::MessagePump> pump; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 372 | { |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 373 | AutoLock locked(incoming_queue_lock_); |
| 374 | |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 375 | bool was_empty = incoming_queue_.empty(); |
| 376 | incoming_queue_.push(pending_task); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 377 | if (!was_empty) |
| 378 | return; // Someone else should have started the sub-pump. |
| 379 | |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 380 | pump = pump_; |
[email protected] | ea15e98 | 2008-08-15 07:31:20 | [diff] [blame] | 381 | } |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 382 | // Since the incoming_queue_ may contain a task that destroys this message |
| 383 | // loop, we cannot exit incoming_queue_lock_ until we are done with |this|. |
| 384 | // We use a stack-based reference to the message pump so that we can call |
| 385 | // ScheduleWork outside of incoming_queue_lock_. |
[email protected] | ea15e98 | 2008-08-15 07:31:20 | [diff] [blame] | 386 | |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 387 | pump->ScheduleWork(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | void MessageLoop::SetNestableTasksAllowed(bool allowed) { |
[email protected] | 124a2bdf | 2008-08-09 00:14:09 | [diff] [blame] | 391 | if (nestable_tasks_allowed_ != allowed) { |
| 392 | nestable_tasks_allowed_ = allowed; |
| 393 | if (!nestable_tasks_allowed_) |
| 394 | return; |
| 395 | // Start the native pump if we are not already pumping. |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 396 | pump_->ScheduleWork(); |
[email protected] | 124a2bdf | 2008-08-09 00:14:09 | [diff] [blame] | 397 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | bool MessageLoop::NestableTasksAllowed() const { |
| 401 | return nestable_tasks_allowed_; |
| 402 | } |
| 403 | |
[email protected] | b5f9510 | 2009-07-01 19:53:59 | [diff] [blame] | 404 | bool MessageLoop::IsNested() { |
| 405 | return state_->run_depth > 1; |
| 406 | } |
| 407 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 408 | //------------------------------------------------------------------------------ |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 409 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 410 | void MessageLoop::RunTask(Task* task) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 411 | DCHECK(nestable_tasks_allowed_); |
| 412 | // Execute the task and assume the worst: It is probably not reentrant. |
| 413 | nestable_tasks_allowed_ = false; |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 414 | |
| 415 | HistogramEvent(kTaskRunEvent); |
[email protected] | 9cfb89a | 2010-06-09 21:20:41 | [diff] [blame] | 416 | FOR_EACH_OBSERVER(TaskObserver, task_observers_, |
[email protected] | 02468668 | 2010-10-26 23:40:48 | [diff] [blame] | 417 | WillProcessTask(task)); |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 418 | task->Run(); |
[email protected] | 02468668 | 2010-10-26 23:40:48 | [diff] [blame] | 419 | FOR_EACH_OBSERVER(TaskObserver, task_observers_, DidProcessTask(task)); |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 420 | delete task; |
| 421 | |
| 422 | nestable_tasks_allowed_ = true; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 423 | } |
| 424 | |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 425 | bool MessageLoop::DeferOrRunPendingTask(const PendingTask& pending_task) { |
| 426 | if (pending_task.nestable || state_->run_depth == 1) { |
| 427 | RunTask(pending_task.task); |
| 428 | // Show that we ran a task (Note: a new one might arrive as a |
| 429 | // consequence!). |
| 430 | return true; |
| 431 | } |
| 432 | |
| 433 | // We couldn't run the task now because we're in a nested message loop |
| 434 | // and the task isn't nestable. |
| 435 | deferred_non_nestable_work_queue_.push(pending_task); |
| 436 | return false; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 437 | } |
| 438 | |
[email protected] | 001747c | 2008-09-10 00:37:07 | [diff] [blame] | 439 | void MessageLoop::AddToDelayedWorkQueue(const PendingTask& pending_task) { |
| 440 | // Move to the delayed work queue. Initialize the sequence number |
| 441 | // before inserting into the delayed_work_queue_. The sequence number |
| 442 | // is used to faciliate FIFO sorting when two tasks have the same |
| 443 | // delayed_run_time value. |
| 444 | PendingTask new_pending_task(pending_task); |
| 445 | new_pending_task.sequence_num = next_sequence_num_++; |
| 446 | delayed_work_queue_.push(new_pending_task); |
| 447 | } |
| 448 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 449 | void MessageLoop::ReloadWorkQueue() { |
| 450 | // We can improve performance of our loading tasks from incoming_queue_ to |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 451 | // work_queue_ by waiting until the last minute (work_queue_ is empty) to |
| 452 | // load. That reduces the number of locks-per-task significantly when our |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 453 | // queues get large. |
| 454 | if (!work_queue_.empty()) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 455 | return; // Wait till we *really* need to lock and load. |
| 456 | |
| 457 | // Acquire all we can from the inter-thread queue with one lock acquisition. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 458 | { |
| 459 | AutoLock lock(incoming_queue_lock_); |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 460 | if (incoming_queue_.empty()) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 461 | return; |
[email protected] | b2f0ea1 | 2009-09-02 20:05:21 | [diff] [blame] | 462 | incoming_queue_.Swap(&work_queue_); // Constant time |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 463 | DCHECK(incoming_queue_.empty()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 464 | } |
| 465 | } |
| 466 | |
[email protected] | 001747c | 2008-09-10 00:37:07 | [diff] [blame] | 467 | bool MessageLoop::DeletePendingTasks() { |
| 468 | bool did_work = !work_queue_.empty(); |
| 469 | while (!work_queue_.empty()) { |
| 470 | PendingTask pending_task = work_queue_.front(); |
| 471 | work_queue_.pop(); |
| 472 | if (!pending_task.delayed_run_time.is_null()) { |
| 473 | // We want to delete delayed tasks in the same order in which they would |
| 474 | // normally be deleted in case of any funny dependencies between delayed |
| 475 | // tasks. |
| 476 | AddToDelayedWorkQueue(pending_task); |
| 477 | } else { |
[email protected] | 24db026 | 2010-08-03 03:06:21 | [diff] [blame] | 478 | // TODO(darin): Delete all tasks once it is safe to do so. |
| 479 | // Until it is totally safe, just do it when running Purify or |
| 480 | // Valgrind. |
[email protected] | f4681a9 | 2010-10-27 20:03:42 | [diff] [blame] | 481 | #if defined(PURIFY) || defined(USE_HEAPCHECKER) |
[email protected] | 404ec5e | 2009-03-11 20:06:02 | [diff] [blame] | 482 | delete pending_task.task; |
[email protected] | 24db026 | 2010-08-03 03:06:21 | [diff] [blame] | 483 | #elif defined(OS_POSIX) |
| 484 | if (RUNNING_ON_VALGRIND) |
| 485 | delete pending_task.task; |
| 486 | #endif // defined(OS_POSIX) |
[email protected] | 001747c | 2008-09-10 00:37:07 | [diff] [blame] | 487 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 488 | } |
[email protected] | 001747c | 2008-09-10 00:37:07 | [diff] [blame] | 489 | did_work |= !deferred_non_nestable_work_queue_.empty(); |
| 490 | while (!deferred_non_nestable_work_queue_.empty()) { |
[email protected] | 24db026 | 2010-08-03 03:06:21 | [diff] [blame] | 491 | // TODO(darin): Delete all tasks once it is safe to do so. |
| 492 | // Until it is totaly safe, only delete them under Purify and Valgrind. |
| 493 | Task* task = NULL; |
[email protected] | f4681a9 | 2010-10-27 20:03:42 | [diff] [blame] | 494 | #if defined(PURIFY) || defined(USE_HEAPCHECKER) |
[email protected] | 24db026 | 2010-08-03 03:06:21 | [diff] [blame] | 495 | task = deferred_non_nestable_work_queue_.front().task; |
| 496 | #elif defined(OS_POSIX) |
| 497 | if (RUNNING_ON_VALGRIND) |
| 498 | task = deferred_non_nestable_work_queue_.front().task; |
| 499 | #endif |
[email protected] | 8df21d3 | 2009-03-11 19:53:50 | [diff] [blame] | 500 | deferred_non_nestable_work_queue_.pop(); |
[email protected] | 24db026 | 2010-08-03 03:06:21 | [diff] [blame] | 501 | if (task) |
| 502 | delete task; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 503 | } |
[email protected] | 001747c | 2008-09-10 00:37:07 | [diff] [blame] | 504 | did_work |= !delayed_work_queue_.empty(); |
| 505 | while (!delayed_work_queue_.empty()) { |
[email protected] | 24db026 | 2010-08-03 03:06:21 | [diff] [blame] | 506 | Task* task = delayed_work_queue_.top().task; |
[email protected] | 001747c | 2008-09-10 00:37:07 | [diff] [blame] | 507 | delayed_work_queue_.pop(); |
[email protected] | 24db026 | 2010-08-03 03:06:21 | [diff] [blame] | 508 | delete task; |
[email protected] | 001747c | 2008-09-10 00:37:07 | [diff] [blame] | 509 | } |
| 510 | return did_work; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 511 | } |
| 512 | |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 513 | bool MessageLoop::DoWork() { |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 514 | if (!nestable_tasks_allowed_) { |
| 515 | // Task can't be executed right now. |
| 516 | return false; |
| 517 | } |
| 518 | |
| 519 | for (;;) { |
| 520 | ReloadWorkQueue(); |
| 521 | if (work_queue_.empty()) |
| 522 | break; |
| 523 | |
| 524 | // Execute oldest task. |
| 525 | do { |
| 526 | PendingTask pending_task = work_queue_.front(); |
| 527 | work_queue_.pop(); |
| 528 | if (!pending_task.delayed_run_time.is_null()) { |
[email protected] | 001747c | 2008-09-10 00:37:07 | [diff] [blame] | 529 | AddToDelayedWorkQueue(pending_task); |
[email protected] | 72deacd | 2008-09-23 19:19:20 | [diff] [blame] | 530 | // If we changed the topmost task, then it is time to re-schedule. |
[email protected] | 11f76c7 | 2010-10-21 06:32:33 | [diff] [blame] | 531 | if (delayed_work_queue_.top().task == pending_task.task) |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 532 | pump_->ScheduleDelayedWork(pending_task.delayed_run_time); |
| 533 | } else { |
| 534 | if (DeferOrRunPendingTask(pending_task)) |
| 535 | return true; |
| 536 | } |
| 537 | } while (!work_queue_.empty()); |
| 538 | } |
| 539 | |
| 540 | // Nothing happened. |
| 541 | return false; |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 542 | } |
| 543 | |
[email protected] | 7e7fab4 | 2010-11-06 22:23:29 | [diff] [blame] | 544 | bool MessageLoop::DoDelayedWork(base::TimeTicks* next_delayed_work_time) { |
[email protected] | 11f76c7 | 2010-10-21 06:32:33 | [diff] [blame] | 545 | if (!nestable_tasks_allowed_ || delayed_work_queue_.empty()) { |
[email protected] | 7e7fab4 | 2010-11-06 22:23:29 | [diff] [blame] | 546 | recent_time_ = *next_delayed_work_time = TimeTicks(); |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 547 | return false; |
| 548 | } |
[email protected] | 1d2eb13 | 2008-12-08 17:36:06 | [diff] [blame] | 549 | |
[email protected] | 7e7fab4 | 2010-11-06 22:23:29 | [diff] [blame] | 550 | // When we "fall behind," there will be a lot of tasks in the delayed work |
[email protected] | a8f7d3d | 2010-11-04 23:23:42 | [diff] [blame] | 551 | // queue that are ready to run. To increase efficiency when we fall behind, |
| 552 | // we will only call Time::Now() intermittently, and then process all tasks |
| 553 | // that are ready to run before calling it again. As a result, the more we |
| 554 | // fall behind (and have a lot of ready-to-run delayed tasks), the more |
| 555 | // efficient we'll be at handling the tasks. |
[email protected] | 7e7fab4 | 2010-11-06 22:23:29 | [diff] [blame] | 556 | |
| 557 | TimeTicks next_run_time = delayed_work_queue_.top().delayed_run_time; |
[email protected] | a8f7d3d | 2010-11-04 23:23:42 | [diff] [blame] | 558 | if (next_run_time > recent_time_) { |
[email protected] | 7e7fab4 | 2010-11-06 22:23:29 | [diff] [blame] | 559 | recent_time_ = TimeTicks::Now(); // Get a better view of Now(); |
[email protected] | a8f7d3d | 2010-11-04 23:23:42 | [diff] [blame] | 560 | if (next_run_time > recent_time_) { |
| 561 | *next_delayed_work_time = next_run_time; |
| 562 | return false; |
| 563 | } |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 564 | } |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 565 | |
[email protected] | 11f76c7 | 2010-10-21 06:32:33 | [diff] [blame] | 566 | PendingTask pending_task = delayed_work_queue_.top(); |
| 567 | delayed_work_queue_.pop(); |
[email protected] | 1d2eb13 | 2008-12-08 17:36:06 | [diff] [blame] | 568 | |
[email protected] | 11f76c7 | 2010-10-21 06:32:33 | [diff] [blame] | 569 | if (!delayed_work_queue_.empty()) |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 570 | *next_delayed_work_time = delayed_work_queue_.top().delayed_run_time; |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 571 | |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 572 | return DeferOrRunPendingTask(pending_task); |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | bool MessageLoop::DoIdleWork() { |
| 576 | if (ProcessNextDelayedNonNestableTask()) |
| 577 | return true; |
| 578 | |
| 579 | if (state_->quit_received) |
| 580 | pump_->Quit(); |
| 581 | |
| 582 | return false; |
| 583 | } |
| 584 | |
| 585 | //------------------------------------------------------------------------------ |
| 586 | // MessageLoop::AutoRunState |
| 587 | |
| 588 | MessageLoop::AutoRunState::AutoRunState(MessageLoop* loop) : loop_(loop) { |
| 589 | // Make the loop reference us. |
| 590 | previous_state_ = loop_->state_; |
| 591 | if (previous_state_) { |
| 592 | run_depth = previous_state_->run_depth + 1; |
[email protected] | ea15e98 | 2008-08-15 07:31:20 | [diff] [blame] | 593 | } else { |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 594 | run_depth = 1; |
[email protected] | ea15e98 | 2008-08-15 07:31:20 | [diff] [blame] | 595 | } |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 596 | loop_->state_ = this; |
| 597 | |
| 598 | // Initialize the other fields: |
| 599 | quit_received = false; |
[email protected] | e43eddf1 | 2009-12-29 00:32:52 | [diff] [blame] | 600 | #if !defined(OS_MACOSX) |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 601 | dispatcher = NULL; |
| 602 | #endif |
| 603 | } |
| 604 | |
| 605 | MessageLoop::AutoRunState::~AutoRunState() { |
| 606 | loop_->state_ = previous_state_; |
[email protected] | a5b94a9 | 2008-08-12 23:25:43 | [diff] [blame] | 607 | } |
| 608 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 609 | //------------------------------------------------------------------------------ |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 610 | // MessageLoop::PendingTask |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 611 | |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 612 | bool MessageLoop::PendingTask::operator<(const PendingTask& other) const { |
| 613 | // Since the top of a priority queue is defined as the "greatest" element, we |
| 614 | // need to invert the comparison here. We want the smaller time to be at the |
| 615 | // top of the heap. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 616 | |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 617 | if (delayed_run_time < other.delayed_run_time) |
| 618 | return false; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 619 | |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 620 | if (delayed_run_time > other.delayed_run_time) |
| 621 | return true; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 622 | |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 623 | // If the times happen to match, then we use the sequence number to decide. |
| 624 | // Compare the difference to support integer roll-over. |
| 625 | return (sequence_num - other.sequence_num) > 0; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | //------------------------------------------------------------------------------ |
| 629 | // Method and data for histogramming events and actions taken by each instance |
| 630 | // on each thread. |
| 631 | |
| 632 | // static |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 633 | void MessageLoop::EnableHistogrammer(bool enable) { |
| 634 | enable_histogrammer_ = enable; |
| 635 | } |
| 636 | |
| 637 | void MessageLoop::StartHistogrammer() { |
| 638 | if (enable_histogrammer_ && !message_histogram_.get() |
[email protected] | 835d7c8 | 2010-10-14 04:38:38 | [diff] [blame] | 639 | && base::StatisticsRecorder::WasStarted()) { |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 640 | DCHECK(!thread_name_.empty()); |
[email protected] | 835d7c8 | 2010-10-14 04:38:38 | [diff] [blame] | 641 | message_histogram_ = base::LinearHistogram::FactoryGet( |
| 642 | "MsgLoop:" + thread_name_, |
[email protected] | 2753b39 | 2009-12-28 06:59:52 | [diff] [blame] | 643 | kLeastNonZeroMessageId, kMaxMessageId, |
| 644 | kNumberOfDistinctMessagesDisplayed, |
| 645 | message_histogram_->kHexRangePrintingFlag); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 646 | message_histogram_->SetRangeDescriptions(event_descriptions_); |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | void MessageLoop::HistogramEvent(int event) { |
| 651 | if (message_histogram_.get()) |
| 652 | message_histogram_->Add(event); |
| 653 | } |
| 654 | |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 655 | //------------------------------------------------------------------------------ |
| 656 | // MessageLoopForUI |
| 657 | |
| 658 | #if defined(OS_WIN) |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 659 | void MessageLoopForUI::DidProcessMessage(const MSG& message) { |
| 660 | pump_win()->DidProcessMessage(message); |
| 661 | } |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 662 | #endif // defined(OS_WIN) |
| 663 | |
[email protected] | e43eddf1 | 2009-12-29 00:32:52 | [diff] [blame] | 664 | #if !defined(OS_MACOSX) |
[email protected] | 148d105 | 2009-07-31 22:53:37 | [diff] [blame] | 665 | void MessageLoopForUI::AddObserver(Observer* observer) { |
| 666 | pump_ui()->AddObserver(observer); |
| 667 | } |
| 668 | |
| 669 | void MessageLoopForUI::RemoveObserver(Observer* observer) { |
| 670 | pump_ui()->RemoveObserver(observer); |
| 671 | } |
| 672 | |
| 673 | void MessageLoopForUI::Run(Dispatcher* dispatcher) { |
| 674 | AutoRunState save_state(this); |
| 675 | state_->dispatcher = dispatcher; |
| 676 | RunHandler(); |
| 677 | } |
[email protected] | e43eddf1 | 2009-12-29 00:32:52 | [diff] [blame] | 678 | #endif // !defined(OS_MACOSX) |
[email protected] | 148d105 | 2009-07-31 22:53:37 | [diff] [blame] | 679 | |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 680 | //------------------------------------------------------------------------------ |
| 681 | // MessageLoopForIO |
| 682 | |
| 683 | #if defined(OS_WIN) |
| 684 | |
[email protected] | 32cda29d | 2008-10-09 23:58:43 | [diff] [blame] | 685 | void MessageLoopForIO::RegisterIOHandler(HANDLE file, IOHandler* handler) { |
| 686 | pump_io()->RegisterIOHandler(file, handler); |
| 687 | } |
| 688 | |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 689 | bool MessageLoopForIO::WaitForIOCompletion(DWORD timeout, IOHandler* filter) { |
| 690 | return pump_io()->WaitForIOCompletion(timeout, filter); |
[email protected] | 32cda29d | 2008-10-09 23:58:43 | [diff] [blame] | 691 | } |
| 692 | |
[email protected] | 36987e9 | 2008-09-18 18:46:26 | [diff] [blame] | 693 | #elif defined(OS_POSIX) |
| 694 | |
[email protected] | e45e6c0 | 2008-12-15 22:02:17 | [diff] [blame] | 695 | bool MessageLoopForIO::WatchFileDescriptor(int fd, |
| 696 | bool persistent, |
| 697 | Mode mode, |
| 698 | FileDescriptorWatcher *controller, |
| 699 | Watcher *delegate) { |
| 700 | return pump_libevent()->WatchFileDescriptor( |
| 701 | fd, |
| 702 | persistent, |
| 703 | static_cast<base::MessagePumpLibevent::Mode>(mode), |
| 704 | controller, |
| 705 | delegate); |
[email protected] | 36987e9 | 2008-09-18 18:46:26 | [diff] [blame] | 706 | } |
| 707 | |
[email protected] | 36987e9 | 2008-09-18 18:46:26 | [diff] [blame] | 708 | #endif |