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