OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef TimerHeap_h |
| 6 #define TimerHeap_h |
| 7 |
| 8 #include "wtf/HashSet.h" |
| 9 #include "wtf/Noncopyable.h" |
| 10 #include "wtf/Threading.h" |
| 11 #include "wtf/Vector.h" |
| 12 |
| 13 namespace blink { |
| 14 |
| 15 class TimerBase; |
| 16 class TimerHeapEntry; |
| 17 |
| 18 // Gets notified by a TimerHeap when the item at the front of the |
| 19 // queue may have changed--either it's a different item, or the "next |
| 20 // fire time" of the item has changed. |
| 21 class TimerQueueObserver { |
| 22 WTF_MAKE_NONCOPYABLE(TimerQueueObserver); |
| 23 |
| 24 public: |
| 25 TimerQueueObserver(); |
| 26 virtual ~TimerQueueObserver(); |
| 27 |
| 28 virtual void nextFiringTimerChanged() = 0; |
| 29 }; |
| 30 |
| 31 // FIXME: That this is a heap is an implementation detail; rename it |
| 32 // to TimerQueue or TimerPriorityQueue or NextFiringTimerQueue or |
| 33 // something like that. |
| 34 |
| 35 // Timers are stored in a min-heap, keyed by firing time. There's one |
| 36 // heap per-thread; the timers in each heap are serviced by one |
| 37 // platform timer (the "real" timer) whose schedule is set by the |
| 38 // timer that's at the the top of the heap (ie the timer that should |
| 39 // fire next). |
| 40 class TimerHeap { |
| 41 WTF_MAKE_NONCOPYABLE(TimerHeap); |
| 42 |
| 43 public: |
| 44 // Gets the thread-static TimerHeap for the current thread. |
| 45 static TimerHeap& get(); |
| 46 |
| 47 // Creates a TimerHeap that will notify the observer when the head |
| 48 // of the queue changes. The observer must live as long as the |
| 49 // queue is being modified. |
| 50 TimerHeap(TimerQueueObserver*); |
| 51 virtual ~TimerHeap(); |
| 52 |
| 53 // Does a consistency check of the entire TimerHeap structure. |
| 54 void check() const; |
| 55 |
| 56 // Used by TimerThread |
| 57 bool isEmpty() const; |
| 58 TimerBase* first() const; |
| 59 |
| 60 // Used by TimerHeapEntry |
| 61 void didChangeValue(TimerBase&, double oldValue); |
| 62 |
| 63 private: |
| 64 friend class TimerHeapIterator; |
| 65 friend class TimerHeapReference; |
| 66 |
| 67 // Performs a consistency check of a specific entry. |
| 68 void check(const TimerHeapEntry&) const; |
| 69 |
| 70 // Checks whether an entry is in the heap, if so, that the heap |
| 71 // property holds around the entry. |
| 72 bool hasValidHeapPosition(const TimerHeapEntry&) const; |
| 73 bool parentHeapPropertyHolds(const TimerHeapEntry&) const; |
| 74 bool childHeapPropertyHolds(const TimerHeapEntry&, size_t childIndex) const; |
| 75 |
| 76 // Percolates a timer that is in the heap whose key got smaller up |
| 77 // in the heap, if necessary. Used by didChangeValue. |
| 78 void didDecreaseKey(TimerBase*); |
| 79 |
| 80 // Like didDecreaseKey, but pushes a timer in the heap whose key |
| 81 // got larger deeper into the heap, if necessary. |
| 82 void didIncreaseKey(TimerBase*); |
| 83 |
| 84 void insert(TimerBase*); |
| 85 void remove(TimerBase*); |
| 86 void pop(TimerBase*); |
| 87 |
| 88 TimerQueueObserver* m_observer; |
| 89 ThreadIdentifier m_thread; |
| 90 Vector<TimerBase*> m_heap; |
| 91 HashSet<TimerBase*> m_entries; |
| 92 size_t m_currentHeapInsertionOrder; |
| 93 }; |
| 94 |
| 95 } // namespace blink |
| 96 |
| 97 #endif // TimerHeap_h |
OLD | NEW |