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