1/*
2 * Copyright (C) 2006 Apple Inc. All rights reserved.
3 * Copyright (C) 2009 Google Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#ifndef ThreadTimers_h
28#define ThreadTimers_h
29
30#include <wtf/MonotonicTime.h>
31#include <wtf/Noncopyable.h>
32#include <wtf/RefCounted.h>
33#include <wtf/ThreadSafeRefCounted.h>
34#include <wtf/Vector.h>
35
36namespace WebCore {
37
38class SharedTimer;
39class ThreadTimers;
40class TimerBase;
41
42struct ThreadTimerHeapItem;
43typedef Vector<RefPtr<ThreadTimerHeapItem>> ThreadTimerHeap;
44
45// A collection of timers per thread. Kept in ThreadGlobalData.
46class ThreadTimers {
47 WTF_MAKE_NONCOPYABLE(ThreadTimers); WTF_MAKE_FAST_ALLOCATED;
48public:
49 ThreadTimers();
50
51 // On a thread different then main, we should set the thread's instance of the SharedTimer.
52 void setSharedTimer(SharedTimer*);
53
54 ThreadTimerHeap& timerHeap() { return m_timerHeap; }
55
56 void updateSharedTimer();
57 void fireTimersInNestedEventLoop();
58
59private:
60 void sharedTimerFiredInternal();
61 void fireTimersInNestedEventLoopInternal();
62
63 ThreadTimerHeap m_timerHeap;
64 SharedTimer* m_sharedTimer { nullptr }; // External object, can be a run loop on a worker thread. Normally set/reset by worker thread.
65 bool m_firingTimers { false }; // Reentrancy guard.
66 MonotonicTime m_pendingSharedTimerFireTime;
67};
68
69struct ThreadTimerHeapItem : ThreadSafeRefCounted<ThreadTimerHeapItem> {
70 static RefPtr<ThreadTimerHeapItem> create(TimerBase&, MonotonicTime, unsigned);
71
72 bool hasTimer() const { return m_timer; }
73 TimerBase& timer();
74 void clearTimer();
75
76 ThreadTimerHeap& timerHeap() const;
77
78 unsigned heapIndex() const;
79 void setHeapIndex(unsigned newIndex);
80 void setNotInHeap() { m_heapIndex = invalidHeapIndex; }
81
82 bool isInHeap() const { return m_heapIndex != invalidHeapIndex; }
83 bool isFirstInHeap() const { return !m_heapIndex; }
84
85 MonotonicTime time;
86 unsigned insertionOrder { 0 };
87
88private:
89 ThreadTimers& m_threadTimers;
90 TimerBase* m_timer { nullptr };
91 unsigned m_heapIndex { invalidHeapIndex };
92
93 static const unsigned invalidHeapIndex = static_cast<unsigned>(-1);
94
95 ThreadTimerHeapItem(TimerBase&, MonotonicTime, unsigned);
96};
97
98inline TimerBase& ThreadTimerHeapItem::timer()
99{
100 ASSERT(m_timer);
101 return *m_timer;
102}
103
104inline void ThreadTimerHeapItem::clearTimer()
105{
106 ASSERT(!isInHeap());
107 m_timer = nullptr;
108}
109
110inline unsigned ThreadTimerHeapItem::heapIndex() const
111{
112 ASSERT(m_heapIndex != invalidHeapIndex);
113 return static_cast<unsigned>(m_heapIndex);
114}
115
116inline void ThreadTimerHeapItem::setHeapIndex(unsigned newIndex)
117{
118 ASSERT(newIndex != invalidHeapIndex);
119 m_heapIndex = newIndex;
120}
121
122inline ThreadTimerHeap& ThreadTimerHeapItem::timerHeap() const
123{
124 return m_threadTimers.timerHeap();
125}
126
127}
128
129#endif
130