| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
| 10 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 106 } | 106 } |
| 107 | 107 |
| 108 MessageLoop::TaskObserver::~TaskObserver() { | 108 MessageLoop::TaskObserver::~TaskObserver() { |
| 109 } | 109 } |
| 110 | 110 |
| 111 MessageLoop::DestructionObserver::~DestructionObserver() { | 111 MessageLoop::DestructionObserver::~DestructionObserver() { |
| 112 } | 112 } |
| 113 | 113 |
| 114 //------------------------------------------------------------------------------ | 114 //------------------------------------------------------------------------------ |
| 115 | 115 |
| 116 // static | |
| 117 MessageLoop* MessageLoop::current() { | |
| 118 // TODO(darin): sadly, we cannot enable this yet since people call us even | |
| 119 // when they have no intention of using us. | |
| 120 // DCHECK(loop) << "Ouch, did you forget to initialize me?"; | |
| 121 return lazy_tls_ptr.Pointer()->Get(); | |
| 122 } | |
| 123 | |
| 124 MessageLoop::MessageLoop(Type type) | 116 MessageLoop::MessageLoop(Type type) |
| 125 : type_(type), | 117 : type_(type), |
| 126 nestable_tasks_allowed_(true), | 118 nestable_tasks_allowed_(true), |
| 127 exception_restoration_(false), | 119 exception_restoration_(false), |
| 128 state_(NULL), | 120 state_(NULL), |
| 129 next_sequence_num_(0) { | 121 next_sequence_num_(0) { |
| 130 DCHECK(!current()) << "should only have one message loop per thread"; | 122 DCHECK(!current()) << "should only have one message loop per thread"; |
| 131 lazy_tls_ptr.Pointer()->Set(this); | 123 lazy_tls_ptr.Pointer()->Set(this); |
| 132 | 124 |
| 133 // TODO(rvargas): Get rid of the OS guards. | 125 // TODO(rvargas): Get rid of the OS guards. |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 185 DCHECK(!did_work); | 177 DCHECK(!did_work); |
| 186 | 178 |
| 187 // Let interested parties have one last shot at accessing this. | 179 // Let interested parties have one last shot at accessing this. |
| 188 FOR_EACH_OBSERVER(DestructionObserver, destruction_observers_, | 180 FOR_EACH_OBSERVER(DestructionObserver, destruction_observers_, |
| 189 WillDestroyCurrentMessageLoop()); | 181 WillDestroyCurrentMessageLoop()); |
| 190 | 182 |
| 191 // OK, now make it so that no one can find us. | 183 // OK, now make it so that no one can find us. |
| 192 lazy_tls_ptr.Pointer()->Set(NULL); | 184 lazy_tls_ptr.Pointer()->Set(NULL); |
| 193 } | 185 } |
| 194 | 186 |
| 187 // static |
| 188 MessageLoop* MessageLoop::current() { |
| 189 // TODO(darin): sadly, we cannot enable this yet since people call us even |
| 190 // when they have no intention of using us. |
| 191 // DCHECK(loop) << "Ouch, did you forget to initialize me?"; |
| 192 return lazy_tls_ptr.Pointer()->Get(); |
| 193 } |
| 194 |
| 195 // static |
| 196 void MessageLoop::EnableHistogrammer(bool enable) { |
| 197 enable_histogrammer_ = enable; |
| 198 } |
| 199 |
| 195 void MessageLoop::AddDestructionObserver( | 200 void MessageLoop::AddDestructionObserver( |
| 196 DestructionObserver* destruction_observer) { | 201 DestructionObserver* destruction_observer) { |
| 197 DCHECK_EQ(this, current()); | 202 DCHECK_EQ(this, current()); |
| 198 destruction_observers_.AddObserver(destruction_observer); | 203 destruction_observers_.AddObserver(destruction_observer); |
| 199 } | 204 } |
| 200 | 205 |
| 201 void MessageLoop::RemoveDestructionObserver( | 206 void MessageLoop::RemoveDestructionObserver( |
| 202 DestructionObserver* destruction_observer) { | 207 DestructionObserver* destruction_observer) { |
| 203 DCHECK_EQ(this, current()); | 208 DCHECK_EQ(this, current()); |
| 204 destruction_observers_.RemoveObserver(destruction_observer); | 209 destruction_observers_.RemoveObserver(destruction_observer); |
| 205 } | 210 } |
| 206 | 211 |
| 207 void MessageLoop::AddTaskObserver(TaskObserver* task_observer) { | 212 void MessageLoop::PostTask( |
| 208 DCHECK_EQ(this, current()); | 213 const tracked_objects::Location& from_here, Task* task) { |
| 209 task_observers_.AddObserver(task_observer); | 214 PostTask_Helper(from_here, task, 0, true); |
| 210 } | 215 } |
| 211 | 216 |
| 212 void MessageLoop::RemoveTaskObserver(TaskObserver* task_observer) { | 217 void MessageLoop::PostDelayedTask( |
| 213 DCHECK_EQ(this, current()); | 218 const tracked_objects::Location& from_here, Task* task, int64 delay_ms) { |
| 214 task_observers_.RemoveObserver(task_observer); | 219 PostTask_Helper(from_here, task, delay_ms, true); |
| 220 } |
| 221 |
| 222 void MessageLoop::PostNonNestableTask( |
| 223 const tracked_objects::Location& from_here, Task* task) { |
| 224 PostTask_Helper(from_here, task, 0, false); |
| 225 } |
| 226 |
| 227 void MessageLoop::PostNonNestableDelayedTask( |
| 228 const tracked_objects::Location& from_here, Task* task, int64 delay_ms) { |
| 229 PostTask_Helper(from_here, task, delay_ms, false); |
| 215 } | 230 } |
| 216 | 231 |
| 217 void MessageLoop::Run() { | 232 void MessageLoop::Run() { |
| 218 AutoRunState save_state(this); | 233 AutoRunState save_state(this); |
| 219 RunHandler(); | 234 RunHandler(); |
| 220 } | 235 } |
| 221 | 236 |
| 222 void MessageLoop::RunAllPending() { | 237 void MessageLoop::RunAllPending() { |
| 223 AutoRunState save_state(this); | 238 AutoRunState save_state(this); |
| 224 state_->quit_received = true; // Means run until we would otherwise block. | 239 state_->quit_received = true; // Means run until we would otherwise block. |
| 225 RunHandler(); | 240 RunHandler(); |
| 226 } | 241 } |
| 227 | 242 |
| 243 void MessageLoop::Quit() { |
| 244 DCHECK_EQ(this, current()); |
| 245 if (state_) { |
| 246 state_->quit_received = true; |
| 247 } else { |
| 248 NOTREACHED() << "Must be inside Run to call Quit"; |
| 249 } |
| 250 } |
| 251 |
| 252 void MessageLoop::QuitNow() { |
| 253 DCHECK_EQ(this, current()); |
| 254 if (state_) { |
| 255 pump_->Quit(); |
| 256 } else { |
| 257 NOTREACHED() << "Must be inside Run to call Quit"; |
| 258 } |
| 259 } |
| 260 |
| 261 void MessageLoop::SetNestableTasksAllowed(bool allowed) { |
| 262 if (nestable_tasks_allowed_ != allowed) { |
| 263 nestable_tasks_allowed_ = allowed; |
| 264 if (!nestable_tasks_allowed_) |
| 265 return; |
| 266 // Start the native pump if we are not already pumping. |
| 267 pump_->ScheduleWork(); |
| 268 } |
| 269 } |
| 270 |
| 271 bool MessageLoop::NestableTasksAllowed() const { |
| 272 return nestable_tasks_allowed_; |
| 273 } |
| 274 |
| 275 bool MessageLoop::IsNested() { |
| 276 return state_->run_depth > 1; |
| 277 } |
| 278 |
| 279 void MessageLoop::AddTaskObserver(TaskObserver* task_observer) { |
| 280 DCHECK_EQ(this, current()); |
| 281 task_observers_.AddObserver(task_observer); |
| 282 } |
| 283 |
| 284 void MessageLoop::RemoveTaskObserver(TaskObserver* task_observer) { |
| 285 DCHECK_EQ(this, current()); |
| 286 task_observers_.RemoveObserver(task_observer); |
| 287 } |
| 288 |
| 289 //------------------------------------------------------------------------------ |
| 290 |
| 228 // Runs the loop in two different SEH modes: | 291 // Runs the loop in two different SEH modes: |
| 229 // enable_SEH_restoration_ = false : any unhandled exception goes to the last | 292 // enable_SEH_restoration_ = false : any unhandled exception goes to the last |
| 230 // one that calls SetUnhandledExceptionFilter(). | 293 // one that calls SetUnhandledExceptionFilter(). |
| 231 // enable_SEH_restoration_ = true : any unhandled exception goes to the filter | 294 // enable_SEH_restoration_ = true : any unhandled exception goes to the filter |
| 232 // that was existed before the loop was run. | 295 // that was existed before the loop was run. |
| 233 void MessageLoop::RunHandler() { | 296 void MessageLoop::RunHandler() { |
| 234 #if defined(OS_WIN) | 297 #if defined(OS_WIN) |
| 235 if (exception_restoration_) { | 298 if (exception_restoration_) { |
| 236 RunInternalInSEHFrame(); | 299 RunInternalInSEHFrame(); |
| 237 return; | 300 return; |
| 238 } | 301 } |
| 239 #endif | 302 #endif |
| 240 | 303 |
| 241 RunInternal(); | 304 RunInternal(); |
| 242 } | 305 } |
| 243 //------------------------------------------------------------------------------ | 306 |
| 244 #if defined(OS_WIN) | 307 #if defined(OS_WIN) |
| 245 __declspec(noinline) void MessageLoop::RunInternalInSEHFrame() { | 308 __declspec(noinline) void MessageLoop::RunInternalInSEHFrame() { |
| 246 LPTOP_LEVEL_EXCEPTION_FILTER current_filter = GetTopSEHFilter(); | 309 LPTOP_LEVEL_EXCEPTION_FILTER current_filter = GetTopSEHFilter(); |
| 247 __try { | 310 __try { |
| 248 RunInternal(); | 311 RunInternal(); |
| 249 } __except(SEHFilter(current_filter)) { | 312 } __except(SEHFilter(current_filter)) { |
| 250 } | 313 } |
| 251 return; | 314 return; |
| 252 } | 315 } |
| 253 #endif | 316 #endif |
| 254 //------------------------------------------------------------------------------ | |
| 255 | 317 |
| 256 void MessageLoop::RunInternal() { | 318 void MessageLoop::RunInternal() { |
| 257 DCHECK_EQ(this, current()); | 319 DCHECK_EQ(this, current()); |
| 258 | 320 |
| 259 StartHistogrammer(); | 321 StartHistogrammer(); |
| 260 | 322 |
| 261 #if !defined(OS_MACOSX) | 323 #if !defined(OS_MACOSX) |
| 262 if (state_->dispatcher && type() == TYPE_UI) { | 324 if (state_->dispatcher && type() == TYPE_UI) { |
| 263 static_cast<base::MessagePumpForUI*>(pump_.get())-> | 325 static_cast<base::MessagePumpForUI*>(pump_.get())-> |
| 264 RunWithDispatcher(this, state_->dispatcher); | 326 RunWithDispatcher(this, state_->dispatcher); |
| 265 return; | 327 return; |
| 266 } | 328 } |
| 267 #endif | 329 #endif |
| 268 | 330 |
| 269 pump_->Run(this); | 331 pump_->Run(this); |
| 270 } | 332 } |
| 271 | 333 |
| 272 //------------------------------------------------------------------------------ | |
| 273 // Wrapper functions for use in above message loop framework. | |
| 274 | |
| 275 bool MessageLoop::ProcessNextDelayedNonNestableTask() { | 334 bool MessageLoop::ProcessNextDelayedNonNestableTask() { |
| 276 if (state_->run_depth != 1) | 335 if (state_->run_depth != 1) |
| 277 return false; | 336 return false; |
| 278 | 337 |
| 279 if (deferred_non_nestable_work_queue_.empty()) | 338 if (deferred_non_nestable_work_queue_.empty()) |
| 280 return false; | 339 return false; |
| 281 | 340 |
| 282 Task* task = deferred_non_nestable_work_queue_.front().task; | 341 Task* task = deferred_non_nestable_work_queue_.front().task; |
| 283 deferred_non_nestable_work_queue_.pop(); | 342 deferred_non_nestable_work_queue_.pop(); |
| 284 | 343 |
| 285 RunTask(task); | 344 RunTask(task); |
| 286 return true; | 345 return true; |
| 287 } | 346 } |
| 288 | 347 |
| 289 //------------------------------------------------------------------------------ | |
| 290 | |
| 291 void MessageLoop::Quit() { | |
| 292 DCHECK_EQ(this, current()); | |
| 293 if (state_) { | |
| 294 state_->quit_received = true; | |
| 295 } else { | |
| 296 NOTREACHED() << "Must be inside Run to call Quit"; | |
| 297 } | |
| 298 } | |
| 299 | |
| 300 void MessageLoop::QuitNow() { | |
| 301 DCHECK_EQ(this, current()); | |
| 302 if (state_) { | |
| 303 pump_->Quit(); | |
| 304 } else { | |
| 305 NOTREACHED() << "Must be inside Run to call Quit"; | |
| 306 } | |
| 307 } | |
| 308 | |
| 309 void MessageLoop::PostTask( | |
| 310 const tracked_objects::Location& from_here, Task* task) { | |
| 311 PostTask_Helper(from_here, task, 0, true); | |
| 312 } | |
| 313 | |
| 314 void MessageLoop::PostDelayedTask( | |
| 315 const tracked_objects::Location& from_here, Task* task, int64 delay_ms) { | |
| 316 PostTask_Helper(from_here, task, delay_ms, true); | |
| 317 } | |
| 318 | |
| 319 void MessageLoop::PostNonNestableTask( | |
| 320 const tracked_objects::Location& from_here, Task* task) { | |
| 321 PostTask_Helper(from_here, task, 0, false); | |
| 322 } | |
| 323 | |
| 324 void MessageLoop::PostNonNestableDelayedTask( | |
| 325 const tracked_objects::Location& from_here, Task* task, int64 delay_ms) { | |
| 326 PostTask_Helper(from_here, task, delay_ms, false); | |
| 327 } | |
| 328 | |
| 329 // Possibly called on a background thread! | |
| 330 void MessageLoop::PostTask_Helper( | |
| 331 const tracked_objects::Location& from_here, Task* task, int64 delay_ms, | |
| 332 bool nestable) { | |
| 333 task->SetBirthPlace(from_here); | |
| 334 | |
| 335 PendingTask pending_task(task, nestable); | |
| 336 | |
| 337 if (delay_ms > 0) { | |
| 338 pending_task.delayed_run_time = | |
| 339 TimeTicks::Now() + TimeDelta::FromMilliseconds(delay_ms); | |
| 340 | |
| 341 #if defined(OS_WIN) | |
| 342 if (high_resolution_timer_expiration_.is_null()) { | |
| 343 // Windows timers are granular to 15.6ms. If we only set high-res | |
| 344 // timers for those under 15.6ms, then a 18ms timer ticks at ~32ms, | |
| 345 // which as a percentage is pretty inaccurate. So enable high | |
| 346 // res timers for any timer which is within 2x of the granularity. | |
| 347 // This is a tradeoff between accuracy and power management. | |
| 348 bool needs_high_res_timers = | |
| 349 delay_ms < (2 * base::Time::kMinLowResolutionThresholdMs); | |
| 350 if (needs_high_res_timers) { | |
| 351 base::Time::ActivateHighResolutionTimer(true); | |
| 352 high_resolution_timer_expiration_ = TimeTicks::Now() + | |
| 353 TimeDelta::FromMilliseconds(kHighResolutionTimerModeLeaseTimeMs); | |
| 354 } | |
| 355 } | |
| 356 #endif | |
| 357 } else { | |
| 358 DCHECK_EQ(delay_ms, 0) << "delay should not be negative"; | |
| 359 } | |
| 360 | |
| 361 #if defined(OS_WIN) | |
| 362 if (!high_resolution_timer_expiration_.is_null()) { | |
| 363 if (TimeTicks::Now() > high_resolution_timer_expiration_) { | |
| 364 base::Time::ActivateHighResolutionTimer(false); | |
| 365 high_resolution_timer_expiration_ = TimeTicks(); | |
| 366 } | |
| 367 } | |
| 368 #endif | |
| 369 | |
| 370 // Warning: Don't try to short-circuit, and handle this thread's tasks more | |
| 371 // directly, as it could starve handling of foreign threads. Put every task | |
| 372 // into this queue. | |
| 373 | |
| 374 scoped_refptr<base::MessagePump> pump; | |
| 375 { | |
| 376 AutoLock locked(incoming_queue_lock_); | |
| 377 | |
| 378 bool was_empty = incoming_queue_.empty(); | |
| 379 incoming_queue_.push(pending_task); | |
| 380 if (!was_empty) | |
| 381 return; // Someone else should have started the sub-pump. | |
| 382 | |
| 383 pump = pump_; | |
| 384 } | |
| 385 // Since the incoming_queue_ may contain a task that destroys this message | |
| 386 // loop, we cannot exit incoming_queue_lock_ until we are done with |this|. | |
| 387 // We use a stack-based reference to the message pump so that we can call | |
| 388 // ScheduleWork outside of incoming_queue_lock_. | |
| 389 | |
| 390 pump->ScheduleWork(); | |
| 391 } | |
| 392 | |
| 393 void MessageLoop::SetNestableTasksAllowed(bool allowed) { | |
| 394 if (nestable_tasks_allowed_ != allowed) { | |
| 395 nestable_tasks_allowed_ = allowed; | |
| 396 if (!nestable_tasks_allowed_) | |
| 397 return; | |
| 398 // Start the native pump if we are not already pumping. | |
| 399 pump_->ScheduleWork(); | |
| 400 } | |
| 401 } | |
| 402 | |
| 403 bool MessageLoop::NestableTasksAllowed() const { | |
| 404 return nestable_tasks_allowed_; | |
| 405 } | |
| 406 | |
| 407 bool MessageLoop::IsNested() { | |
| 408 return state_->run_depth > 1; | |
| 409 } | |
| 410 | |
| 411 //------------------------------------------------------------------------------ | |
| 412 | |
| 413 void MessageLoop::RunTask(Task* task) { | 348 void MessageLoop::RunTask(Task* task) { |
| 414 DCHECK(nestable_tasks_allowed_); | 349 DCHECK(nestable_tasks_allowed_); |
| 415 // Execute the task and assume the worst: It is probably not reentrant. | 350 // Execute the task and assume the worst: It is probably not reentrant. |
| 416 nestable_tasks_allowed_ = false; | 351 nestable_tasks_allowed_ = false; |
| 417 | 352 |
| 418 HistogramEvent(kTaskRunEvent); | 353 HistogramEvent(kTaskRunEvent); |
| 419 FOR_EACH_OBSERVER(TaskObserver, task_observers_, | 354 FOR_EACH_OBSERVER(TaskObserver, task_observers_, |
| 420 WillProcessTask(task)); | 355 WillProcessTask(task)); |
| 421 task->Run(); | 356 task->Run(); |
| 422 FOR_EACH_OBSERVER(TaskObserver, task_observers_, DidProcessTask(task)); | 357 FOR_EACH_OBSERVER(TaskObserver, task_observers_, DidProcessTask(task)); |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 506 } | 441 } |
| 507 did_work |= !delayed_work_queue_.empty(); | 442 did_work |= !delayed_work_queue_.empty(); |
| 508 while (!delayed_work_queue_.empty()) { | 443 while (!delayed_work_queue_.empty()) { |
| 509 Task* task = delayed_work_queue_.top().task; | 444 Task* task = delayed_work_queue_.top().task; |
| 510 delayed_work_queue_.pop(); | 445 delayed_work_queue_.pop(); |
| 511 delete task; | 446 delete task; |
| 512 } | 447 } |
| 513 return did_work; | 448 return did_work; |
| 514 } | 449 } |
| 515 | 450 |
| 451 // Possibly called on a background thread! |
| 452 void MessageLoop::PostTask_Helper( |
| 453 const tracked_objects::Location& from_here, Task* task, int64 delay_ms, |
| 454 bool nestable) { |
| 455 task->SetBirthPlace(from_here); |
| 456 |
| 457 PendingTask pending_task(task, nestable); |
| 458 |
| 459 if (delay_ms > 0) { |
| 460 pending_task.delayed_run_time = |
| 461 TimeTicks::Now() + TimeDelta::FromMilliseconds(delay_ms); |
| 462 |
| 463 #if defined(OS_WIN) |
| 464 if (high_resolution_timer_expiration_.is_null()) { |
| 465 // Windows timers are granular to 15.6ms. If we only set high-res |
| 466 // timers for those under 15.6ms, then a 18ms timer ticks at ~32ms, |
| 467 // which as a percentage is pretty inaccurate. So enable high |
| 468 // res timers for any timer which is within 2x of the granularity. |
| 469 // This is a tradeoff between accuracy and power management. |
| 470 bool needs_high_res_timers = |
| 471 delay_ms < (2 * base::Time::kMinLowResolutionThresholdMs); |
| 472 if (needs_high_res_timers) { |
| 473 base::Time::ActivateHighResolutionTimer(true); |
| 474 high_resolution_timer_expiration_ = TimeTicks::Now() + |
| 475 TimeDelta::FromMilliseconds(kHighResolutionTimerModeLeaseTimeMs); |
| 476 } |
| 477 } |
| 478 #endif |
| 479 } else { |
| 480 DCHECK_EQ(delay_ms, 0) << "delay should not be negative"; |
| 481 } |
| 482 |
| 483 #if defined(OS_WIN) |
| 484 if (!high_resolution_timer_expiration_.is_null()) { |
| 485 if (TimeTicks::Now() > high_resolution_timer_expiration_) { |
| 486 base::Time::ActivateHighResolutionTimer(false); |
| 487 high_resolution_timer_expiration_ = TimeTicks(); |
| 488 } |
| 489 } |
| 490 #endif |
| 491 |
| 492 // Warning: Don't try to short-circuit, and handle this thread's tasks more |
| 493 // directly, as it could starve handling of foreign threads. Put every task |
| 494 // into this queue. |
| 495 |
| 496 scoped_refptr<base::MessagePump> pump; |
| 497 { |
| 498 AutoLock locked(incoming_queue_lock_); |
| 499 |
| 500 bool was_empty = incoming_queue_.empty(); |
| 501 incoming_queue_.push(pending_task); |
| 502 if (!was_empty) |
| 503 return; // Someone else should have started the sub-pump. |
| 504 |
| 505 pump = pump_; |
| 506 } |
| 507 // Since the incoming_queue_ may contain a task that destroys this message |
| 508 // loop, we cannot exit incoming_queue_lock_ until we are done with |this|. |
| 509 // We use a stack-based reference to the message pump so that we can call |
| 510 // ScheduleWork outside of incoming_queue_lock_. |
| 511 |
| 512 pump->ScheduleWork(); |
| 513 } |
| 514 |
| 515 //------------------------------------------------------------------------------ |
| 516 // Method and data for histogramming events and actions taken by each instance |
| 517 // on each thread. |
| 518 |
| 519 void MessageLoop::StartHistogrammer() { |
| 520 if (enable_histogrammer_ && !message_histogram_.get() |
| 521 && base::StatisticsRecorder::IsActive()) { |
| 522 DCHECK(!thread_name_.empty()); |
| 523 message_histogram_ = base::LinearHistogram::FactoryGet( |
| 524 "MsgLoop:" + thread_name_, |
| 525 kLeastNonZeroMessageId, kMaxMessageId, |
| 526 kNumberOfDistinctMessagesDisplayed, |
| 527 message_histogram_->kHexRangePrintingFlag); |
| 528 message_histogram_->SetRangeDescriptions(event_descriptions_); |
| 529 } |
| 530 } |
| 531 |
| 532 void MessageLoop::HistogramEvent(int event) { |
| 533 if (message_histogram_.get()) |
| 534 message_histogram_->Add(event); |
| 535 } |
| 536 |
| 516 bool MessageLoop::DoWork() { | 537 bool MessageLoop::DoWork() { |
| 517 if (!nestable_tasks_allowed_) { | 538 if (!nestable_tasks_allowed_) { |
| 518 // Task can't be executed right now. | 539 // Task can't be executed right now. |
| 519 return false; | 540 return false; |
| 520 } | 541 } |
| 521 | 542 |
| 522 for (;;) { | 543 for (;;) { |
| 523 ReloadWorkQueue(); | 544 ReloadWorkQueue(); |
| 524 if (work_queue_.empty()) | 545 if (work_queue_.empty()) |
| 525 break; | 546 break; |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 622 | 643 |
| 623 if (delayed_run_time > other.delayed_run_time) | 644 if (delayed_run_time > other.delayed_run_time) |
| 624 return true; | 645 return true; |
| 625 | 646 |
| 626 // If the times happen to match, then we use the sequence number to decide. | 647 // If the times happen to match, then we use the sequence number to decide. |
| 627 // Compare the difference to support integer roll-over. | 648 // Compare the difference to support integer roll-over. |
| 628 return (sequence_num - other.sequence_num) > 0; | 649 return (sequence_num - other.sequence_num) > 0; |
| 629 } | 650 } |
| 630 | 651 |
| 631 //------------------------------------------------------------------------------ | 652 //------------------------------------------------------------------------------ |
| 632 // Method and data for histogramming events and actions taken by each instance | |
| 633 // on each thread. | |
| 634 | |
| 635 // static | |
| 636 void MessageLoop::EnableHistogrammer(bool enable) { | |
| 637 enable_histogrammer_ = enable; | |
| 638 } | |
| 639 | |
| 640 void MessageLoop::StartHistogrammer() { | |
| 641 if (enable_histogrammer_ && !message_histogram_.get() | |
| 642 && base::StatisticsRecorder::IsActive()) { | |
| 643 DCHECK(!thread_name_.empty()); | |
| 644 message_histogram_ = base::LinearHistogram::FactoryGet( | |
| 645 "MsgLoop:" + thread_name_, | |
| 646 kLeastNonZeroMessageId, kMaxMessageId, | |
| 647 kNumberOfDistinctMessagesDisplayed, | |
| 648 message_histogram_->kHexRangePrintingFlag); | |
| 649 message_histogram_->SetRangeDescriptions(event_descriptions_); | |
| 650 } | |
| 651 } | |
| 652 | |
| 653 void MessageLoop::HistogramEvent(int event) { | |
| 654 if (message_histogram_.get()) | |
| 655 message_histogram_->Add(event); | |
| 656 } | |
| 657 | |
| 658 //------------------------------------------------------------------------------ | |
| 659 // MessageLoopForUI | 653 // MessageLoopForUI |
| 660 | 654 |
| 661 #if defined(OS_WIN) | 655 #if defined(OS_WIN) |
| 662 void MessageLoopForUI::DidProcessMessage(const MSG& message) { | 656 void MessageLoopForUI::DidProcessMessage(const MSG& message) { |
| 663 pump_win()->DidProcessMessage(message); | 657 pump_win()->DidProcessMessage(message); |
| 664 } | 658 } |
| 665 #endif // defined(OS_WIN) | 659 #endif // defined(OS_WIN) |
| 666 | 660 |
| 667 #if !defined(OS_MACOSX) && !defined(OS_NACL) | 661 #if !defined(OS_MACOSX) && !defined(OS_NACL) |
| 668 void MessageLoopForUI::AddObserver(Observer* observer) { | 662 void MessageLoopForUI::AddObserver(Observer* observer) { |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 702 Watcher *delegate) { | 696 Watcher *delegate) { |
| 703 return pump_libevent()->WatchFileDescriptor( | 697 return pump_libevent()->WatchFileDescriptor( |
| 704 fd, | 698 fd, |
| 705 persistent, | 699 persistent, |
| 706 static_cast<base::MessagePumpLibevent::Mode>(mode), | 700 static_cast<base::MessagePumpLibevent::Mode>(mode), |
| 707 controller, | 701 controller, |
| 708 delegate); | 702 delegate); |
| 709 } | 703 } |
| 710 | 704 |
| 711 #endif | 705 #endif |
| OLD | NEW |