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