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