thestig@chromium.org | 7016bac | 2010-04-15 10:04:29 +0900 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
license.bot | f003cfe | 2008-08-24 09:55:55 +0900 | [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 | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 4 | |
darin@google.com | 6ddeb84 | 2008-08-15 16:31:20 +0900 | [diff] [blame] | 5 | #include "base/message_loop.h" |
| 6 | |
darin@google.com | 981f355 | 2008-08-16 12:09:05 +0900 | [diff] [blame] | 7 | #include <algorithm> |
| 8 | |
mmentovai@google.com | fa5f993 | 2008-08-22 07:26:06 +0900 | [diff] [blame] | 9 | #include "base/compiler_specific.h" |
deanm@chromium.org | cd1ce30 | 2008-09-10 19:54:06 +0900 | [diff] [blame] | 10 | #include "base/lazy_instance.h" |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 11 | #include "base/logging.h" |
darin@google.com | 12d40bb | 2008-08-20 03:36:23 +0900 | [diff] [blame] | 12 | #include "base/message_pump_default.h" |
brettw@chromium.org | 275c2ec | 2010-10-14 13:38:38 +0900 | [diff] [blame] | 13 | #include "base/metrics/histogram.h" |
deanm@chromium.org | cd1ce30 | 2008-09-10 19:54:06 +0900 | [diff] [blame] | 14 | #include "base/thread_local.h" |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 15 | |
mark@chromium.org | 059d049 | 2008-09-24 06:08:28 +0900 | [diff] [blame] | 16 | #if defined(OS_MACOSX) |
| 17 | #include "base/message_pump_mac.h" |
| 18 | #endif |
dkegel@google.com | 9e044ae | 2008-09-19 03:46:26 +0900 | [diff] [blame] | 19 | #if defined(OS_POSIX) |
| 20 | #include "base/message_pump_libevent.h" |
kuchhal@chromium.org | e3bd2eb | 2009-05-15 01:44:26 +0900 | [diff] [blame] | 21 | #include "base/third_party/valgrind/valgrind.h" |
dkegel@google.com | 9e044ae | 2008-09-19 03:46:26 +0900 | [diff] [blame] | 22 | #endif |
evan@chromium.org | 875bb6e | 2009-12-29 09:32:52 +0900 | [diff] [blame] | 23 | #if defined(OS_POSIX) && !defined(OS_MACOSX) |
dsh@google.com | 119a252 | 2008-10-04 01:52:59 +0900 | [diff] [blame] | 24 | #include "base/message_pump_glib.h" |
| 25 | #endif |
rjkroege@google.com | 3080f44 | 2010-10-23 01:17:47 +0900 | [diff] [blame] | 26 | #if defined(TOUCH_UI) |
| 27 | #include "base/message_pump_glib_x.h" |
| 28 | #endif |
dkegel@google.com | 9e044ae | 2008-09-19 03:46:26 +0900 | [diff] [blame] | 29 | |
dsh@google.com | 0f8dd26 | 2008-10-28 05:43:33 +0900 | [diff] [blame] | 30 | using base::Time; |
| 31 | using base::TimeDelta; |
| 32 | |
erg@chromium.org | a752852 | 2010-07-16 02:23:23 +0900 | [diff] [blame] | 33 | namespace { |
| 34 | |
deanm@chromium.org | cd1ce30 | 2008-09-10 19:54:06 +0900 | [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. |
erg@chromium.org | a752852 | 2010-07-16 02:23:23 +0900 | [diff] [blame] | 37 | base::LazyInstance<base::ThreadLocalPointer<MessageLoop> > lazy_tls_ptr( |
deanm@chromium.org | cd1ce30 | 2008-09-10 19:54:06 +0900 | [diff] [blame] | 38 | base::LINKER_INITIALIZED); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 39 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [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. |
erg@chromium.org | a752852 | 2010-07-16 02:23:23 +0900 | [diff] [blame] | 42 | const int kTaskRunEvent = 0x1; |
| 43 | const int kTimerEvent = 0x2; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 44 | |
| 45 | // Provide range of message IDs for use in histogramming and debug display. |
erg@chromium.org | a752852 | 2010-07-16 02:23:23 +0900 | [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 | |
brettw@chromium.org | 275c2ec | 2010-10-14 13:38:38 +0900 | [diff] [blame] | 66 | const base::LinearHistogram::DescriptionPair event_descriptions_[] = { |
erg@chromium.org | a752852 | 2010-07-16 02:23:23 +0900 | [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 | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 80 | |
| 81 | //------------------------------------------------------------------------------ |
| 82 | |
darin@google.com | 981f355 | 2008-08-16 12:09:05 +0900 | [diff] [blame] | 83 | #if defined(OS_WIN) |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 84 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [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 | |
darin@google.com | 981f355 | 2008-08-16 12:09:05 +0900 | [diff] [blame] | 101 | #endif // defined(OS_WIN) |
| 102 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 103 | //------------------------------------------------------------------------------ |
| 104 | |
erg@chromium.org | 493f5f6 | 2010-07-16 06:03:54 +0900 | [diff] [blame] | 105 | MessageLoop::TaskObserver::TaskObserver() { |
| 106 | } |
| 107 | |
| 108 | MessageLoop::TaskObserver::~TaskObserver() { |
| 109 | } |
| 110 | |
| 111 | MessageLoop::DestructionObserver::~DestructionObserver() { |
| 112 | } |
| 113 | |
| 114 | //------------------------------------------------------------------------------ |
| 115 | |
deanm@chromium.org | cd1ce30 | 2008-09-10 19:54:06 +0900 | [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. |
erg@google.com | bf6ce9f | 2010-01-27 08:08:02 +0900 | [diff] [blame] | 120 | // DCHECK(loop) << "Ouch, did you forget to initialize me?"; |
deanm@chromium.org | cd1ce30 | 2008-09-10 19:54:06 +0900 | [diff] [blame] | 121 | return lazy_tls_ptr.Pointer()->Get(); |
| 122 | } |
| 123 | |
darin@google.com | d936b5b | 2008-08-26 14:53:57 +0900 | [diff] [blame] | 124 | MessageLoop::MessageLoop(Type type) |
| 125 | : type_(type), |
darin@google.com | ee6fa72 | 2008-08-13 08:25:43 +0900 | [diff] [blame] | 126 | nestable_tasks_allowed_(true), |
darin@google.com | 12d40bb | 2008-08-20 03:36:23 +0900 | [diff] [blame] | 127 | exception_restoration_(false), |
darin@google.com | be165ae | 2008-09-07 17:08:29 +0900 | [diff] [blame] | 128 | state_(NULL), |
| 129 | next_sequence_num_(0) { |
deanm@chromium.org | cd1ce30 | 2008-09-10 19:54:06 +0900 | [diff] [blame] | 130 | DCHECK(!current()) << "should only have one message loop per thread"; |
| 131 | lazy_tls_ptr.Pointer()->Set(this); |
darin@google.com | d936b5b | 2008-08-26 14:53:57 +0900 | [diff] [blame] | 132 | |
thestig@chromium.org | 7016bac | 2010-04-15 10:04:29 +0900 | [diff] [blame] | 133 | // TODO(rvargas): Get rid of the OS guards. |
darin@google.com | 981f355 | 2008-08-16 12:09:05 +0900 | [diff] [blame] | 134 | #if defined(OS_WIN) |
thestig@chromium.org | 7016bac | 2010-04-15 10:04:29 +0900 | [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() |
rjkroege@google.com | 3080f44 | 2010-10-23 01:17:47 +0900 | [diff] [blame] | 140 | #elif defined(TOUCH_UI) |
| 141 | #define MESSAGE_PUMP_UI new base::MessagePumpGlibX() |
| 142 | #define MESSAGE_PUMP_IO new base::MessagePumpLibevent() |
thestig@chromium.org | 7016bac | 2010-04-15 10:04:29 +0900 | [diff] [blame] | 143 | #elif defined(OS_POSIX) // POSIX but not MACOSX. |
| 144 | #define MESSAGE_PUMP_UI new base::MessagePumpForUI() |
| 145 | #define MESSAGE_PUMP_IO new base::MessagePumpLibevent() |
evan@chromium.org | 875bb6e | 2009-12-29 09:32:52 +0900 | [diff] [blame] | 146 | #else |
thestig@chromium.org | 7016bac | 2010-04-15 10:04:29 +0900 | [diff] [blame] | 147 | #error Not implemented |
evan@chromium.org | 875bb6e | 2009-12-29 09:32:52 +0900 | [diff] [blame] | 148 | #endif |
thestig@chromium.org | 7016bac | 2010-04-15 10:04:29 +0900 | [diff] [blame] | 149 | |
| 150 | if (type_ == TYPE_UI) { |
| 151 | pump_ = MESSAGE_PUMP_UI; |
dsh@google.com | 119a252 | 2008-10-04 01:52:59 +0900 | [diff] [blame] | 152 | } else if (type_ == TYPE_IO) { |
thestig@chromium.org | 7016bac | 2010-04-15 10:04:29 +0900 | [diff] [blame] | 153 | pump_ = MESSAGE_PUMP_IO; |
dkegel@google.com | 9e044ae | 2008-09-19 03:46:26 +0900 | [diff] [blame] | 154 | } else { |
thestig@chromium.org | 7016bac | 2010-04-15 10:04:29 +0900 | [diff] [blame] | 155 | DCHECK_EQ(TYPE_DEFAULT, type_); |
dkegel@google.com | 9e044ae | 2008-09-19 03:46:26 +0900 | [diff] [blame] | 156 | pump_ = new base::MessagePumpDefault(); |
| 157 | } |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | MessageLoop::~MessageLoop() { |
| 161 | DCHECK(this == current()); |
darin@google.com | 965e534 | 2008-08-06 08:16:41 +0900 | [diff] [blame] | 162 | |
| 163 | // Let interested parties have one last shot at accessing this. |
| 164 | FOR_EACH_OBSERVER(DestructionObserver, destruction_observers_, |
| 165 | WillDestroyCurrentMessageLoop()); |
| 166 | |
darin@google.com | 0e50050 | 2008-09-09 14:55:35 +0900 | [diff] [blame] | 167 | DCHECK(!state_); |
| 168 | |
darin@chromium.org | e3af17f | 2008-09-10 09:37:07 +0900 | [diff] [blame] | 169 | // Clean up any unprocessed tasks, but take care: deleting a task could |
| 170 | // result in the addition of more tasks (e.g., via DeleteSoon). We set a |
| 171 | // limit on the number of times we will allow a deleted task to generate more |
| 172 | // tasks. Normally, we should only pass through this loop once or twice. If |
| 173 | // we end up hitting the loop limit, then it is probably due to one task that |
| 174 | // is being stubborn. Inspect the queues to see who is left. |
| 175 | bool did_work; |
| 176 | for (int i = 0; i < 100; ++i) { |
| 177 | DeletePendingTasks(); |
| 178 | ReloadWorkQueue(); |
| 179 | // If we end up with empty queues, then break out of the loop. |
| 180 | did_work = DeletePendingTasks(); |
| 181 | if (!did_work) |
| 182 | break; |
darin@google.com | 0e50050 | 2008-09-09 14:55:35 +0900 | [diff] [blame] | 183 | } |
darin@chromium.org | e3af17f | 2008-09-10 09:37:07 +0900 | [diff] [blame] | 184 | DCHECK(!did_work); |
| 185 | |
| 186 | // OK, now make it so that no one can find us. |
deanm@chromium.org | e4cc592 | 2008-09-10 20:14:56 +0900 | [diff] [blame] | 187 | lazy_tls_ptr.Pointer()->Set(NULL); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 188 | } |
| 189 | |
sky@chromium.org | 18c66dc | 2010-09-16 07:14:36 +0900 | [diff] [blame] | 190 | void MessageLoop::AddDestructionObserver( |
| 191 | DestructionObserver* destruction_observer) { |
darin@google.com | 965e534 | 2008-08-06 08:16:41 +0900 | [diff] [blame] | 192 | DCHECK(this == current()); |
sky@chromium.org | 18c66dc | 2010-09-16 07:14:36 +0900 | [diff] [blame] | 193 | destruction_observers_.AddObserver(destruction_observer); |
darin@google.com | 965e534 | 2008-08-06 08:16:41 +0900 | [diff] [blame] | 194 | } |
| 195 | |
sky@chromium.org | 18c66dc | 2010-09-16 07:14:36 +0900 | [diff] [blame] | 196 | void MessageLoop::RemoveDestructionObserver( |
| 197 | DestructionObserver* destruction_observer) { |
darin@google.com | 965e534 | 2008-08-06 08:16:41 +0900 | [diff] [blame] | 198 | DCHECK(this == current()); |
sky@chromium.org | 18c66dc | 2010-09-16 07:14:36 +0900 | [diff] [blame] | 199 | destruction_observers_.RemoveObserver(destruction_observer); |
darin@google.com | 965e534 | 2008-08-06 08:16:41 +0900 | [diff] [blame] | 200 | } |
| 201 | |
sky@chromium.org | 18c66dc | 2010-09-16 07:14:36 +0900 | [diff] [blame] | 202 | void MessageLoop::AddTaskObserver(TaskObserver* task_observer) { |
willchan@chromium.org | a904763 | 2010-06-10 06:20:41 +0900 | [diff] [blame] | 203 | DCHECK_EQ(this, current()); |
sky@chromium.org | 18c66dc | 2010-09-16 07:14:36 +0900 | [diff] [blame] | 204 | task_observers_.AddObserver(task_observer); |
willchan@chromium.org | a904763 | 2010-06-10 06:20:41 +0900 | [diff] [blame] | 205 | } |
| 206 | |
sky@chromium.org | 18c66dc | 2010-09-16 07:14:36 +0900 | [diff] [blame] | 207 | void MessageLoop::RemoveTaskObserver(TaskObserver* task_observer) { |
willchan@chromium.org | a904763 | 2010-06-10 06:20:41 +0900 | [diff] [blame] | 208 | DCHECK_EQ(this, current()); |
sky@chromium.org | 18c66dc | 2010-09-16 07:14:36 +0900 | [diff] [blame] | 209 | task_observers_.RemoveObserver(task_observer); |
willchan@chromium.org | a904763 | 2010-06-10 06:20:41 +0900 | [diff] [blame] | 210 | } |
| 211 | |
darin@google.com | 6ddeb84 | 2008-08-15 16:31:20 +0900 | [diff] [blame] | 212 | void MessageLoop::Run() { |
darin@google.com | 981f355 | 2008-08-16 12:09:05 +0900 | [diff] [blame] | 213 | AutoRunState save_state(this); |
| 214 | RunHandler(); |
darin@google.com | 6ddeb84 | 2008-08-15 16:31:20 +0900 | [diff] [blame] | 215 | } |
| 216 | |
jar@google.com | 9239e02 | 2008-07-31 22:10:20 +0900 | [diff] [blame] | 217 | void MessageLoop::RunAllPending() { |
darin@google.com | 981f355 | 2008-08-16 12:09:05 +0900 | [diff] [blame] | 218 | AutoRunState save_state(this); |
| 219 | state_->quit_received = true; // Means run until we would otherwise block. |
| 220 | RunHandler(); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | // Runs the loop in two different SEH modes: |
| 224 | // enable_SEH_restoration_ = false : any unhandled exception goes to the last |
| 225 | // one that calls SetUnhandledExceptionFilter(). |
| 226 | // enable_SEH_restoration_ = true : any unhandled exception goes to the filter |
| 227 | // that was existed before the loop was run. |
darin@google.com | 981f355 | 2008-08-16 12:09:05 +0900 | [diff] [blame] | 228 | void MessageLoop::RunHandler() { |
| 229 | #if defined(OS_WIN) |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 230 | if (exception_restoration_) { |
stoyan@google.com | 283facb | 2009-10-27 03:15:59 +0900 | [diff] [blame] | 231 | RunInternalInSEHFrame(); |
darin@google.com | 981f355 | 2008-08-16 12:09:05 +0900 | [diff] [blame] | 232 | return; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 233 | } |
darin@google.com | 981f355 | 2008-08-16 12:09:05 +0900 | [diff] [blame] | 234 | #endif |
| 235 | |
| 236 | RunInternal(); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 237 | } |
stoyan@google.com | 283facb | 2009-10-27 03:15:59 +0900 | [diff] [blame] | 238 | //------------------------------------------------------------------------------ |
| 239 | #if defined(OS_WIN) |
| 240 | __declspec(noinline) void MessageLoop::RunInternalInSEHFrame() { |
| 241 | LPTOP_LEVEL_EXCEPTION_FILTER current_filter = GetTopSEHFilter(); |
| 242 | __try { |
| 243 | RunInternal(); |
| 244 | } __except(SEHFilter(current_filter)) { |
| 245 | } |
| 246 | return; |
| 247 | } |
| 248 | #endif |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 249 | //------------------------------------------------------------------------------ |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 250 | |
darin@google.com | 981f355 | 2008-08-16 12:09:05 +0900 | [diff] [blame] | 251 | void MessageLoop::RunInternal() { |
| 252 | DCHECK(this == current()); |
| 253 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 254 | StartHistogrammer(); |
| 255 | |
evan@chromium.org | 875bb6e | 2009-12-29 09:32:52 +0900 | [diff] [blame] | 256 | #if !defined(OS_MACOSX) |
jcampan@chromium.org | 0542358 | 2009-08-01 07:53:37 +0900 | [diff] [blame] | 257 | if (state_->dispatcher && type() == TYPE_UI) { |
| 258 | static_cast<base::MessagePumpForUI*>(pump_.get())-> |
| 259 | RunWithDispatcher(this, state_->dispatcher); |
darin@google.com | 981f355 | 2008-08-16 12:09:05 +0900 | [diff] [blame] | 260 | return; |
jar@google.com | 9239e02 | 2008-07-31 22:10:20 +0900 | [diff] [blame] | 261 | } |
darin@google.com | 981f355 | 2008-08-16 12:09:05 +0900 | [diff] [blame] | 262 | #endif |
jeremy@chromium.org | 2ed223c | 2008-12-09 02:36:06 +0900 | [diff] [blame] | 263 | |
darin@google.com | 981f355 | 2008-08-16 12:09:05 +0900 | [diff] [blame] | 264 | pump_->Run(this); |
jar@google.com | fbaaf69 | 2008-07-30 16:50:53 +0900 | [diff] [blame] | 265 | } |
jar@google.com | 7ff36e6 | 2008-07-30 15:58:56 +0900 | [diff] [blame] | 266 | |
jar@google.com | b4d1bff | 2008-07-31 04:03:59 +0900 | [diff] [blame] | 267 | //------------------------------------------------------------------------------ |
| 268 | // Wrapper functions for use in above message loop framework. |
| 269 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 270 | bool MessageLoop::ProcessNextDelayedNonNestableTask() { |
darin@google.com | 981f355 | 2008-08-16 12:09:05 +0900 | [diff] [blame] | 271 | if (state_->run_depth != 1) |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 272 | return false; |
| 273 | |
darin@google.com | be165ae | 2008-09-07 17:08:29 +0900 | [diff] [blame] | 274 | if (deferred_non_nestable_work_queue_.empty()) |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 275 | return false; |
jeremy@chromium.org | 2ed223c | 2008-12-09 02:36:06 +0900 | [diff] [blame] | 276 | |
darin@google.com | be165ae | 2008-09-07 17:08:29 +0900 | [diff] [blame] | 277 | Task* task = deferred_non_nestable_work_queue_.front().task; |
| 278 | deferred_non_nestable_work_queue_.pop(); |
jeremy@chromium.org | 2ed223c | 2008-12-09 02:36:06 +0900 | [diff] [blame] | 279 | |
darin@google.com | be165ae | 2008-09-07 17:08:29 +0900 | [diff] [blame] | 280 | RunTask(task); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 281 | return true; |
| 282 | } |
| 283 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 284 | //------------------------------------------------------------------------------ |
| 285 | |
| 286 | void MessageLoop::Quit() { |
darin@google.com | 981f355 | 2008-08-16 12:09:05 +0900 | [diff] [blame] | 287 | DCHECK(current() == this); |
| 288 | if (state_) { |
| 289 | state_->quit_received = true; |
| 290 | } else { |
| 291 | NOTREACHED() << "Must be inside Run to call Quit"; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 292 | } |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 293 | } |
| 294 | |
darin@chromium.org | d70a12c | 2010-02-23 16:12:22 +0900 | [diff] [blame] | 295 | void MessageLoop::QuitNow() { |
| 296 | DCHECK(current() == this); |
| 297 | if (state_) { |
| 298 | pump_->Quit(); |
| 299 | } else { |
| 300 | NOTREACHED() << "Must be inside Run to call Quit"; |
| 301 | } |
| 302 | } |
| 303 | |
darin@google.com | be165ae | 2008-09-07 17:08:29 +0900 | [diff] [blame] | 304 | void MessageLoop::PostTask( |
| 305 | const tracked_objects::Location& from_here, Task* task) { |
| 306 | PostTask_Helper(from_here, task, 0, true); |
| 307 | } |
| 308 | |
| 309 | void MessageLoop::PostDelayedTask( |
phajdan.jr@chromium.org | c3c9225 | 2009-06-18 02:23:51 +0900 | [diff] [blame] | 310 | const tracked_objects::Location& from_here, Task* task, int64 delay_ms) { |
darin@google.com | be165ae | 2008-09-07 17:08:29 +0900 | [diff] [blame] | 311 | PostTask_Helper(from_here, task, delay_ms, true); |
| 312 | } |
| 313 | |
| 314 | void MessageLoop::PostNonNestableTask( |
| 315 | const tracked_objects::Location& from_here, Task* task) { |
| 316 | PostTask_Helper(from_here, task, 0, false); |
| 317 | } |
| 318 | |
| 319 | void MessageLoop::PostNonNestableDelayedTask( |
phajdan.jr@chromium.org | c3c9225 | 2009-06-18 02:23:51 +0900 | [diff] [blame] | 320 | const tracked_objects::Location& from_here, Task* task, int64 delay_ms) { |
darin@google.com | be165ae | 2008-09-07 17:08:29 +0900 | [diff] [blame] | 321 | PostTask_Helper(from_here, task, delay_ms, false); |
| 322 | } |
| 323 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 324 | // Possibly called on a background thread! |
darin@google.com | be165ae | 2008-09-07 17:08:29 +0900 | [diff] [blame] | 325 | void MessageLoop::PostTask_Helper( |
phajdan.jr@chromium.org | c3c9225 | 2009-06-18 02:23:51 +0900 | [diff] [blame] | 326 | const tracked_objects::Location& from_here, Task* task, int64 delay_ms, |
darin@google.com | be165ae | 2008-09-07 17:08:29 +0900 | [diff] [blame] | 327 | bool nestable) { |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 328 | task->SetBirthPlace(from_here); |
darin@google.com | 0795f57 | 2008-08-30 09:22:48 +0900 | [diff] [blame] | 329 | |
darin@google.com | be165ae | 2008-09-07 17:08:29 +0900 | [diff] [blame] | 330 | PendingTask pending_task(task, nestable); |
darin@google.com | 0795f57 | 2008-08-30 09:22:48 +0900 | [diff] [blame] | 331 | |
| 332 | if (delay_ms > 0) { |
darin@google.com | be165ae | 2008-09-07 17:08:29 +0900 | [diff] [blame] | 333 | pending_task.delayed_run_time = |
darin@google.com | 0795f57 | 2008-08-30 09:22:48 +0900 | [diff] [blame] | 334 | Time::Now() + TimeDelta::FromMilliseconds(delay_ms); |
mbelshe@chromium.org | de50b7d | 2010-06-29 13:58:15 +0900 | [diff] [blame] | 335 | |
| 336 | #if defined(OS_WIN) |
| 337 | if (high_resolution_timer_expiration_.is_null()) { |
| 338 | // Windows timers are granular to 15.6ms. If we only set high-res |
| 339 | // timers for those under 15.6ms, then a 18ms timer ticks at ~32ms, |
| 340 | // which as a percentage is pretty inaccurate. So enable high |
| 341 | // res timers for any timer which is within 2x of the granularity. |
| 342 | // This is a tradeoff between accuracy and power management. |
| 343 | bool needs_high_res_timers = |
| 344 | delay_ms < (2 * Time::kMinLowResolutionThresholdMs); |
| 345 | if (needs_high_res_timers) { |
phajdan.jr@chromium.org | 3337f3d | 2010-10-20 19:50:38 +0900 | [diff] [blame] | 346 | Time::ActivateHighResolutionTimer(true); |
| 347 | high_resolution_timer_expiration_ = base::TimeTicks::Now() + |
| 348 | TimeDelta::FromMilliseconds(kHighResolutionTimerModeLeaseTimeMs); |
mbelshe@chromium.org | de50b7d | 2010-06-29 13:58:15 +0900 | [diff] [blame] | 349 | } |
| 350 | } |
| 351 | #endif |
darin@google.com | 0795f57 | 2008-08-30 09:22:48 +0900 | [diff] [blame] | 352 | } else { |
jar@chromium.org | ed5238a | 2009-12-28 15:59:52 +0900 | [diff] [blame] | 353 | DCHECK_EQ(delay_ms, 0) << "delay should not be negative"; |
darin@google.com | 0795f57 | 2008-08-30 09:22:48 +0900 | [diff] [blame] | 354 | } |
| 355 | |
mbelshe@chromium.org | de50b7d | 2010-06-29 13:58:15 +0900 | [diff] [blame] | 356 | #if defined(OS_WIN) |
| 357 | if (!high_resolution_timer_expiration_.is_null()) { |
| 358 | if (base::TimeTicks::Now() > high_resolution_timer_expiration_) { |
| 359 | Time::ActivateHighResolutionTimer(false); |
| 360 | high_resolution_timer_expiration_ = base::TimeTicks(); |
| 361 | } |
| 362 | } |
| 363 | #endif |
| 364 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 365 | // Warning: Don't try to short-circuit, and handle this thread's tasks more |
| 366 | // directly, as it could starve handling of foreign threads. Put every task |
| 367 | // into this queue. |
| 368 | |
darin@google.com | 981f355 | 2008-08-16 12:09:05 +0900 | [diff] [blame] | 369 | scoped_refptr<base::MessagePump> pump; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 370 | { |
darin@google.com | 981f355 | 2008-08-16 12:09:05 +0900 | [diff] [blame] | 371 | AutoLock locked(incoming_queue_lock_); |
| 372 | |
darin@google.com | be165ae | 2008-09-07 17:08:29 +0900 | [diff] [blame] | 373 | bool was_empty = incoming_queue_.empty(); |
| 374 | incoming_queue_.push(pending_task); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 375 | if (!was_empty) |
| 376 | return; // Someone else should have started the sub-pump. |
| 377 | |
darin@google.com | 981f355 | 2008-08-16 12:09:05 +0900 | [diff] [blame] | 378 | pump = pump_; |
darin@google.com | 6ddeb84 | 2008-08-15 16:31:20 +0900 | [diff] [blame] | 379 | } |
darin@google.com | 981f355 | 2008-08-16 12:09:05 +0900 | [diff] [blame] | 380 | // Since the incoming_queue_ may contain a task that destroys this message |
| 381 | // loop, we cannot exit incoming_queue_lock_ until we are done with |this|. |
| 382 | // We use a stack-based reference to the message pump so that we can call |
| 383 | // ScheduleWork outside of incoming_queue_lock_. |
darin@google.com | 6ddeb84 | 2008-08-15 16:31:20 +0900 | [diff] [blame] | 384 | |
darin@google.com | 981f355 | 2008-08-16 12:09:05 +0900 | [diff] [blame] | 385 | pump->ScheduleWork(); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | void MessageLoop::SetNestableTasksAllowed(bool allowed) { |
mpcomplete@google.com | 989d5f8 | 2008-08-09 09:14:09 +0900 | [diff] [blame] | 389 | if (nestable_tasks_allowed_ != allowed) { |
| 390 | nestable_tasks_allowed_ = allowed; |
| 391 | if (!nestable_tasks_allowed_) |
| 392 | return; |
| 393 | // Start the native pump if we are not already pumping. |
darin@google.com | 981f355 | 2008-08-16 12:09:05 +0900 | [diff] [blame] | 394 | pump_->ScheduleWork(); |
mpcomplete@google.com | 989d5f8 | 2008-08-09 09:14:09 +0900 | [diff] [blame] | 395 | } |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | bool MessageLoop::NestableTasksAllowed() const { |
| 399 | return nestable_tasks_allowed_; |
| 400 | } |
| 401 | |
jcampan@chromium.org | eac5717 | 2009-07-02 04:53:59 +0900 | [diff] [blame] | 402 | bool MessageLoop::IsNested() { |
| 403 | return state_->run_depth > 1; |
| 404 | } |
| 405 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 406 | //------------------------------------------------------------------------------ |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 407 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 408 | void MessageLoop::RunTask(Task* task) { |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 409 | DCHECK(nestable_tasks_allowed_); |
| 410 | // Execute the task and assume the worst: It is probably not reentrant. |
| 411 | nestable_tasks_allowed_ = false; |
darin@google.com | be165ae | 2008-09-07 17:08:29 +0900 | [diff] [blame] | 412 | |
| 413 | HistogramEvent(kTaskRunEvent); |
willchan@chromium.org | a904763 | 2010-06-10 06:20:41 +0900 | [diff] [blame] | 414 | FOR_EACH_OBSERVER(TaskObserver, task_observers_, |
davemoore@chromium.org | eb5f68f | 2010-10-27 08:40:48 +0900 | [diff] [blame^] | 415 | WillProcessTask(task)); |
darin@google.com | be165ae | 2008-09-07 17:08:29 +0900 | [diff] [blame] | 416 | task->Run(); |
davemoore@chromium.org | eb5f68f | 2010-10-27 08:40:48 +0900 | [diff] [blame^] | 417 | FOR_EACH_OBSERVER(TaskObserver, task_observers_, DidProcessTask(task)); |
darin@google.com | be165ae | 2008-09-07 17:08:29 +0900 | [diff] [blame] | 418 | delete task; |
| 419 | |
| 420 | nestable_tasks_allowed_ = true; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 421 | } |
| 422 | |
darin@google.com | be165ae | 2008-09-07 17:08:29 +0900 | [diff] [blame] | 423 | bool MessageLoop::DeferOrRunPendingTask(const PendingTask& pending_task) { |
| 424 | if (pending_task.nestable || state_->run_depth == 1) { |
| 425 | RunTask(pending_task.task); |
| 426 | // Show that we ran a task (Note: a new one might arrive as a |
| 427 | // consequence!). |
| 428 | return true; |
| 429 | } |
| 430 | |
| 431 | // We couldn't run the task now because we're in a nested message loop |
| 432 | // and the task isn't nestable. |
| 433 | deferred_non_nestable_work_queue_.push(pending_task); |
| 434 | return false; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 435 | } |
| 436 | |
darin@chromium.org | e3af17f | 2008-09-10 09:37:07 +0900 | [diff] [blame] | 437 | void MessageLoop::AddToDelayedWorkQueue(const PendingTask& pending_task) { |
| 438 | // Move to the delayed work queue. Initialize the sequence number |
| 439 | // before inserting into the delayed_work_queue_. The sequence number |
| 440 | // is used to faciliate FIFO sorting when two tasks have the same |
| 441 | // delayed_run_time value. |
| 442 | PendingTask new_pending_task(pending_task); |
| 443 | new_pending_task.sequence_num = next_sequence_num_++; |
| 444 | delayed_work_queue_.push(new_pending_task); |
| 445 | } |
| 446 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 447 | void MessageLoop::ReloadWorkQueue() { |
| 448 | // We can improve performance of our loading tasks from incoming_queue_ to |
darin@google.com | 981f355 | 2008-08-16 12:09:05 +0900 | [diff] [blame] | 449 | // work_queue_ by waiting until the last minute (work_queue_ is empty) to |
| 450 | // load. That reduces the number of locks-per-task significantly when our |
darin@google.com | be165ae | 2008-09-07 17:08:29 +0900 | [diff] [blame] | 451 | // queues get large. |
| 452 | if (!work_queue_.empty()) |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 453 | return; // Wait till we *really* need to lock and load. |
| 454 | |
| 455 | // Acquire all we can from the inter-thread queue with one lock acquisition. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 456 | { |
| 457 | AutoLock lock(incoming_queue_lock_); |
darin@google.com | be165ae | 2008-09-07 17:08:29 +0900 | [diff] [blame] | 458 | if (incoming_queue_.empty()) |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 459 | return; |
darin@chromium.org | b80ef1a | 2009-09-03 05:05:21 +0900 | [diff] [blame] | 460 | incoming_queue_.Swap(&work_queue_); // Constant time |
darin@google.com | be165ae | 2008-09-07 17:08:29 +0900 | [diff] [blame] | 461 | DCHECK(incoming_queue_.empty()); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 462 | } |
| 463 | } |
| 464 | |
darin@chromium.org | e3af17f | 2008-09-10 09:37:07 +0900 | [diff] [blame] | 465 | bool MessageLoop::DeletePendingTasks() { |
| 466 | bool did_work = !work_queue_.empty(); |
| 467 | while (!work_queue_.empty()) { |
| 468 | PendingTask pending_task = work_queue_.front(); |
| 469 | work_queue_.pop(); |
| 470 | if (!pending_task.delayed_run_time.is_null()) { |
| 471 | // We want to delete delayed tasks in the same order in which they would |
| 472 | // normally be deleted in case of any funny dependencies between delayed |
| 473 | // tasks. |
| 474 | AddToDelayedWorkQueue(pending_task); |
| 475 | } else { |
akalin@chromium.org | 839060b | 2010-08-03 12:06:21 +0900 | [diff] [blame] | 476 | // TODO(darin): Delete all tasks once it is safe to do so. |
| 477 | // Until it is totally safe, just do it when running Purify or |
| 478 | // Valgrind. |
thestig@chromium.org | d4a0a92 | 2010-10-15 11:38:43 +0900 | [diff] [blame] | 479 | #if defined(PURIFY) |
jar@chromium.org | 6377235 | 2009-03-12 05:06:02 +0900 | [diff] [blame] | 480 | delete pending_task.task; |
akalin@chromium.org | 839060b | 2010-08-03 12:06:21 +0900 | [diff] [blame] | 481 | #elif defined(OS_POSIX) |
| 482 | if (RUNNING_ON_VALGRIND) |
| 483 | delete pending_task.task; |
| 484 | #endif // defined(OS_POSIX) |
darin@chromium.org | e3af17f | 2008-09-10 09:37:07 +0900 | [diff] [blame] | 485 | } |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 486 | } |
darin@chromium.org | e3af17f | 2008-09-10 09:37:07 +0900 | [diff] [blame] | 487 | did_work |= !deferred_non_nestable_work_queue_.empty(); |
| 488 | while (!deferred_non_nestable_work_queue_.empty()) { |
akalin@chromium.org | 839060b | 2010-08-03 12:06:21 +0900 | [diff] [blame] | 489 | // TODO(darin): Delete all tasks once it is safe to do so. |
| 490 | // Until it is totaly safe, only delete them under Purify and Valgrind. |
| 491 | Task* task = NULL; |
thestig@chromium.org | d4a0a92 | 2010-10-15 11:38:43 +0900 | [diff] [blame] | 492 | #if defined(PURIFY) |
akalin@chromium.org | 839060b | 2010-08-03 12:06:21 +0900 | [diff] [blame] | 493 | task = deferred_non_nestable_work_queue_.front().task; |
| 494 | #elif defined(OS_POSIX) |
| 495 | if (RUNNING_ON_VALGRIND) |
| 496 | task = deferred_non_nestable_work_queue_.front().task; |
| 497 | #endif |
jar@chromium.org | 2fa6b4b | 2009-03-12 04:53:50 +0900 | [diff] [blame] | 498 | deferred_non_nestable_work_queue_.pop(); |
akalin@chromium.org | 839060b | 2010-08-03 12:06:21 +0900 | [diff] [blame] | 499 | if (task) |
| 500 | delete task; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 501 | } |
darin@chromium.org | e3af17f | 2008-09-10 09:37:07 +0900 | [diff] [blame] | 502 | did_work |= !delayed_work_queue_.empty(); |
| 503 | while (!delayed_work_queue_.empty()) { |
akalin@chromium.org | 839060b | 2010-08-03 12:06:21 +0900 | [diff] [blame] | 504 | Task* task = delayed_work_queue_.top().task; |
darin@chromium.org | e3af17f | 2008-09-10 09:37:07 +0900 | [diff] [blame] | 505 | delayed_work_queue_.pop(); |
akalin@chromium.org | 839060b | 2010-08-03 12:06:21 +0900 | [diff] [blame] | 506 | delete task; |
darin@chromium.org | e3af17f | 2008-09-10 09:37:07 +0900 | [diff] [blame] | 507 | } |
| 508 | return did_work; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 509 | } |
| 510 | |
darin@google.com | 981f355 | 2008-08-16 12:09:05 +0900 | [diff] [blame] | 511 | bool MessageLoop::DoWork() { |
darin@google.com | be165ae | 2008-09-07 17:08:29 +0900 | [diff] [blame] | 512 | if (!nestable_tasks_allowed_) { |
| 513 | // Task can't be executed right now. |
| 514 | return false; |
| 515 | } |
| 516 | |
| 517 | for (;;) { |
| 518 | ReloadWorkQueue(); |
| 519 | if (work_queue_.empty()) |
| 520 | break; |
| 521 | |
| 522 | // Execute oldest task. |
| 523 | do { |
| 524 | PendingTask pending_task = work_queue_.front(); |
| 525 | work_queue_.pop(); |
| 526 | if (!pending_task.delayed_run_time.is_null()) { |
darin@chromium.org | e3af17f | 2008-09-10 09:37:07 +0900 | [diff] [blame] | 527 | AddToDelayedWorkQueue(pending_task); |
darin@chromium.org | b2d3345 | 2008-09-24 04:19:20 +0900 | [diff] [blame] | 528 | // If we changed the topmost task, then it is time to re-schedule. |
jar@chromium.org | 4035507 | 2010-10-21 15:32:33 +0900 | [diff] [blame] | 529 | if (delayed_work_queue_.top().task == pending_task.task) |
darin@google.com | be165ae | 2008-09-07 17:08:29 +0900 | [diff] [blame] | 530 | pump_->ScheduleDelayedWork(pending_task.delayed_run_time); |
| 531 | } else { |
| 532 | if (DeferOrRunPendingTask(pending_task)) |
| 533 | return true; |
| 534 | } |
| 535 | } while (!work_queue_.empty()); |
| 536 | } |
| 537 | |
| 538 | // Nothing happened. |
| 539 | return false; |
darin@google.com | 981f355 | 2008-08-16 12:09:05 +0900 | [diff] [blame] | 540 | } |
| 541 | |
rjkroege@google.com | 3080f44 | 2010-10-23 01:17:47 +0900 | [diff] [blame] | 542 | bool MessageLoop::DoDelayedWork(base::Time* next_delayed_work_time) { |
jar@chromium.org | 4035507 | 2010-10-21 15:32:33 +0900 | [diff] [blame] | 543 | if (!nestable_tasks_allowed_ || delayed_work_queue_.empty()) { |
rjkroege@google.com | 3080f44 | 2010-10-23 01:17:47 +0900 | [diff] [blame] | 544 | *next_delayed_work_time = base::Time(); |
darin@google.com | be165ae | 2008-09-07 17:08:29 +0900 | [diff] [blame] | 545 | return false; |
| 546 | } |
jeremy@chromium.org | 2ed223c | 2008-12-09 02:36:06 +0900 | [diff] [blame] | 547 | |
jar@chromium.org | 4035507 | 2010-10-21 15:32:33 +0900 | [diff] [blame] | 548 | if (delayed_work_queue_.top().delayed_run_time > Time::Now()) { |
| 549 | *next_delayed_work_time = delayed_work_queue_.top().delayed_run_time; |
| 550 | return false; |
darin@google.com | be165ae | 2008-09-07 17:08:29 +0900 | [diff] [blame] | 551 | } |
darin@google.com | 981f355 | 2008-08-16 12:09:05 +0900 | [diff] [blame] | 552 | |
jar@chromium.org | 4035507 | 2010-10-21 15:32:33 +0900 | [diff] [blame] | 553 | PendingTask pending_task = delayed_work_queue_.top(); |
| 554 | delayed_work_queue_.pop(); |
jeremy@chromium.org | 2ed223c | 2008-12-09 02:36:06 +0900 | [diff] [blame] | 555 | |
jar@chromium.org | 4035507 | 2010-10-21 15:32:33 +0900 | [diff] [blame] | 556 | if (!delayed_work_queue_.empty()) |
darin@google.com | be165ae | 2008-09-07 17:08:29 +0900 | [diff] [blame] | 557 | *next_delayed_work_time = delayed_work_queue_.top().delayed_run_time; |
darin@google.com | 981f355 | 2008-08-16 12:09:05 +0900 | [diff] [blame] | 558 | |
darin@google.com | be165ae | 2008-09-07 17:08:29 +0900 | [diff] [blame] | 559 | return DeferOrRunPendingTask(pending_task); |
darin@google.com | 981f355 | 2008-08-16 12:09:05 +0900 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | bool MessageLoop::DoIdleWork() { |
| 563 | if (ProcessNextDelayedNonNestableTask()) |
| 564 | return true; |
| 565 | |
| 566 | if (state_->quit_received) |
| 567 | pump_->Quit(); |
| 568 | |
| 569 | return false; |
| 570 | } |
| 571 | |
| 572 | //------------------------------------------------------------------------------ |
| 573 | // MessageLoop::AutoRunState |
| 574 | |
| 575 | MessageLoop::AutoRunState::AutoRunState(MessageLoop* loop) : loop_(loop) { |
| 576 | // Make the loop reference us. |
| 577 | previous_state_ = loop_->state_; |
| 578 | if (previous_state_) { |
| 579 | run_depth = previous_state_->run_depth + 1; |
darin@google.com | 6ddeb84 | 2008-08-15 16:31:20 +0900 | [diff] [blame] | 580 | } else { |
darin@google.com | 981f355 | 2008-08-16 12:09:05 +0900 | [diff] [blame] | 581 | run_depth = 1; |
darin@google.com | 6ddeb84 | 2008-08-15 16:31:20 +0900 | [diff] [blame] | 582 | } |
darin@google.com | 981f355 | 2008-08-16 12:09:05 +0900 | [diff] [blame] | 583 | loop_->state_ = this; |
| 584 | |
| 585 | // Initialize the other fields: |
| 586 | quit_received = false; |
evan@chromium.org | 875bb6e | 2009-12-29 09:32:52 +0900 | [diff] [blame] | 587 | #if !defined(OS_MACOSX) |
darin@google.com | 981f355 | 2008-08-16 12:09:05 +0900 | [diff] [blame] | 588 | dispatcher = NULL; |
| 589 | #endif |
| 590 | } |
| 591 | |
| 592 | MessageLoop::AutoRunState::~AutoRunState() { |
| 593 | loop_->state_ = previous_state_; |
darin@google.com | ee6fa72 | 2008-08-13 08:25:43 +0900 | [diff] [blame] | 594 | } |
| 595 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 596 | //------------------------------------------------------------------------------ |
darin@google.com | be165ae | 2008-09-07 17:08:29 +0900 | [diff] [blame] | 597 | // MessageLoop::PendingTask |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 598 | |
darin@google.com | be165ae | 2008-09-07 17:08:29 +0900 | [diff] [blame] | 599 | bool MessageLoop::PendingTask::operator<(const PendingTask& other) const { |
| 600 | // Since the top of a priority queue is defined as the "greatest" element, we |
| 601 | // need to invert the comparison here. We want the smaller time to be at the |
| 602 | // top of the heap. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 603 | |
darin@google.com | be165ae | 2008-09-07 17:08:29 +0900 | [diff] [blame] | 604 | if (delayed_run_time < other.delayed_run_time) |
| 605 | return false; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 606 | |
darin@google.com | be165ae | 2008-09-07 17:08:29 +0900 | [diff] [blame] | 607 | if (delayed_run_time > other.delayed_run_time) |
| 608 | return true; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 609 | |
darin@google.com | be165ae | 2008-09-07 17:08:29 +0900 | [diff] [blame] | 610 | // If the times happen to match, then we use the sequence number to decide. |
| 611 | // Compare the difference to support integer roll-over. |
| 612 | return (sequence_num - other.sequence_num) > 0; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | //------------------------------------------------------------------------------ |
| 616 | // Method and data for histogramming events and actions taken by each instance |
| 617 | // on each thread. |
| 618 | |
| 619 | // static |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 620 | void MessageLoop::EnableHistogrammer(bool enable) { |
| 621 | enable_histogrammer_ = enable; |
| 622 | } |
| 623 | |
| 624 | void MessageLoop::StartHistogrammer() { |
| 625 | if (enable_histogrammer_ && !message_histogram_.get() |
brettw@chromium.org | 275c2ec | 2010-10-14 13:38:38 +0900 | [diff] [blame] | 626 | && base::StatisticsRecorder::WasStarted()) { |
darin@google.com | 981f355 | 2008-08-16 12:09:05 +0900 | [diff] [blame] | 627 | DCHECK(!thread_name_.empty()); |
brettw@chromium.org | 275c2ec | 2010-10-14 13:38:38 +0900 | [diff] [blame] | 628 | message_histogram_ = base::LinearHistogram::FactoryGet( |
| 629 | "MsgLoop:" + thread_name_, |
jar@chromium.org | ed5238a | 2009-12-28 15:59:52 +0900 | [diff] [blame] | 630 | kLeastNonZeroMessageId, kMaxMessageId, |
| 631 | kNumberOfDistinctMessagesDisplayed, |
| 632 | message_histogram_->kHexRangePrintingFlag); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 633 | message_histogram_->SetRangeDescriptions(event_descriptions_); |
| 634 | } |
| 635 | } |
| 636 | |
| 637 | void MessageLoop::HistogramEvent(int event) { |
| 638 | if (message_histogram_.get()) |
| 639 | message_histogram_->Add(event); |
| 640 | } |
| 641 | |
darin@google.com | d936b5b | 2008-08-26 14:53:57 +0900 | [diff] [blame] | 642 | //------------------------------------------------------------------------------ |
| 643 | // MessageLoopForUI |
| 644 | |
| 645 | #if defined(OS_WIN) |
darin@google.com | d936b5b | 2008-08-26 14:53:57 +0900 | [diff] [blame] | 646 | void MessageLoopForUI::DidProcessMessage(const MSG& message) { |
| 647 | pump_win()->DidProcessMessage(message); |
| 648 | } |
darin@google.com | d936b5b | 2008-08-26 14:53:57 +0900 | [diff] [blame] | 649 | #endif // defined(OS_WIN) |
| 650 | |
evan@chromium.org | 875bb6e | 2009-12-29 09:32:52 +0900 | [diff] [blame] | 651 | #if !defined(OS_MACOSX) |
jcampan@chromium.org | 0542358 | 2009-08-01 07:53:37 +0900 | [diff] [blame] | 652 | void MessageLoopForUI::AddObserver(Observer* observer) { |
| 653 | pump_ui()->AddObserver(observer); |
| 654 | } |
| 655 | |
| 656 | void MessageLoopForUI::RemoveObserver(Observer* observer) { |
| 657 | pump_ui()->RemoveObserver(observer); |
| 658 | } |
| 659 | |
| 660 | void MessageLoopForUI::Run(Dispatcher* dispatcher) { |
| 661 | AutoRunState save_state(this); |
| 662 | state_->dispatcher = dispatcher; |
| 663 | RunHandler(); |
| 664 | } |
evan@chromium.org | 875bb6e | 2009-12-29 09:32:52 +0900 | [diff] [blame] | 665 | #endif // !defined(OS_MACOSX) |
jcampan@chromium.org | 0542358 | 2009-08-01 07:53:37 +0900 | [diff] [blame] | 666 | |
darin@google.com | d936b5b | 2008-08-26 14:53:57 +0900 | [diff] [blame] | 667 | //------------------------------------------------------------------------------ |
| 668 | // MessageLoopForIO |
| 669 | |
| 670 | #if defined(OS_WIN) |
| 671 | |
rvargas@google.com | 9e49aa2 | 2008-10-10 08:58:43 +0900 | [diff] [blame] | 672 | void MessageLoopForIO::RegisterIOHandler(HANDLE file, IOHandler* handler) { |
| 673 | pump_io()->RegisterIOHandler(file, handler); |
| 674 | } |
| 675 | |
rvargas@google.com | 7388754 | 2008-11-08 06:52:15 +0900 | [diff] [blame] | 676 | bool MessageLoopForIO::WaitForIOCompletion(DWORD timeout, IOHandler* filter) { |
| 677 | return pump_io()->WaitForIOCompletion(timeout, filter); |
rvargas@google.com | 9e49aa2 | 2008-10-10 08:58:43 +0900 | [diff] [blame] | 678 | } |
| 679 | |
dkegel@google.com | 9e044ae | 2008-09-19 03:46:26 +0900 | [diff] [blame] | 680 | #elif defined(OS_POSIX) |
| 681 | |
jeremy@chromium.org | efc0db0 | 2008-12-16 07:02:17 +0900 | [diff] [blame] | 682 | bool MessageLoopForIO::WatchFileDescriptor(int fd, |
| 683 | bool persistent, |
| 684 | Mode mode, |
| 685 | FileDescriptorWatcher *controller, |
| 686 | Watcher *delegate) { |
| 687 | return pump_libevent()->WatchFileDescriptor( |
| 688 | fd, |
| 689 | persistent, |
| 690 | static_cast<base::MessagePumpLibevent::Mode>(mode), |
| 691 | controller, |
| 692 | delegate); |
dkegel@google.com | 9e044ae | 2008-09-19 03:46:26 +0900 | [diff] [blame] | 693 | } |
| 694 | |
dkegel@google.com | 9e044ae | 2008-09-19 03:46:26 +0900 | [diff] [blame] | 695 | #endif |