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