| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2006, 2008 Apple Inc. All rights reserved. | 2 * Copyright (C) 2006, 2008 Apple Inc. All rights reserved. |
| 3 * Copyright (C) 2009 Google Inc. All rights reserved. | 3 * Copyright (C) 2009 Google Inc. All rights reserved. |
| 4 * | 4 * |
| 5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
| 6 * modification, are permitted provided that the following conditions | 6 * modification, are permitted provided that the following conditions |
| 7 * are met: | 7 * are met: |
| 8 * 1. Redistributions of source code must retain the above copyright | 8 * 1. Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * 2. Redistributions in binary form must reproduce the above copyright | 10 * 2. Redistributions in binary form must reproduce the above copyright |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 #include "platform/Timer.h" | 28 #include "platform/Timer.h" |
| 29 | 29 |
| 30 #include "platform/PlatformThreadData.h" | 30 #include "platform/PlatformThreadData.h" |
| 31 #include "platform/ThreadTimers.h" | 31 #include "platform/ThreadTimers.h" |
| 32 #include "wtf/CurrentTime.h" | 32 #include "wtf/CurrentTime.h" |
| 33 #include "wtf/HashSet.h" | 33 #include "wtf/HashSet.h" |
| 34 #include <limits.h> | 34 #include <limits.h> |
| 35 #include <math.h> | 35 #include <math.h> |
| 36 #include <limits> | 36 #include <limits> |
| 37 | 37 |
| 38 using namespace std; | |
| 39 | |
| 40 namespace blink { | 38 namespace blink { |
| 41 | 39 |
| 42 class TimerHeapReference; | 40 class TimerHeapReference; |
| 43 | 41 |
| 44 // Timers are stored in a heap data structure, used to implement a priority queu
e. | 42 // Timers are stored in a heap data structure, used to implement a priority queu
e. |
| 45 // This allows us to efficiently determine which timer needs to fire the soonest
. | 43 // This allows us to efficiently determine which timer needs to fire the soonest
. |
| 46 // Then we set a single shared system timer to fire at that time. | 44 // Then we set a single shared system timer to fire at that time. |
| 47 // | 45 // |
| 48 // When a timer's "next fire time" changes, we need to move it around in the pri
ority queue. | 46 // When a timer's "next fire time" changes, we need to move it around in the pri
ority queue. |
| 49 static Vector<TimerBase*>& threadGlobalTimerHeap() | 47 static Vector<TimerBase*>& threadGlobalTimerHeap() |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 99 | 97 |
| 100 // Invoke the assignment operator, since that takes care of updating m_heapI
ndex. | 98 // Invoke the assignment operator, since that takes care of updating m_heapI
ndex. |
| 101 a = timerB; | 99 a = timerB; |
| 102 b = timerA; | 100 b = timerA; |
| 103 } | 101 } |
| 104 | 102 |
| 105 // ---------------- | 103 // ---------------- |
| 106 | 104 |
| 107 // Class to represent iterators in the heap when calling the standard library he
ap algorithms. | 105 // Class to represent iterators in the heap when calling the standard library he
ap algorithms. |
| 108 // Uses a custom pointer and reference type that update indices for pointers in
the heap. | 106 // Uses a custom pointer and reference type that update indices for pointers in
the heap. |
| 109 class TimerHeapIterator : public iterator<random_access_iterator_tag, TimerBase*
, ptrdiff_t, TimerHeapPointer, TimerHeapReference> { | 107 class TimerHeapIterator : public std::iterator<std::random_access_iterator_tag,
TimerBase*, ptrdiff_t, TimerHeapPointer, TimerHeapReference> { |
| 110 public: | 108 public: |
| 111 explicit TimerHeapIterator(TimerBase** pointer) : m_pointer(pointer) { check
Consistency(); } | 109 explicit TimerHeapIterator(TimerBase** pointer) : m_pointer(pointer) { check
Consistency(); } |
| 112 | 110 |
| 113 TimerHeapIterator& operator++() { checkConsistency(); ++m_pointer; checkCons
istency(); return *this; } | 111 TimerHeapIterator& operator++() { checkConsistency(); ++m_pointer; checkCons
istency(); return *this; } |
| 114 TimerHeapIterator operator++(int) { checkConsistency(1); return TimerHeapIte
rator(m_pointer++); } | 112 TimerHeapIterator operator++(int) { checkConsistency(1); return TimerHeapIte
rator(m_pointer++); } |
| 115 | 113 |
| 116 TimerHeapIterator& operator--() { checkConsistency(); --m_pointer; checkCons
istency(); return *this; } | 114 TimerHeapIterator& operator--() { checkConsistency(); --m_pointer; checkCons
istency(); return *this; } |
| 117 TimerHeapIterator operator--(int) { checkConsistency(-1); return TimerHeapIt
erator(m_pointer--); } | 115 TimerHeapIterator operator--(int) { checkConsistency(-1); return TimerHeapIt
erator(m_pointer--); } |
| 118 | 116 |
| 119 TimerHeapIterator& operator+=(ptrdiff_t i) { checkConsistency(); m_pointer +
= i; checkConsistency(); return *this; } | 117 TimerHeapIterator& operator+=(ptrdiff_t i) { checkConsistency(); m_pointer +
= i; checkConsistency(); return *this; } |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 173 // The comparisons below are "backwards" because the heap puts the largest | 171 // The comparisons below are "backwards" because the heap puts the largest |
| 174 // element first and we want the lowest time to be the first one in the heap
. | 172 // element first and we want the lowest time to be the first one in the heap
. |
| 175 double aFireTime = a->m_nextFireTime; | 173 double aFireTime = a->m_nextFireTime; |
| 176 double bFireTime = b->m_nextFireTime; | 174 double bFireTime = b->m_nextFireTime; |
| 177 if (bFireTime != aFireTime) | 175 if (bFireTime != aFireTime) |
| 178 return bFireTime < aFireTime; | 176 return bFireTime < aFireTime; |
| 179 | 177 |
| 180 // We need to look at the difference of the insertion orders instead of comp
aring the two | 178 // We need to look at the difference of the insertion orders instead of comp
aring the two |
| 181 // outright in case of overflow. | 179 // outright in case of overflow. |
| 182 unsigned difference = a->m_heapInsertionOrder - b->m_heapInsertionOrder; | 180 unsigned difference = a->m_heapInsertionOrder - b->m_heapInsertionOrder; |
| 183 return difference < numeric_limits<unsigned>::max() / 2; | 181 return difference < std::numeric_limits<unsigned>::max() / 2; |
| 184 } | 182 } |
| 185 | 183 |
| 186 // ---------------- | 184 // ---------------- |
| 187 | 185 |
| 188 TimerBase::TimerBase() | 186 TimerBase::TimerBase() |
| 189 : m_nextFireTime(0) | 187 : m_nextFireTime(0) |
| 190 , m_unalignedNextFireTime(0) | 188 , m_unalignedNextFireTime(0) |
| 191 , m_repeatInterval(0) | 189 , m_repeatInterval(0) |
| 192 , m_heapIndex(-1) | 190 , m_heapIndex(-1) |
| 193 , m_cachedThreadGlobalTimerHeap(0) | 191 , m_cachedThreadGlobalTimerHeap(0) |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 287 ASSERT(!inHeap()); | 285 ASSERT(!inHeap()); |
| 288 timerHeap().append(this); | 286 timerHeap().append(this); |
| 289 m_heapIndex = timerHeap().size() - 1; | 287 m_heapIndex = timerHeap().size() - 1; |
| 290 heapDecreaseKey(); | 288 heapDecreaseKey(); |
| 291 } | 289 } |
| 292 | 290 |
| 293 inline void TimerBase::heapPop() | 291 inline void TimerBase::heapPop() |
| 294 { | 292 { |
| 295 // Temporarily force this timer to have the minimum key so we can pop it. | 293 // Temporarily force this timer to have the minimum key so we can pop it. |
| 296 double fireTime = m_nextFireTime; | 294 double fireTime = m_nextFireTime; |
| 297 m_nextFireTime = -numeric_limits<double>::infinity(); | 295 m_nextFireTime = -std::numeric_limits<double>::infinity(); |
| 298 heapDecreaseKey(); | 296 heapDecreaseKey(); |
| 299 heapPopMin(); | 297 heapPopMin(); |
| 300 m_nextFireTime = fireTime; | 298 m_nextFireTime = fireTime; |
| 301 } | 299 } |
| 302 | 300 |
| 303 void TimerBase::heapPopMin() | 301 void TimerBase::heapPopMin() |
| 304 { | 302 { |
| 305 ASSERT(this == timerHeap().first()); | 303 ASSERT(this == timerHeap().first()); |
| 306 checkHeapIndex(); | 304 checkHeapIndex(); |
| 307 Vector<TimerBase*>& heap = timerHeap(); | 305 Vector<TimerBase*>& heap = timerHeap(); |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 402 } | 400 } |
| 403 | 401 |
| 404 void TimerBase::didChangeAlignmentInterval() | 402 void TimerBase::didChangeAlignmentInterval() |
| 405 { | 403 { |
| 406 setNextFireTime(m_unalignedNextFireTime); | 404 setNextFireTime(m_unalignedNextFireTime); |
| 407 } | 405 } |
| 408 | 406 |
| 409 double TimerBase::nextUnalignedFireInterval() const | 407 double TimerBase::nextUnalignedFireInterval() const |
| 410 { | 408 { |
| 411 ASSERT(isActive()); | 409 ASSERT(isActive()); |
| 412 return max(m_unalignedNextFireTime - monotonicallyIncreasingTime(), 0.0); | 410 return std::max(m_unalignedNextFireTime - monotonicallyIncreasingTime(), 0.0
); |
| 413 } | 411 } |
| 414 | 412 |
| 415 } // namespace blink | 413 } // namespace blink |
| 416 | 414 |
| OLD | NEW |