| 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 11 matching lines...) Expand all Loading... |
| 22 #endif | 22 #endif |
| 23 #if defined(OS_POSIX) && !defined(OS_MACOSX) | 23 #if defined(OS_POSIX) && !defined(OS_MACOSX) |
| 24 #include "base/message_pump_glib.h" | 24 #include "base/message_pump_glib.h" |
| 25 #endif | 25 #endif |
| 26 #if defined(TOUCH_UI) | 26 #if defined(TOUCH_UI) |
| 27 #include "base/message_pump_glib_x.h" | 27 #include "base/message_pump_glib_x.h" |
| 28 #endif | 28 #endif |
| 29 | 29 |
| 30 using base::Time; | 30 using base::Time; |
| 31 using base::TimeDelta; | 31 using base::TimeDelta; |
| 32 using base::TimeTicks; |
| 32 | 33 |
| 33 namespace { | 34 namespace { |
| 34 | 35 |
| 35 // A lazily created thread local storage for quick access to a thread's message | 36 // A lazily created thread local storage for quick access to a thread's message |
| 36 // loop, if one exists. This should be safe and free of static constructors. | 37 // loop, if one exists. This should be safe and free of static constructors. |
| 37 base::LazyInstance<base::ThreadLocalPointer<MessageLoop> > lazy_tls_ptr( | 38 base::LazyInstance<base::ThreadLocalPointer<MessageLoop> > lazy_tls_ptr( |
| 38 base::LINKER_INITIALIZED); | 39 base::LINKER_INITIALIZED); |
| 39 | 40 |
| 40 // Logical events for Histogram profiling. Run with -message-loop-histogrammer | 41 // Logical events for Histogram profiling. Run with -message-loop-histogrammer |
| 41 // to get an accounting of messages and actions taken on each thread. | 42 // to get an accounting of messages and actions taken on each thread. |
| (...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 325 // Possibly called on a background thread! | 326 // Possibly called on a background thread! |
| 326 void MessageLoop::PostTask_Helper( | 327 void MessageLoop::PostTask_Helper( |
| 327 const tracked_objects::Location& from_here, Task* task, int64 delay_ms, | 328 const tracked_objects::Location& from_here, Task* task, int64 delay_ms, |
| 328 bool nestable) { | 329 bool nestable) { |
| 329 task->SetBirthPlace(from_here); | 330 task->SetBirthPlace(from_here); |
| 330 | 331 |
| 331 PendingTask pending_task(task, nestable); | 332 PendingTask pending_task(task, nestable); |
| 332 | 333 |
| 333 if (delay_ms > 0) { | 334 if (delay_ms > 0) { |
| 334 pending_task.delayed_run_time = | 335 pending_task.delayed_run_time = |
| 335 Time::Now() + TimeDelta::FromMilliseconds(delay_ms); | 336 TimeTicks::Now() + TimeDelta::FromMilliseconds(delay_ms); |
| 336 | 337 |
| 337 #if defined(OS_WIN) | 338 #if defined(OS_WIN) |
| 338 if (high_resolution_timer_expiration_.is_null()) { | 339 if (high_resolution_timer_expiration_.is_null()) { |
| 339 // Windows timers are granular to 15.6ms. If we only set high-res | 340 // Windows timers are granular to 15.6ms. If we only set high-res |
| 340 // timers for those under 15.6ms, then a 18ms timer ticks at ~32ms, | 341 // timers for those under 15.6ms, then a 18ms timer ticks at ~32ms, |
| 341 // which as a percentage is pretty inaccurate. So enable high | 342 // which as a percentage is pretty inaccurate. So enable high |
| 342 // res timers for any timer which is within 2x of the granularity. | 343 // res timers for any timer which is within 2x of the granularity. |
| 343 // This is a tradeoff between accuracy and power management. | 344 // This is a tradeoff between accuracy and power management. |
| 344 bool needs_high_res_timers = | 345 bool needs_high_res_timers = |
| 345 delay_ms < (2 * Time::kMinLowResolutionThresholdMs); | 346 delay_ms < (2 * Time::kMinLowResolutionThresholdMs); |
| 346 if (needs_high_res_timers) { | 347 if (needs_high_res_timers) { |
| 347 Time::ActivateHighResolutionTimer(true); | 348 Time::ActivateHighResolutionTimer(true); |
| 348 high_resolution_timer_expiration_ = base::TimeTicks::Now() + | 349 high_resolution_timer_expiration_ = TimeTicks::Now() + |
| 349 TimeDelta::FromMilliseconds(kHighResolutionTimerModeLeaseTimeMs); | 350 TimeDelta::FromMilliseconds(kHighResolutionTimerModeLeaseTimeMs); |
| 350 } | 351 } |
| 351 } | 352 } |
| 352 #endif | 353 #endif |
| 353 } else { | 354 } else { |
| 354 DCHECK_EQ(delay_ms, 0) << "delay should not be negative"; | 355 DCHECK_EQ(delay_ms, 0) << "delay should not be negative"; |
| 355 } | 356 } |
| 356 | 357 |
| 357 #if defined(OS_WIN) | 358 #if defined(OS_WIN) |
| 358 if (!high_resolution_timer_expiration_.is_null()) { | 359 if (!high_resolution_timer_expiration_.is_null()) { |
| 359 if (base::TimeTicks::Now() > high_resolution_timer_expiration_) { | 360 if (TimeTicks::Now() > high_resolution_timer_expiration_) { |
| 360 Time::ActivateHighResolutionTimer(false); | 361 Time::ActivateHighResolutionTimer(false); |
| 361 high_resolution_timer_expiration_ = base::TimeTicks(); | 362 high_resolution_timer_expiration_ = TimeTicks(); |
| 362 } | 363 } |
| 363 } | 364 } |
| 364 #endif | 365 #endif |
| 365 | 366 |
| 366 // Warning: Don't try to short-circuit, and handle this thread's tasks more | 367 // Warning: Don't try to short-circuit, and handle this thread's tasks more |
| 367 // directly, as it could starve handling of foreign threads. Put every task | 368 // directly, as it could starve handling of foreign threads. Put every task |
| 368 // into this queue. | 369 // into this queue. |
| 369 | 370 |
| 370 scoped_refptr<base::MessagePump> pump; | 371 scoped_refptr<base::MessagePump> pump; |
| 371 { | 372 { |
| (...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 533 if (DeferOrRunPendingTask(pending_task)) | 534 if (DeferOrRunPendingTask(pending_task)) |
| 534 return true; | 535 return true; |
| 535 } | 536 } |
| 536 } while (!work_queue_.empty()); | 537 } while (!work_queue_.empty()); |
| 537 } | 538 } |
| 538 | 539 |
| 539 // Nothing happened. | 540 // Nothing happened. |
| 540 return false; | 541 return false; |
| 541 } | 542 } |
| 542 | 543 |
| 543 bool MessageLoop::DoDelayedWork(base::Time* next_delayed_work_time) { | 544 bool MessageLoop::DoDelayedWork(base::TimeTicks* next_delayed_work_time) { |
| 544 if (!nestable_tasks_allowed_ || delayed_work_queue_.empty()) { | 545 if (!nestable_tasks_allowed_ || delayed_work_queue_.empty()) { |
| 545 recent_time_ = *next_delayed_work_time = base::Time(); | 546 recent_time_ = *next_delayed_work_time = TimeTicks(); |
| 546 return false; | 547 return false; |
| 547 } | 548 } |
| 548 | 549 |
| 549 // When we "fall behind," there may be a lot of tasks in the delayed work | 550 // When we "fall behind," there will be a lot of tasks in the delayed work |
| 550 // queue that are ready to run. To increase efficiency when we fall behind, | 551 // queue that are ready to run. To increase efficiency when we fall behind, |
| 551 // we will only call Time::Now() intermittently, and then process all tasks | 552 // we will only call Time::Now() intermittently, and then process all tasks |
| 552 // that are ready to run before calling it again. As a result, the more we | 553 // that are ready to run before calling it again. As a result, the more we |
| 553 // fall behind (and have a lot of ready-to-run delayed tasks), the more | 554 // fall behind (and have a lot of ready-to-run delayed tasks), the more |
| 554 // efficient we'll be at handling the tasks. | 555 // efficient we'll be at handling the tasks. |
| 555 base::Time next_run_time = delayed_work_queue_.top().delayed_run_time; | 556 |
| 557 TimeTicks next_run_time = delayed_work_queue_.top().delayed_run_time; |
| 556 if (next_run_time > recent_time_) { | 558 if (next_run_time > recent_time_) { |
| 557 recent_time_ = base::Time::Now(); // Get a better view of Now(). | 559 recent_time_ = TimeTicks::Now(); // Get a better view of Now(); |
| 558 if (next_run_time > recent_time_) { | 560 if (next_run_time > recent_time_) { |
| 559 *next_delayed_work_time = next_run_time; | 561 *next_delayed_work_time = next_run_time; |
| 560 return false; | 562 return false; |
| 561 } | 563 } |
| 562 } | 564 } |
| 563 | 565 |
| 564 PendingTask pending_task = delayed_work_queue_.top(); | 566 PendingTask pending_task = delayed_work_queue_.top(); |
| 565 delayed_work_queue_.pop(); | 567 delayed_work_queue_.pop(); |
| 566 | 568 |
| 567 if (!delayed_work_queue_.empty()) | 569 if (!delayed_work_queue_.empty()) |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 697 Watcher *delegate) { | 699 Watcher *delegate) { |
| 698 return pump_libevent()->WatchFileDescriptor( | 700 return pump_libevent()->WatchFileDescriptor( |
| 699 fd, | 701 fd, |
| 700 persistent, | 702 persistent, |
| 701 static_cast<base::MessagePumpLibevent::Mode>(mode), | 703 static_cast<base::MessagePumpLibevent::Mode>(mode), |
| 702 controller, | 704 controller, |
| 703 delegate); | 705 delegate); |
| 704 } | 706 } |
| 705 | 707 |
| 706 #endif | 708 #endif |
| OLD | NEW |