| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/message_loop.h" | 5 #include "base/message_loop.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
| (...skipping 21 matching lines...) Expand all Loading... |
| 32 #endif | 32 #endif |
| 33 #if defined(OS_ANDROID) | 33 #if defined(OS_ANDROID) |
| 34 #include "base/message_pump_android.h" | 34 #include "base/message_pump_android.h" |
| 35 #endif | 35 #endif |
| 36 | 36 |
| 37 #if defined(TOOLKIT_GTK) | 37 #if defined(TOOLKIT_GTK) |
| 38 #include <gdk/gdk.h> | 38 #include <gdk/gdk.h> |
| 39 #include <gdk/gdkx.h> | 39 #include <gdk/gdkx.h> |
| 40 #endif | 40 #endif |
| 41 | 41 |
| 42 using base::PendingTask; | 42 namespace base { |
| 43 using base::TimeDelta; | |
| 44 using base::TimeTicks; | |
| 45 | 43 |
| 46 namespace { | 44 namespace { |
| 47 | 45 |
| 48 // A lazily created thread local storage for quick access to a thread's message | 46 // A lazily created thread local storage for quick access to a thread's message |
| 49 // loop, if one exists. This should be safe and free of static constructors. | 47 // loop, if one exists. This should be safe and free of static constructors. |
| 50 base::LazyInstance<base::ThreadLocalPointer<MessageLoop> > lazy_tls_ptr = | 48 LazyInstance<base::ThreadLocalPointer<MessageLoop> > lazy_tls_ptr = |
| 51 LAZY_INSTANCE_INITIALIZER; | 49 LAZY_INSTANCE_INITIALIZER; |
| 52 | 50 |
| 53 // Logical events for Histogram profiling. Run with -message-loop-histogrammer | 51 // Logical events for Histogram profiling. Run with -message-loop-histogrammer |
| 54 // to get an accounting of messages and actions taken on each thread. | 52 // to get an accounting of messages and actions taken on each thread. |
| 55 const int kTaskRunEvent = 0x1; | 53 const int kTaskRunEvent = 0x1; |
| 56 const int kTimerEvent = 0x2; | 54 const int kTimerEvent = 0x2; |
| 57 | 55 |
| 58 // Provide range of message IDs for use in histogramming and debug display. | 56 // Provide range of message IDs for use in histogramming and debug display. |
| 59 const int kLeastNonZeroMessageId = 1; | 57 const int kLeastNonZeroMessageId = 1; |
| 60 const int kMaxMessageId = 1099; | 58 const int kMaxMessageId = 1099; |
| 61 const int kNumberOfDistinctMessagesDisplayed = 1100; | 59 const int kNumberOfDistinctMessagesDisplayed = 1100; |
| 62 | 60 |
| 63 // Provide a macro that takes an expression (such as a constant, or macro | 61 // Provide a macro that takes an expression (such as a constant, or macro |
| 64 // constant) and creates a pair to initalize an array of pairs. In this case, | 62 // constant) and creates a pair to initalize an array of pairs. In this case, |
| 65 // our pair consists of the expressions value, and the "stringized" version | 63 // our pair consists of the expressions value, and the "stringized" version |
| 66 // of the expression (i.e., the exrpression put in quotes). For example, if | 64 // of the expression (i.e., the exrpression put in quotes). For example, if |
| 67 // we have: | 65 // we have: |
| 68 // #define FOO 2 | 66 // #define FOO 2 |
| 69 // #define BAR 5 | 67 // #define BAR 5 |
| 70 // then the following: | 68 // then the following: |
| 71 // VALUE_TO_NUMBER_AND_NAME(FOO + BAR) | 69 // VALUE_TO_NUMBER_AND_NAME(FOO + BAR) |
| 72 // will expand to: | 70 // will expand to: |
| 73 // {7, "FOO + BAR"} | 71 // {7, "FOO + BAR"} |
| 74 // We use the resulting array as an argument to our histogram, which reads the | 72 // We use the resulting array as an argument to our histogram, which reads the |
| 75 // number as a bucket identifier, and proceeds to use the corresponding name | 73 // number as a bucket identifier, and proceeds to use the corresponding name |
| 76 // in the pair (i.e., the quoted string) when printing out a histogram. | 74 // in the pair (i.e., the quoted string) when printing out a histogram. |
| 77 #define VALUE_TO_NUMBER_AND_NAME(name) {name, #name}, | 75 #define VALUE_TO_NUMBER_AND_NAME(name) {name, #name}, |
| 78 | 76 |
| 79 const base::LinearHistogram::DescriptionPair event_descriptions_[] = { | 77 const LinearHistogram::DescriptionPair event_descriptions_[] = { |
| 80 // Provide some pretty print capability in our histogram for our internal | 78 // Provide some pretty print capability in our histogram for our internal |
| 81 // messages. | 79 // messages. |
| 82 | 80 |
| 83 // A few events we handle (kindred to messages), and used to profile actions. | 81 // A few events we handle (kindred to messages), and used to profile actions. |
| 84 VALUE_TO_NUMBER_AND_NAME(kTaskRunEvent) | 82 VALUE_TO_NUMBER_AND_NAME(kTaskRunEvent) |
| 85 VALUE_TO_NUMBER_AND_NAME(kTimerEvent) | 83 VALUE_TO_NUMBER_AND_NAME(kTimerEvent) |
| 86 | 84 |
| 87 {-1, NULL} // The list must be null terminated, per API to histogram. | 85 {-1, NULL} // The list must be null terminated, per API to histogram. |
| 88 }; | 86 }; |
| 89 | 87 |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 142 exception_restoration_(false), | 140 exception_restoration_(false), |
| 143 message_histogram_(NULL), | 141 message_histogram_(NULL), |
| 144 run_loop_(NULL), | 142 run_loop_(NULL), |
| 145 #if defined(OS_WIN) | 143 #if defined(OS_WIN) |
| 146 os_modal_loop_(false), | 144 os_modal_loop_(false), |
| 147 #endif // OS_WIN | 145 #endif // OS_WIN |
| 148 next_sequence_num_(0) { | 146 next_sequence_num_(0) { |
| 149 DCHECK(!current()) << "should only have one message loop per thread"; | 147 DCHECK(!current()) << "should only have one message loop per thread"; |
| 150 lazy_tls_ptr.Pointer()->Set(this); | 148 lazy_tls_ptr.Pointer()->Set(this); |
| 151 | 149 |
| 152 message_loop_proxy_ = new base::MessageLoopProxyImpl(); | 150 message_loop_proxy_ = new MessageLoopProxyImpl(); |
| 153 thread_task_runner_handle_.reset( | 151 thread_task_runner_handle_.reset( |
| 154 new base::ThreadTaskRunnerHandle(message_loop_proxy_)); | 152 new ThreadTaskRunnerHandle(message_loop_proxy_)); |
| 155 | 153 |
| 156 // TODO(rvargas): Get rid of the OS guards. | 154 // TODO(rvargas): Get rid of the OS guards. |
| 157 #if defined(OS_WIN) | 155 #if defined(OS_WIN) |
| 158 #define MESSAGE_PUMP_UI new base::MessagePumpForUI() | 156 #define MESSAGE_PUMP_UI new MessagePumpForUI() |
| 159 #define MESSAGE_PUMP_IO new base::MessagePumpForIO() | 157 #define MESSAGE_PUMP_IO new MessagePumpForIO() |
| 160 #elif defined(OS_IOS) | 158 #elif defined(OS_IOS) |
| 161 #define MESSAGE_PUMP_UI base::MessagePumpMac::Create() | 159 #define MESSAGE_PUMP_UI MessagePumpMac::Create() |
| 162 #define MESSAGE_PUMP_IO new base::MessagePumpIOSForIO() | 160 #define MESSAGE_PUMP_IO new MessagePumpIOSForIO() |
| 163 #elif defined(OS_MACOSX) | 161 #elif defined(OS_MACOSX) |
| 164 #define MESSAGE_PUMP_UI base::MessagePumpMac::Create() | 162 #define MESSAGE_PUMP_UI MessagePumpMac::Create() |
| 165 #define MESSAGE_PUMP_IO new base::MessagePumpLibevent() | 163 #define MESSAGE_PUMP_IO new MessagePumpLibevent() |
| 166 #elif defined(OS_NACL) | 164 #elif defined(OS_NACL) |
| 167 // Currently NaCl doesn't have a UI MessageLoop. | 165 // Currently NaCl doesn't have a UI MessageLoop. |
| 168 // TODO(abarth): Figure out if we need this. | 166 // TODO(abarth): Figure out if we need this. |
| 169 #define MESSAGE_PUMP_UI NULL | 167 #define MESSAGE_PUMP_UI NULL |
| 170 // ipc_channel_nacl.cc uses a worker thread to do socket reads currently, and | 168 // ipc_channel_nacl.cc uses a worker thread to do socket reads currently, and |
| 171 // doesn't require extra support for watching file descriptors. | 169 // doesn't require extra support for watching file descriptors. |
| 172 #define MESSAGE_PUMP_IO new base::MessagePumpDefault(); | 170 #define MESSAGE_PUMP_IO new MessagePumpDefault(); |
| 173 #elif defined(OS_POSIX) // POSIX but not MACOSX. | 171 #elif defined(OS_POSIX) // POSIX but not MACOSX. |
| 174 #define MESSAGE_PUMP_UI new base::MessagePumpForUI() | 172 #define MESSAGE_PUMP_UI new MessagePumpForUI() |
| 175 #define MESSAGE_PUMP_IO new base::MessagePumpLibevent() | 173 #define MESSAGE_PUMP_IO new MessagePumpLibevent() |
| 176 #else | 174 #else |
| 177 #error Not implemented | 175 #error Not implemented |
| 178 #endif | 176 #endif |
| 179 | 177 |
| 180 if (type_ == TYPE_UI) { | 178 if (type_ == TYPE_UI) { |
| 181 if (message_pump_for_ui_factory_) | 179 if (message_pump_for_ui_factory_) |
| 182 pump_ = message_pump_for_ui_factory_(); | 180 pump_ = message_pump_for_ui_factory_(); |
| 183 else | 181 else |
| 184 pump_ = MESSAGE_PUMP_UI; | 182 pump_ = MESSAGE_PUMP_UI; |
| 185 } else if (type_ == TYPE_IO) { | 183 } else if (type_ == TYPE_IO) { |
| 186 pump_ = MESSAGE_PUMP_IO; | 184 pump_ = MESSAGE_PUMP_IO; |
| 187 } else { | 185 } else { |
| 188 DCHECK_EQ(TYPE_DEFAULT, type_); | 186 DCHECK_EQ(TYPE_DEFAULT, type_); |
| 189 pump_ = new base::MessagePumpDefault(); | 187 pump_ = new MessagePumpDefault(); |
| 190 } | 188 } |
| 191 } | 189 } |
| 192 | 190 |
| 193 MessageLoop::~MessageLoop() { | 191 MessageLoop::~MessageLoop() { |
| 194 DCHECK_EQ(this, current()); | 192 DCHECK_EQ(this, current()); |
| 195 | 193 |
| 196 DCHECK(!run_loop_); | 194 DCHECK(!run_loop_); |
| 197 | 195 |
| 198 // Clean up any unprocessed tasks, but take care: deleting a task could | 196 // Clean up any unprocessed tasks, but take care: deleting a task could |
| 199 // result in the addition of more tasks (e.g., via DeleteSoon). We set a | 197 // result in the addition of more tasks (e.g., via DeleteSoon). We set a |
| (...skipping 12 matching lines...) Expand all Loading... |
| 212 } | 210 } |
| 213 DCHECK(!did_work); | 211 DCHECK(!did_work); |
| 214 | 212 |
| 215 // Let interested parties have one last shot at accessing this. | 213 // Let interested parties have one last shot at accessing this. |
| 216 FOR_EACH_OBSERVER(DestructionObserver, destruction_observers_, | 214 FOR_EACH_OBSERVER(DestructionObserver, destruction_observers_, |
| 217 WillDestroyCurrentMessageLoop()); | 215 WillDestroyCurrentMessageLoop()); |
| 218 | 216 |
| 219 thread_task_runner_handle_.reset(); | 217 thread_task_runner_handle_.reset(); |
| 220 | 218 |
| 221 // Tell the message_loop_proxy that we are dying. | 219 // Tell the message_loop_proxy that we are dying. |
| 222 static_cast<base::MessageLoopProxyImpl*>(message_loop_proxy_.get())-> | 220 static_cast<MessageLoopProxyImpl*>(message_loop_proxy_.get())-> |
| 223 WillDestroyCurrentMessageLoop(); | 221 WillDestroyCurrentMessageLoop(); |
| 224 message_loop_proxy_ = NULL; | 222 message_loop_proxy_ = NULL; |
| 225 | 223 |
| 226 // OK, now make it so that no one can find us. | 224 // OK, now make it so that no one can find us. |
| 227 lazy_tls_ptr.Pointer()->Set(NULL); | 225 lazy_tls_ptr.Pointer()->Set(NULL); |
| 228 | 226 |
| 229 #if defined(OS_WIN) | 227 #if defined(OS_WIN) |
| 230 // If we left the high-resolution timer activated, deactivate it now. | 228 // If we left the high-resolution timer activated, deactivate it now. |
| 231 // Doing this is not-critical, it is mainly to make sure we track | 229 // Doing this is not-critical, it is mainly to make sure we track |
| 232 // the high resolution timer activations properly in our unit tests. | 230 // the high resolution timer activations properly in our unit tests. |
| 233 if (!high_resolution_timer_expiration_.is_null()) { | 231 if (!high_resolution_timer_expiration_.is_null()) { |
| 234 base::Time::ActivateHighResolutionTimer(false); | 232 Time::ActivateHighResolutionTimer(false); |
| 235 high_resolution_timer_expiration_ = base::TimeTicks(); | 233 high_resolution_timer_expiration_ = TimeTicks(); |
| 236 } | 234 } |
| 237 #endif | 235 #endif |
| 238 } | 236 } |
| 239 | 237 |
| 240 // static | 238 // static |
| 241 MessageLoop* MessageLoop::current() { | 239 MessageLoop* MessageLoop::current() { |
| 242 // TODO(darin): sadly, we cannot enable this yet since people call us even | 240 // TODO(darin): sadly, we cannot enable this yet since people call us even |
| 243 // when they have no intention of using us. | 241 // when they have no intention of using us. |
| 244 // DCHECK(loop) << "Ouch, did you forget to initialize me?"; | 242 // DCHECK(loop) << "Ouch, did you forget to initialize me?"; |
| 245 return lazy_tls_ptr.Pointer()->Get(); | 243 return lazy_tls_ptr.Pointer()->Get(); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 265 destruction_observers_.AddObserver(destruction_observer); | 263 destruction_observers_.AddObserver(destruction_observer); |
| 266 } | 264 } |
| 267 | 265 |
| 268 void MessageLoop::RemoveDestructionObserver( | 266 void MessageLoop::RemoveDestructionObserver( |
| 269 DestructionObserver* destruction_observer) { | 267 DestructionObserver* destruction_observer) { |
| 270 DCHECK_EQ(this, current()); | 268 DCHECK_EQ(this, current()); |
| 271 destruction_observers_.RemoveObserver(destruction_observer); | 269 destruction_observers_.RemoveObserver(destruction_observer); |
| 272 } | 270 } |
| 273 | 271 |
| 274 void MessageLoop::PostTask( | 272 void MessageLoop::PostTask( |
| 275 const tracked_objects::Location& from_here, const base::Closure& task) { | 273 const tracked_objects::Location& from_here, const Closure& task) { |
| 276 DCHECK(!task.is_null()) << from_here.ToString(); | 274 DCHECK(!task.is_null()) << from_here.ToString(); |
| 277 PendingTask pending_task( | 275 PendingTask pending_task( |
| 278 from_here, task, CalculateDelayedRuntime(TimeDelta()), true); | 276 from_here, task, CalculateDelayedRuntime(TimeDelta()), true); |
| 279 AddToIncomingQueue(&pending_task); | 277 AddToIncomingQueue(&pending_task); |
| 280 } | 278 } |
| 281 | 279 |
| 282 void MessageLoop::PostDelayedTask( | 280 void MessageLoop::PostDelayedTask( |
| 283 const tracked_objects::Location& from_here, | 281 const tracked_objects::Location& from_here, |
| 284 const base::Closure& task, | 282 const Closure& task, |
| 285 TimeDelta delay) { | 283 TimeDelta delay) { |
| 286 DCHECK(!task.is_null()) << from_here.ToString(); | 284 DCHECK(!task.is_null()) << from_here.ToString(); |
| 287 PendingTask pending_task( | 285 PendingTask pending_task( |
| 288 from_here, task, CalculateDelayedRuntime(delay), true); | 286 from_here, task, CalculateDelayedRuntime(delay), true); |
| 289 AddToIncomingQueue(&pending_task); | 287 AddToIncomingQueue(&pending_task); |
| 290 } | 288 } |
| 291 | 289 |
| 292 void MessageLoop::PostNonNestableTask( | 290 void MessageLoop::PostNonNestableTask( |
| 293 const tracked_objects::Location& from_here, | 291 const tracked_objects::Location& from_here, |
| 294 const base::Closure& task) { | 292 const Closure& task) { |
| 295 DCHECK(!task.is_null()) << from_here.ToString(); | 293 DCHECK(!task.is_null()) << from_here.ToString(); |
| 296 PendingTask pending_task( | 294 PendingTask pending_task( |
| 297 from_here, task, CalculateDelayedRuntime(TimeDelta()), false); | 295 from_here, task, CalculateDelayedRuntime(TimeDelta()), false); |
| 298 AddToIncomingQueue(&pending_task); | 296 AddToIncomingQueue(&pending_task); |
| 299 } | 297 } |
| 300 | 298 |
| 301 void MessageLoop::PostNonNestableDelayedTask( | 299 void MessageLoop::PostNonNestableDelayedTask( |
| 302 const tracked_objects::Location& from_here, | 300 const tracked_objects::Location& from_here, |
| 303 const base::Closure& task, | 301 const Closure& task, |
| 304 TimeDelta delay) { | 302 TimeDelta delay) { |
| 305 DCHECK(!task.is_null()) << from_here.ToString(); | 303 DCHECK(!task.is_null()) << from_here.ToString(); |
| 306 PendingTask pending_task( | 304 PendingTask pending_task( |
| 307 from_here, task, CalculateDelayedRuntime(delay), false); | 305 from_here, task, CalculateDelayedRuntime(delay), false); |
| 308 AddToIncomingQueue(&pending_task); | 306 AddToIncomingQueue(&pending_task); |
| 309 } | 307 } |
| 310 | 308 |
| 311 void MessageLoop::Run() { | 309 void MessageLoop::Run() { |
| 312 base::RunLoop run_loop; | 310 RunLoop run_loop; |
| 313 run_loop.Run(); | 311 run_loop.Run(); |
| 314 } | 312 } |
| 315 | 313 |
| 316 void MessageLoop::RunUntilIdle() { | 314 void MessageLoop::RunUntilIdle() { |
| 317 base::RunLoop run_loop; | 315 RunLoop run_loop; |
| 318 run_loop.RunUntilIdle(); | 316 run_loop.RunUntilIdle(); |
| 319 } | 317 } |
| 320 | 318 |
| 321 void MessageLoop::QuitWhenIdle() { | 319 void MessageLoop::QuitWhenIdle() { |
| 322 DCHECK_EQ(this, current()); | 320 DCHECK_EQ(this, current()); |
| 323 if (run_loop_) { | 321 if (run_loop_) { |
| 324 run_loop_->quit_when_idle_received_ = true; | 322 run_loop_->quit_when_idle_received_ = true; |
| 325 } else { | 323 } else { |
| 326 NOTREACHED() << "Must be inside Run to call Quit"; | 324 NOTREACHED() << "Must be inside Run to call Quit"; |
| 327 } | 325 } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 338 | 336 |
| 339 bool MessageLoop::IsType(Type type) const { | 337 bool MessageLoop::IsType(Type type) const { |
| 340 return type_ == type; | 338 return type_ == type; |
| 341 } | 339 } |
| 342 | 340 |
| 343 static void QuitCurrentWhenIdle() { | 341 static void QuitCurrentWhenIdle() { |
| 344 MessageLoop::current()->QuitWhenIdle(); | 342 MessageLoop::current()->QuitWhenIdle(); |
| 345 } | 343 } |
| 346 | 344 |
| 347 // static | 345 // static |
| 348 base::Closure MessageLoop::QuitWhenIdleClosure() { | 346 Closure MessageLoop::QuitWhenIdleClosure() { |
| 349 return base::Bind(&QuitCurrentWhenIdle); | 347 return Bind(&QuitCurrentWhenIdle); |
| 350 } | 348 } |
| 351 | 349 |
| 352 void MessageLoop::SetNestableTasksAllowed(bool allowed) { | 350 void MessageLoop::SetNestableTasksAllowed(bool allowed) { |
| 353 if (nestable_tasks_allowed_ != allowed) { | 351 if (nestable_tasks_allowed_ != allowed) { |
| 354 nestable_tasks_allowed_ = allowed; | 352 nestable_tasks_allowed_ = allowed; |
| 355 if (!nestable_tasks_allowed_) | 353 if (!nestable_tasks_allowed_) |
| 356 return; | 354 return; |
| 357 // Start the native pump if we are not already pumping. | 355 // Start the native pump if we are not already pumping. |
| 358 pump_->ScheduleWork(); | 356 pump_->ScheduleWork(); |
| 359 } | 357 } |
| (...skipping 12 matching lines...) Expand all Loading... |
| 372 task_observers_.AddObserver(task_observer); | 370 task_observers_.AddObserver(task_observer); |
| 373 } | 371 } |
| 374 | 372 |
| 375 void MessageLoop::RemoveTaskObserver(TaskObserver* task_observer) { | 373 void MessageLoop::RemoveTaskObserver(TaskObserver* task_observer) { |
| 376 DCHECK_EQ(this, current()); | 374 DCHECK_EQ(this, current()); |
| 377 task_observers_.RemoveObserver(task_observer); | 375 task_observers_.RemoveObserver(task_observer); |
| 378 } | 376 } |
| 379 | 377 |
| 380 void MessageLoop::AssertIdle() const { | 378 void MessageLoop::AssertIdle() const { |
| 381 // We only check |incoming_queue_|, since we don't want to lock |work_queue_|. | 379 // We only check |incoming_queue_|, since we don't want to lock |work_queue_|. |
| 382 base::AutoLock lock(incoming_queue_lock_); | 380 AutoLock lock(incoming_queue_lock_); |
| 383 DCHECK(incoming_queue_.empty()); | 381 DCHECK(incoming_queue_.empty()); |
| 384 } | 382 } |
| 385 | 383 |
| 386 bool MessageLoop::is_running() const { | 384 bool MessageLoop::is_running() const { |
| 387 DCHECK_EQ(this, current()); | 385 DCHECK_EQ(this, current()); |
| 388 return run_loop_ != NULL; | 386 return run_loop_ != NULL; |
| 389 } | 387 } |
| 390 | 388 |
| 391 //------------------------------------------------------------------------------ | 389 //------------------------------------------------------------------------------ |
| 392 | 390 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 417 } | 415 } |
| 418 #endif | 416 #endif |
| 419 | 417 |
| 420 void MessageLoop::RunInternal() { | 418 void MessageLoop::RunInternal() { |
| 421 DCHECK_EQ(this, current()); | 419 DCHECK_EQ(this, current()); |
| 422 | 420 |
| 423 StartHistogrammer(); | 421 StartHistogrammer(); |
| 424 | 422 |
| 425 #if !defined(OS_MACOSX) && !defined(OS_ANDROID) | 423 #if !defined(OS_MACOSX) && !defined(OS_ANDROID) |
| 426 if (run_loop_->dispatcher_ && type() == TYPE_UI) { | 424 if (run_loop_->dispatcher_ && type() == TYPE_UI) { |
| 427 static_cast<base::MessagePumpForUI*>(pump_.get())-> | 425 static_cast<MessagePumpForUI*>(pump_.get())-> |
| 428 RunWithDispatcher(this, run_loop_->dispatcher_); | 426 RunWithDispatcher(this, run_loop_->dispatcher_); |
| 429 return; | 427 return; |
| 430 } | 428 } |
| 431 #endif | 429 #endif |
| 432 | 430 |
| 433 pump_->Run(this); | 431 pump_->Run(this); |
| 434 } | 432 } |
| 435 | 433 |
| 436 bool MessageLoop::ProcessNextDelayedNonNestableTask() { | 434 bool MessageLoop::ProcessNextDelayedNonNestableTask() { |
| 437 if (run_loop_->run_depth_ != 1) | 435 if (run_loop_->run_depth_ != 1) |
| (...skipping 19 matching lines...) Expand all Loading... |
| 457 // Execute the task and assume the worst: It is probably not reentrant. | 455 // Execute the task and assume the worst: It is probably not reentrant. |
| 458 nestable_tasks_allowed_ = false; | 456 nestable_tasks_allowed_ = false; |
| 459 | 457 |
| 460 // Before running the task, store the program counter where it was posted | 458 // Before running the task, store the program counter where it was posted |
| 461 // and deliberately alias it to ensure it is on the stack if the task | 459 // and deliberately alias it to ensure it is on the stack if the task |
| 462 // crashes. Be careful not to assume that the variable itself will have the | 460 // crashes. Be careful not to assume that the variable itself will have the |
| 463 // expected value when displayed by the optimizer in an optimized build. | 461 // expected value when displayed by the optimizer in an optimized build. |
| 464 // Look at a memory dump of the stack. | 462 // Look at a memory dump of the stack. |
| 465 const void* program_counter = | 463 const void* program_counter = |
| 466 pending_task.posted_from.program_counter(); | 464 pending_task.posted_from.program_counter(); |
| 467 base::debug::Alias(&program_counter); | 465 debug::Alias(&program_counter); |
| 468 | 466 |
| 469 HistogramEvent(kTaskRunEvent); | 467 HistogramEvent(kTaskRunEvent); |
| 470 | 468 |
| 471 tracked_objects::TrackedTime start_time = | 469 tracked_objects::TrackedTime start_time = |
| 472 tracked_objects::ThreadData::NowForStartOfRun(pending_task.birth_tally); | 470 tracked_objects::ThreadData::NowForStartOfRun(pending_task.birth_tally); |
| 473 | 471 |
| 474 FOR_EACH_OBSERVER(TaskObserver, task_observers_, | 472 FOR_EACH_OBSERVER(TaskObserver, task_observers_, |
| 475 WillProcessTask(pending_task)); | 473 WillProcessTask(pending_task)); |
| 476 pending_task.task.Run(); | 474 pending_task.task.Run(); |
| 477 FOR_EACH_OBSERVER(TaskObserver, task_observers_, | 475 FOR_EACH_OBSERVER(TaskObserver, task_observers_, |
| (...skipping 27 matching lines...) Expand all Loading... |
| 505 void MessageLoop::ReloadWorkQueue() { | 503 void MessageLoop::ReloadWorkQueue() { |
| 506 // We can improve performance of our loading tasks from incoming_queue_ to | 504 // We can improve performance of our loading tasks from incoming_queue_ to |
| 507 // work_queue_ by waiting until the last minute (work_queue_ is empty) to | 505 // work_queue_ by waiting until the last minute (work_queue_ is empty) to |
| 508 // load. That reduces the number of locks-per-task significantly when our | 506 // load. That reduces the number of locks-per-task significantly when our |
| 509 // queues get large. | 507 // queues get large. |
| 510 if (!work_queue_.empty()) | 508 if (!work_queue_.empty()) |
| 511 return; // Wait till we *really* need to lock and load. | 509 return; // Wait till we *really* need to lock and load. |
| 512 | 510 |
| 513 // Acquire all we can from the inter-thread queue with one lock acquisition. | 511 // Acquire all we can from the inter-thread queue with one lock acquisition. |
| 514 { | 512 { |
| 515 base::AutoLock lock(incoming_queue_lock_); | 513 AutoLock lock(incoming_queue_lock_); |
| 516 if (incoming_queue_.empty()) | 514 if (incoming_queue_.empty()) |
| 517 return; | 515 return; |
| 518 incoming_queue_.Swap(&work_queue_); // Constant time | 516 incoming_queue_.Swap(&work_queue_); // Constant time |
| 519 DCHECK(incoming_queue_.empty()); | 517 DCHECK(incoming_queue_.empty()); |
| 520 } | 518 } |
| 521 } | 519 } |
| 522 | 520 |
| 523 bool MessageLoop::DeletePendingTasks() { | 521 bool MessageLoop::DeletePendingTasks() { |
| 524 bool did_work = !work_queue_.empty(); | 522 bool did_work = !work_queue_.empty(); |
| 525 while (!work_queue_.empty()) { | 523 while (!work_queue_.empty()) { |
| (...skipping 29 matching lines...) Expand all Loading... |
| 555 delayed_run_time = TimeTicks::Now() + delay; | 553 delayed_run_time = TimeTicks::Now() + delay; |
| 556 | 554 |
| 557 #if defined(OS_WIN) | 555 #if defined(OS_WIN) |
| 558 if (high_resolution_timer_expiration_.is_null()) { | 556 if (high_resolution_timer_expiration_.is_null()) { |
| 559 // Windows timers are granular to 15.6ms. If we only set high-res | 557 // Windows timers are granular to 15.6ms. If we only set high-res |
| 560 // timers for those under 15.6ms, then a 18ms timer ticks at ~32ms, | 558 // timers for those under 15.6ms, then a 18ms timer ticks at ~32ms, |
| 561 // which as a percentage is pretty inaccurate. So enable high | 559 // which as a percentage is pretty inaccurate. So enable high |
| 562 // res timers for any timer which is within 2x of the granularity. | 560 // res timers for any timer which is within 2x of the granularity. |
| 563 // This is a tradeoff between accuracy and power management. | 561 // This is a tradeoff between accuracy and power management. |
| 564 bool needs_high_res_timers = delay.InMilliseconds() < | 562 bool needs_high_res_timers = delay.InMilliseconds() < |
| 565 (2 * base::Time::kMinLowResolutionThresholdMs); | 563 (2 * Time::kMinLowResolutionThresholdMs); |
| 566 if (needs_high_res_timers) { | 564 if (needs_high_res_timers) { |
| 567 if (base::Time::ActivateHighResolutionTimer(true)) { | 565 if (Time::ActivateHighResolutionTimer(true)) { |
| 568 high_resolution_timer_expiration_ = TimeTicks::Now() + | 566 high_resolution_timer_expiration_ = TimeTicks::Now() + |
| 569 TimeDelta::FromMilliseconds(kHighResolutionTimerModeLeaseTimeMs); | 567 TimeDelta::FromMilliseconds(kHighResolutionTimerModeLeaseTimeMs); |
| 570 } | 568 } |
| 571 } | 569 } |
| 572 } | 570 } |
| 573 #endif | 571 #endif |
| 574 } else { | 572 } else { |
| 575 DCHECK_EQ(delay.InMilliseconds(), 0) << "delay should not be negative"; | 573 DCHECK_EQ(delay.InMilliseconds(), 0) << "delay should not be negative"; |
| 576 } | 574 } |
| 577 | 575 |
| 578 #if defined(OS_WIN) | 576 #if defined(OS_WIN) |
| 579 if (!high_resolution_timer_expiration_.is_null()) { | 577 if (!high_resolution_timer_expiration_.is_null()) { |
| 580 if (TimeTicks::Now() > high_resolution_timer_expiration_) { | 578 if (TimeTicks::Now() > high_resolution_timer_expiration_) { |
| 581 base::Time::ActivateHighResolutionTimer(false); | 579 Time::ActivateHighResolutionTimer(false); |
| 582 high_resolution_timer_expiration_ = TimeTicks(); | 580 high_resolution_timer_expiration_ = TimeTicks(); |
| 583 } | 581 } |
| 584 } | 582 } |
| 585 #endif | 583 #endif |
| 586 | 584 |
| 587 return delayed_run_time; | 585 return delayed_run_time; |
| 588 } | 586 } |
| 589 | 587 |
| 590 // Possibly called on a background thread! | 588 // Possibly called on a background thread! |
| 591 void MessageLoop::AddToIncomingQueue(PendingTask* pending_task) { | 589 void MessageLoop::AddToIncomingQueue(PendingTask* pending_task) { |
| 592 // Warning: Don't try to short-circuit, and handle this thread's tasks more | 590 // Warning: Don't try to short-circuit, and handle this thread's tasks more |
| 593 // directly, as it could starve handling of foreign threads. Put every task | 591 // directly, as it could starve handling of foreign threads. Put every task |
| 594 // into this queue. | 592 // into this queue. |
| 595 | 593 |
| 596 scoped_refptr<base::MessagePump> pump; | 594 scoped_refptr<MessagePump> pump; |
| 597 { | 595 { |
| 598 base::AutoLock locked(incoming_queue_lock_); | 596 AutoLock locked(incoming_queue_lock_); |
| 599 | 597 |
| 600 // Initialize the sequence number. The sequence number is used for delayed | 598 // Initialize the sequence number. The sequence number is used for delayed |
| 601 // tasks (to faciliate FIFO sorting when two tasks have the same | 599 // tasks (to faciliate FIFO sorting when two tasks have the same |
| 602 // delayed_run_time value) and for identifying the task in about:tracing. | 600 // delayed_run_time value) and for identifying the task in about:tracing. |
| 603 pending_task->sequence_num = next_sequence_num_++; | 601 pending_task->sequence_num = next_sequence_num_++; |
| 604 | 602 |
| 605 TRACE_EVENT_FLOW_BEGIN0("task", "MessageLoop::PostTask", | 603 TRACE_EVENT_FLOW_BEGIN0("task", "MessageLoop::PostTask", |
| 606 TRACE_ID_MANGLE(GetTaskTraceID(*pending_task, this))); | 604 TRACE_ID_MANGLE(GetTaskTraceID(*pending_task, this))); |
| 607 | 605 |
| 608 bool was_empty = incoming_queue_.empty(); | 606 bool was_empty = incoming_queue_.empty(); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 621 pump->ScheduleWork(); | 619 pump->ScheduleWork(); |
| 622 } | 620 } |
| 623 | 621 |
| 624 //------------------------------------------------------------------------------ | 622 //------------------------------------------------------------------------------ |
| 625 // Method and data for histogramming events and actions taken by each instance | 623 // Method and data for histogramming events and actions taken by each instance |
| 626 // on each thread. | 624 // on each thread. |
| 627 | 625 |
| 628 void MessageLoop::StartHistogrammer() { | 626 void MessageLoop::StartHistogrammer() { |
| 629 #if !defined(OS_NACL) // NaCl build has no metrics code. | 627 #if !defined(OS_NACL) // NaCl build has no metrics code. |
| 630 if (enable_histogrammer_ && !message_histogram_ | 628 if (enable_histogrammer_ && !message_histogram_ |
| 631 && base::StatisticsRecorder::IsActive()) { | 629 && StatisticsRecorder::IsActive()) { |
| 632 DCHECK(!thread_name_.empty()); | 630 DCHECK(!thread_name_.empty()); |
| 633 message_histogram_ = base::LinearHistogram::FactoryGetWithRangeDescription( | 631 message_histogram_ = LinearHistogram::FactoryGetWithRangeDescription( |
| 634 "MsgLoop:" + thread_name_, | 632 "MsgLoop:" + thread_name_, |
| 635 kLeastNonZeroMessageId, kMaxMessageId, | 633 kLeastNonZeroMessageId, kMaxMessageId, |
| 636 kNumberOfDistinctMessagesDisplayed, | 634 kNumberOfDistinctMessagesDisplayed, |
| 637 message_histogram_->kHexRangePrintingFlag, | 635 message_histogram_->kHexRangePrintingFlag, |
| 638 event_descriptions_); | 636 event_descriptions_); |
| 639 } | 637 } |
| 640 #endif | 638 #endif |
| 641 } | 639 } |
| 642 | 640 |
| 643 void MessageLoop::HistogramEvent(int event) { | 641 void MessageLoop::HistogramEvent(int event) { |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 715 | 713 |
| 716 if (run_loop_->quit_when_idle_received_) | 714 if (run_loop_->quit_when_idle_received_) |
| 717 pump_->Quit(); | 715 pump_->Quit(); |
| 718 | 716 |
| 719 return false; | 717 return false; |
| 720 } | 718 } |
| 721 | 719 |
| 722 void MessageLoop::DeleteSoonInternal(const tracked_objects::Location& from_here, | 720 void MessageLoop::DeleteSoonInternal(const tracked_objects::Location& from_here, |
| 723 void(*deleter)(const void*), | 721 void(*deleter)(const void*), |
| 724 const void* object) { | 722 const void* object) { |
| 725 PostNonNestableTask(from_here, base::Bind(deleter, object)); | 723 PostNonNestableTask(from_here, Bind(deleter, object)); |
| 726 } | 724 } |
| 727 | 725 |
| 728 void MessageLoop::ReleaseSoonInternal( | 726 void MessageLoop::ReleaseSoonInternal( |
| 729 const tracked_objects::Location& from_here, | 727 const tracked_objects::Location& from_here, |
| 730 void(*releaser)(const void*), | 728 void(*releaser)(const void*), |
| 731 const void* object) { | 729 const void* object) { |
| 732 PostNonNestableTask(from_here, base::Bind(releaser, object)); | 730 PostNonNestableTask(from_here, Bind(releaser, object)); |
| 733 } | 731 } |
| 734 | 732 |
| 735 //------------------------------------------------------------------------------ | 733 //------------------------------------------------------------------------------ |
| 736 // MessageLoopForUI | 734 // MessageLoopForUI |
| 737 | 735 |
| 738 #if defined(OS_WIN) | 736 #if defined(OS_WIN) |
| 739 void MessageLoopForUI::DidProcessMessage(const MSG& message) { | 737 void MessageLoopForUI::DidProcessMessage(const MSG& message) { |
| 740 pump_win()->DidProcessMessage(message); | 738 pump_win()->DidProcessMessage(message); |
| 741 } | 739 } |
| 742 #endif // defined(OS_WIN) | 740 #endif // defined(OS_WIN) |
| 743 | 741 |
| 744 #if defined(OS_ANDROID) | 742 #if defined(OS_ANDROID) |
| 745 void MessageLoopForUI::Start() { | 743 void MessageLoopForUI::Start() { |
| 746 // No Histogram support for UI message loop as it is managed by Java side | 744 // No Histogram support for UI message loop as it is managed by Java side |
| 747 static_cast<base::MessagePumpForUI*>(pump_.get())->Start(this); | 745 static_cast<MessagePumpForUI*>(pump_.get())->Start(this); |
| 748 } | 746 } |
| 749 #endif | 747 #endif |
| 750 | 748 |
| 751 #if defined(OS_IOS) | 749 #if defined(OS_IOS) |
| 752 void MessageLoopForUI::Attach() { | 750 void MessageLoopForUI::Attach() { |
| 753 static_cast<base::MessagePumpUIApplication*>(pump_.get())->Attach(this); | 751 static_cast<MessagePumpUIApplication*>(pump_.get())->Attach(this); |
| 754 } | 752 } |
| 755 #endif | 753 #endif |
| 756 | 754 |
| 757 #if !defined(OS_MACOSX) && !defined(OS_NACL) && !defined(OS_ANDROID) | 755 #if !defined(OS_MACOSX) && !defined(OS_NACL) && !defined(OS_ANDROID) |
| 758 void MessageLoopForUI::AddObserver(Observer* observer) { | 756 void MessageLoopForUI::AddObserver(Observer* observer) { |
| 759 pump_ui()->AddObserver(observer); | 757 pump_ui()->AddObserver(observer); |
| 760 } | 758 } |
| 761 | 759 |
| 762 void MessageLoopForUI::RemoveObserver(Observer* observer) { | 760 void MessageLoopForUI::RemoveObserver(Observer* observer) { |
| 763 pump_ui()->RemoveObserver(observer); | 761 pump_ui()->RemoveObserver(observer); |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 806 Watcher *delegate) { | 804 Watcher *delegate) { |
| 807 return pump_libevent()->WatchFileDescriptor( | 805 return pump_libevent()->WatchFileDescriptor( |
| 808 fd, | 806 fd, |
| 809 persistent, | 807 persistent, |
| 810 mode, | 808 mode, |
| 811 controller, | 809 controller, |
| 812 delegate); | 810 delegate); |
| 813 } | 811 } |
| 814 | 812 |
| 815 #endif | 813 #endif |
| 814 |
| 815 } // namespace base |
| OLD | NEW |