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