RunLoop.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. #ifndef RunLoop_h
  28. #define RunLoop_h
  29. #include <wtf/Deque.h>
  30. #include <wtf/Forward.h>
  31. #include <wtf/FunctionDispatcher.h>
  32. #include <wtf/Functional.h>
  33. #include <wtf/HashMap.h>
  34. #include <wtf/RetainPtr.h>
  35. #include <wtf/Threading.h>
  36. #if PLATFORM(GTK)
  37. #include <wtf/gobject/GRefPtr.h>
  38. #endif
  39. #if PLATFORM(MANX)
  40. #include <manx/RunLoop.h>
  41. #endif
  42. #if PLATFORM(EFL)
  43. #include <Ecore.h>
  44. #endif
  45. namespace WebCore {
  46. class RunLoop : public FunctionDispatcher {
  47. public:
  48. // Must be called from the main thread (except for the Mac platform, where it
  49. // can be called from any thread).
  50. static void initializeMainRunLoop();
  51. // Must be called before entering main run loop. If called, application style run loop will be used, handling events.
  52. static void setUseApplicationRunLoopOnMainRunLoop();
  53. static RunLoop* current();
  54. static RunLoop* main();
  55. ~RunLoop();
  56. virtual void dispatch(const Function<void()>&) OVERRIDE;
  57. static void run();
  58. void stop();
  59. void wakeUp();
  60. #if PLATFORM(MAC)
  61. void runForDuration(double duration);
  62. #endif
  63. class TimerBase {
  64. friend class RunLoop;
  65. public:
  66. explicit TimerBase(RunLoop*);
  67. virtual ~TimerBase();
  68. void startRepeating(double repeatInterval) { start(repeatInterval, true); }
  69. void startOneShot(double interval) { start(interval, false); }
  70. void stop();
  71. bool isActive() const;
  72. virtual void fired() = 0;
  73. private:
  74. void start(double nextFireInterval, bool repeat);
  75. RunLoop* m_runLoop;
  76. #if PLATFORM(WIN)
  77. static void timerFired(RunLoop*, uint64_t ID);
  78. uint64_t m_ID;
  79. bool m_isRepeating;
  80. #elif PLATFORM(MAC)
  81. static void timerFired(CFRunLoopTimerRef, void*);
  82. RetainPtr<CFRunLoopTimerRef> m_timer;
  83. #elif PLATFORM(QT)
  84. static void timerFired(RunLoop*, int ID);
  85. int m_ID;
  86. bool m_isRepeating;
  87. #elif PLATFORM(GTK)
  88. static gboolean timerFiredCallback(RunLoop::TimerBase*);
  89. gboolean isRepeating() const { return m_isRepeating; }
  90. void clearTimerSource();
  91. GRefPtr<GSource> m_timerSource;
  92. gboolean m_isRepeating;
  93. #elif PLATFORM(MANX)
  94. OwnPtr<Manx::RunLoop::Timer> m_timer;
  95. #elif PLATFORM(EFL)
  96. static bool timerFired(void* data);
  97. Ecore_Timer* m_timer;
  98. bool m_isRepeating;
  99. #endif
  100. };
  101. template <typename TimerFiredClass>
  102. class Timer : public TimerBase {
  103. public:
  104. typedef void (TimerFiredClass::*TimerFiredFunction)();
  105. Timer(RunLoop* runLoop, TimerFiredClass* o, TimerFiredFunction f)
  106. : TimerBase(runLoop)
  107. , m_object(o)
  108. , m_function(f)
  109. {
  110. }
  111. private:
  112. virtual void fired() { (m_object->*m_function)(); }
  113. TimerFiredClass* m_object;
  114. TimerFiredFunction m_function;
  115. };
  116. class Holder;
  117. private:
  118. RunLoop();
  119. void performWork();
  120. Mutex m_functionQueueLock;
  121. Deque<Function<void()> > m_functionQueue;
  122. #if PLATFORM(WIN)
  123. static bool registerRunLoopMessageWindowClass();
  124. static LRESULT CALLBACK RunLoopWndProc(HWND, UINT, WPARAM, LPARAM);
  125. LRESULT wndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
  126. HWND m_runLoopMessageWindow;
  127. typedef HashMap<uint64_t, TimerBase*> TimerMap;
  128. TimerMap m_activeTimers;
  129. #elif PLATFORM(MAC)
  130. static void performWork(void*);
  131. RetainPtr<CFRunLoopRef> m_runLoop;
  132. RetainPtr<CFRunLoopSourceRef> m_runLoopSource;
  133. int m_nestingLevel;
  134. #elif PLATFORM(QT)
  135. typedef HashMap<int, TimerBase*> TimerMap;
  136. TimerMap m_activeTimers;
  137. class TimerObject;
  138. TimerObject* m_timerObject;
  139. #elif PLATFORM(GTK)
  140. public:
  141. static gboolean queueWork(RunLoop*);
  142. GMainLoop* innermostLoop();
  143. void pushNestedMainLoop(GMainLoop*);
  144. void popNestedMainLoop();
  145. private:
  146. GRefPtr<GMainContext> m_runLoopContext;
  147. Vector<GRefPtr<GMainLoop> > m_runLoopMainLoops;
  148. #elif PLATFORM(MANX)
  149. Manx::RunLoop *m_manxRunLoop;
  150. OwnPtr<Manx::RunLoop::Timer> m_wakeUpTimer;
  151. #elif PLATFORM(EFL)
  152. bool m_initEfl;
  153. Mutex m_pipeLock;
  154. OwnPtr<Ecore_Pipe> m_pipe;
  155. Mutex m_wakeUpEventRequestedLock;
  156. bool m_wakeUpEventRequested;
  157. static void wakeUpEvent(void* data, void*, unsigned int);
  158. #endif
  159. };
  160. } // namespace WebCore
  161. #endif // RunLoop_h