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 240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
251 | 251 |
252 void MessageLoop::RemoveDestructionObserver( | 252 void MessageLoop::RemoveDestructionObserver( |
253 DestructionObserver* destruction_observer) { | 253 DestructionObserver* destruction_observer) { |
254 DCHECK_EQ(this, current()); | 254 DCHECK_EQ(this, current()); |
255 destruction_observers_.RemoveObserver(destruction_observer); | 255 destruction_observers_.RemoveObserver(destruction_observer); |
256 } | 256 } |
257 | 257 |
258 void MessageLoop::PostTask( | 258 void MessageLoop::PostTask( |
259 const tracked_objects::Location& from_here, const base::Closure& task) { | 259 const tracked_objects::Location& from_here, const base::Closure& task) { |
260 DCHECK(!task.is_null()) << from_here.ToString(); | 260 DCHECK(!task.is_null()) << from_here.ToString(); |
261 PendingTask pending_task( | 261 PendingTask pending_task(from_here, task, CalculateDelayedRuntime(0), true); |
262 from_here, task, CalculateDelayedRuntime(TimeDelta()), true); | |
263 AddToIncomingQueue(&pending_task); | 262 AddToIncomingQueue(&pending_task); |
264 } | 263 } |
265 | 264 |
266 void MessageLoop::PostDelayedTask( | 265 void MessageLoop::PostDelayedTask( |
267 const tracked_objects::Location& from_here, | 266 const tracked_objects::Location& from_here, |
268 const base::Closure& task, | 267 const base::Closure& task, |
269 TimeDelta delay) { | 268 int64 delay_ms) { |
270 DCHECK(!task.is_null()) << from_here.ToString(); | 269 DCHECK(!task.is_null()) << from_here.ToString(); |
271 PendingTask pending_task( | 270 PendingTask pending_task(from_here, task, |
272 from_here, task, CalculateDelayedRuntime(delay), true); | 271 CalculateDelayedRuntime(delay_ms), true); |
273 AddToIncomingQueue(&pending_task); | 272 AddToIncomingQueue(&pending_task); |
274 } | 273 } |
275 | 274 |
| 275 void MessageLoop::PostDelayedTask( |
| 276 const tracked_objects::Location& from_here, |
| 277 const base::Closure& task, |
| 278 base::TimeDelta delay) { |
| 279 PostDelayedTask(from_here, task, delay.InMillisecondsRoundedUp()); |
| 280 } |
| 281 |
276 void MessageLoop::PostNonNestableTask( | 282 void MessageLoop::PostNonNestableTask( |
277 const tracked_objects::Location& from_here, const base::Closure& task) { | 283 const tracked_objects::Location& from_here, const base::Closure& task) { |
278 DCHECK(!task.is_null()) << from_here.ToString(); | 284 DCHECK(!task.is_null()) << from_here.ToString(); |
279 PendingTask pending_task( | 285 PendingTask pending_task(from_here, task, CalculateDelayedRuntime(0), false); |
280 from_here, task, CalculateDelayedRuntime(TimeDelta()), false); | 286 AddToIncomingQueue(&pending_task); |
| 287 } |
| 288 |
| 289 void MessageLoop::PostNonNestableDelayedTask( |
| 290 const tracked_objects::Location& from_here, const base::Closure& task, |
| 291 int64 delay_ms) { |
| 292 DCHECK(!task.is_null()) << from_here.ToString(); |
| 293 PendingTask pending_task(from_here, task, |
| 294 CalculateDelayedRuntime(delay_ms), false); |
281 AddToIncomingQueue(&pending_task); | 295 AddToIncomingQueue(&pending_task); |
282 } | 296 } |
283 | 297 |
284 void MessageLoop::PostNonNestableDelayedTask( | 298 void MessageLoop::PostNonNestableDelayedTask( |
285 const tracked_objects::Location& from_here, | 299 const tracked_objects::Location& from_here, |
286 const base::Closure& task, | 300 const base::Closure& task, |
287 TimeDelta delay) { | 301 base::TimeDelta delay) { |
288 DCHECK(!task.is_null()) << from_here.ToString(); | 302 PostNonNestableDelayedTask(from_here, task, delay.InMillisecondsRoundedUp()); |
289 PendingTask pending_task( | |
290 from_here, task, CalculateDelayedRuntime(delay), false); | |
291 AddToIncomingQueue(&pending_task); | |
292 } | 303 } |
293 | 304 |
294 void MessageLoop::Run() { | 305 void MessageLoop::Run() { |
295 AutoRunState save_state(this); | 306 AutoRunState save_state(this); |
296 RunHandler(); | 307 RunHandler(); |
297 } | 308 } |
298 | 309 |
299 void MessageLoop::RunAllPending() { | 310 void MessageLoop::RunAllPending() { |
300 AutoRunState save_state(this); | 311 AutoRunState save_state(this); |
301 state_->quit_received = true; // Means run until we would otherwise block. | 312 state_->quit_received = true; // Means run until we would otherwise block. |
(...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
525 // not completely clear why we want to leak them in the loops above. This | 536 // not completely clear why we want to leak them in the loops above. This |
526 // code is replicating legacy behavior, and should not be considered | 537 // code is replicating legacy behavior, and should not be considered |
527 // absolutely "correct" behavior. See TODO above about deleting all tasks | 538 // absolutely "correct" behavior. See TODO above about deleting all tasks |
528 // when it's safe. | 539 // when it's safe. |
529 while (!delayed_work_queue_.empty()) { | 540 while (!delayed_work_queue_.empty()) { |
530 delayed_work_queue_.pop(); | 541 delayed_work_queue_.pop(); |
531 } | 542 } |
532 return did_work; | 543 return did_work; |
533 } | 544 } |
534 | 545 |
535 TimeTicks MessageLoop::CalculateDelayedRuntime(TimeDelta delay) { | 546 TimeTicks MessageLoop::CalculateDelayedRuntime(int64 delay_ms) { |
536 TimeTicks delayed_run_time; | 547 TimeTicks delayed_run_time; |
537 if (delay > TimeDelta()) { | 548 if (delay_ms > 0) { |
538 delayed_run_time = TimeTicks::Now() + delay; | 549 delayed_run_time = |
| 550 TimeTicks::Now() + TimeDelta::FromMilliseconds(delay_ms); |
539 | 551 |
540 #if defined(OS_WIN) | 552 #if defined(OS_WIN) |
541 if (high_resolution_timer_expiration_.is_null()) { | 553 if (high_resolution_timer_expiration_.is_null()) { |
542 // Windows timers are granular to 15.6ms. If we only set high-res | 554 // Windows timers are granular to 15.6ms. If we only set high-res |
543 // timers for those under 15.6ms, then a 18ms timer ticks at ~32ms, | 555 // timers for those under 15.6ms, then a 18ms timer ticks at ~32ms, |
544 // which as a percentage is pretty inaccurate. So enable high | 556 // which as a percentage is pretty inaccurate. So enable high |
545 // res timers for any timer which is within 2x of the granularity. | 557 // res timers for any timer which is within 2x of the granularity. |
546 // This is a tradeoff between accuracy and power management. | 558 // This is a tradeoff between accuracy and power management. |
547 bool needs_high_res_timers = delay.InMilliseconds() < | 559 bool needs_high_res_timers = |
548 (2 * base::Time::kMinLowResolutionThresholdMs); | 560 delay_ms < (2 * base::Time::kMinLowResolutionThresholdMs); |
549 if (needs_high_res_timers) { | 561 if (needs_high_res_timers) { |
550 if (base::Time::ActivateHighResolutionTimer(true)) { | 562 if (base::Time::ActivateHighResolutionTimer(true)) { |
551 high_resolution_timer_expiration_ = TimeTicks::Now() + | 563 high_resolution_timer_expiration_ = TimeTicks::Now() + |
552 TimeDelta::FromMilliseconds(kHighResolutionTimerModeLeaseTimeMs); | 564 TimeDelta::FromMilliseconds(kHighResolutionTimerModeLeaseTimeMs); |
553 } | 565 } |
554 } | 566 } |
555 } | 567 } |
556 #endif | 568 #endif |
557 } else { | 569 } else { |
558 DCHECK_EQ(delay.InMilliseconds(), 0) << "delay should not be negative"; | 570 DCHECK_EQ(delay_ms, 0) << "delay should not be negative"; |
559 } | 571 } |
560 | 572 |
561 #if defined(OS_WIN) | 573 #if defined(OS_WIN) |
562 if (!high_resolution_timer_expiration_.is_null()) { | 574 if (!high_resolution_timer_expiration_.is_null()) { |
563 if (TimeTicks::Now() > high_resolution_timer_expiration_) { | 575 if (TimeTicks::Now() > high_resolution_timer_expiration_) { |
564 base::Time::ActivateHighResolutionTimer(false); | 576 base::Time::ActivateHighResolutionTimer(false); |
565 high_resolution_timer_expiration_ = TimeTicks(); | 577 high_resolution_timer_expiration_ = TimeTicks(); |
566 } | 578 } |
567 } | 579 } |
568 #endif | 580 #endif |
(...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
789 Watcher *delegate) { | 801 Watcher *delegate) { |
790 return pump_libevent()->WatchFileDescriptor( | 802 return pump_libevent()->WatchFileDescriptor( |
791 fd, | 803 fd, |
792 persistent, | 804 persistent, |
793 static_cast<base::MessagePumpLibevent::Mode>(mode), | 805 static_cast<base::MessagePumpLibevent::Mode>(mode), |
794 controller, | 806 controller, |
795 delegate); | 807 delegate); |
796 } | 808 } |
797 | 809 |
798 #endif | 810 #endif |
OLD | NEW |