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