| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright (C) 2010 Apple Inc. All rights reserved. | |
| 3 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies) | |
| 4 * Portions Copyright (c) 2010 Motorola Mobility, Inc. All rights reserved. | |
| 5 * | |
| 6 * Redistribution and use in source and binary forms, with or without | |
| 7 * modification, are permitted provided that the following conditions | |
| 8 * are met: | |
| 9 * 1. Redistributions of source code must retain the above copyright | |
| 10 * notice, this list of conditions and the following disclaimer. | |
| 11 * 2. Redistributions in binary form must reproduce the above copyright | |
| 12 * notice, this list of conditions and the following disclaimer in the | |
| 13 * documentation and/or other materials provided with the distribution. | |
| 14 * | |
| 15 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' | |
| 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, | |
| 17 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
| 18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS | |
| 19 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
| 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
| 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
| 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
| 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
| 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF | |
| 25 * THE POSSIBILITY OF SUCH DAMAGE. | |
| 26 */ | |
| 27 | |
| 28 #ifndef RunLoop_h | |
| 29 #define RunLoop_h | |
| 30 | |
| 31 #include <wtf/Deque.h> | |
| 32 #include <wtf/Forward.h> | |
| 33 #include <wtf/Functional.h> | |
| 34 #include <wtf/HashMap.h> | |
| 35 #include <wtf/ThreadSpecific.h> | |
| 36 #include <wtf/Threading.h> | |
| 37 | |
| 38 #if PLATFORM(GTK) | |
| 39 #include <wtf/gobject/GRefPtr.h> | |
| 40 #endif | |
| 41 | |
| 42 namespace WebCore { | |
| 43 | |
| 44 class RunLoop { | |
| 45 public: | |
| 46 // Must be called from the main thread (except for the Mac platform, where i
t | |
| 47 // can be called from any thread). | |
| 48 static void initializeMainRunLoop(); | |
| 49 | |
| 50 // Must be called before entering main run loop. If called, application styl
e run loop will be used, handling events. | |
| 51 static void setUseApplicationRunLoopOnMainRunLoop(); | |
| 52 | |
| 53 static RunLoop* current(); | |
| 54 static RunLoop* main(); | |
| 55 | |
| 56 void dispatch(const Function<void()>&); | |
| 57 | |
| 58 static void run(); | |
| 59 void stop(); | |
| 60 void wakeUp(); | |
| 61 | |
| 62 #if PLATFORM(MAC) | |
| 63 void runForDuration(double duration); | |
| 64 #endif | |
| 65 | |
| 66 class TimerBase { | |
| 67 friend class RunLoop; | |
| 68 public: | |
| 69 explicit TimerBase(RunLoop*); | |
| 70 virtual ~TimerBase(); | |
| 71 | |
| 72 void startRepeating(double repeatInterval) { start(repeatInterval, true)
; } | |
| 73 void startOneShot(double interval) { start(interval, false); } | |
| 74 | |
| 75 void stop(); | |
| 76 bool isActive() const; | |
| 77 | |
| 78 virtual void fired() = 0; | |
| 79 | |
| 80 private: | |
| 81 void start(double nextFireInterval, bool repeat); | |
| 82 | |
| 83 RunLoop* m_runLoop; | |
| 84 | |
| 85 #if PLATFORM(WIN) | |
| 86 static void timerFired(RunLoop*, uint64_t ID); | |
| 87 uint64_t m_ID; | |
| 88 bool m_isRepeating; | |
| 89 #elif PLATFORM(MAC) | |
| 90 static void timerFired(CFRunLoopTimerRef, void*); | |
| 91 CFRunLoopTimerRef m_timer; | |
| 92 #elif PLATFORM(QT) | |
| 93 static void timerFired(RunLoop*, int ID); | |
| 94 int m_ID; | |
| 95 bool m_isRepeating; | |
| 96 #elif PLATFORM(GTK) | |
| 97 static gboolean timerFiredCallback(RunLoop::TimerBase*); | |
| 98 gboolean isRepeating() const { return m_isRepeating; } | |
| 99 void clearTimerSource(); | |
| 100 GRefPtr<GSource> m_timerSource; | |
| 101 gboolean m_isRepeating; | |
| 102 #endif | |
| 103 }; | |
| 104 | |
| 105 template <typename TimerFiredClass> | |
| 106 class Timer : public TimerBase { | |
| 107 public: | |
| 108 typedef void (TimerFiredClass::*TimerFiredFunction)(); | |
| 109 | |
| 110 Timer(RunLoop* runLoop, TimerFiredClass* o, TimerFiredFunction f) | |
| 111 : TimerBase(runLoop) | |
| 112 , m_object(o) | |
| 113 , m_function(f) | |
| 114 { | |
| 115 } | |
| 116 | |
| 117 private: | |
| 118 virtual void fired() { (m_object->*m_function)(); } | |
| 119 | |
| 120 TimerFiredClass* m_object; | |
| 121 TimerFiredFunction m_function; | |
| 122 }; | |
| 123 | |
| 124 private: | |
| 125 friend class WTF::ThreadSpecific<RunLoop>; | |
| 126 | |
| 127 RunLoop(); | |
| 128 ~RunLoop(); | |
| 129 | |
| 130 void performWork(); | |
| 131 | |
| 132 Mutex m_functionQueueLock; | |
| 133 Deque<Function<void()> > m_functionQueue; | |
| 134 | |
| 135 #if PLATFORM(WIN) | |
| 136 static bool registerRunLoopMessageWindowClass(); | |
| 137 static LRESULT CALLBACK RunLoopWndProc(HWND, UINT, WPARAM, LPARAM); | |
| 138 LRESULT wndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam); | |
| 139 HWND m_runLoopMessageWindow; | |
| 140 | |
| 141 typedef HashMap<uint64_t, TimerBase*> TimerMap; | |
| 142 TimerMap m_activeTimers; | |
| 143 #elif PLATFORM(MAC) | |
| 144 RunLoop(CFRunLoopRef); | |
| 145 static void performWork(void*); | |
| 146 CFRunLoopRef m_runLoop; | |
| 147 CFRunLoopSourceRef m_runLoopSource; | |
| 148 int m_nestingLevel; | |
| 149 #elif PLATFORM(QT) | |
| 150 typedef HashMap<int, TimerBase*> TimerMap; | |
| 151 TimerMap m_activeTimers; | |
| 152 class TimerObject; | |
| 153 TimerObject* m_timerObject; | |
| 154 #elif PLATFORM(GTK) | |
| 155 public: | |
| 156 static gboolean queueWork(RunLoop*); | |
| 157 GMainLoop* innermostLoop(); | |
| 158 void pushNestedMainLoop(GMainLoop*); | |
| 159 void popNestedMainLoop(); | |
| 160 private: | |
| 161 GRefPtr<GMainContext> m_runLoopContext; | |
| 162 Vector<GRefPtr<GMainLoop> > m_runLoopMainLoops; | |
| 163 #endif | |
| 164 }; | |
| 165 | |
| 166 } // namespace WebCore | |
| 167 | |
| 168 #endif // RunLoop_h | |
| OLD | NEW |