/src/3rdparty/webkit/Source/WebKit2/Platform/RunLoop.h

https://bitbucket.org/ultra_iter/qt-vtl · C Header · 169 lines · 111 code · 27 blank · 31 comment · 0 complexity · 7b1eda65f36022fcd354657e07d5d1a9 MD5 · raw file

  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/HashMap.h>
  30. #include <wtf/PassOwnPtr.h>
  31. #include <wtf/ThreadSpecific.h>
  32. #include <wtf/Threading.h>
  33. #include <wtf/Vector.h>
  34. #if PLATFORM(GTK)
  35. #include <wtf/gobject/GRefPtr.h>
  36. typedef struct _GSource GSource;
  37. typedef struct _GMainLoop GMainLoop;
  38. typedef struct _GMainContext GMainContext;
  39. typedef int gboolean;
  40. #endif
  41. class WorkItem;
  42. namespace CoreIPC {
  43. class BinarySemaphore;
  44. }
  45. class RunLoop {
  46. public:
  47. // Must be called from the main thread.
  48. static void initializeMainRunLoop();
  49. static RunLoop* current();
  50. static RunLoop* main();
  51. void scheduleWork(PassOwnPtr<WorkItem>);
  52. #if PLATFORM(WIN)
  53. // The absoluteTime is in seconds, starting on January 1, 1970. The time is assumed to use the
  54. // same time zone as WTF::currentTime(). Dispatches sent (not posted) messages to the passed-in
  55. // set of HWNDs until the semaphore is signaled or absoluteTime is reached. Returns true if the
  56. // semaphore is signaled, false otherwise.
  57. static bool dispatchSentMessagesUntil(const Vector<HWND>& windows, CoreIPC::BinarySemaphore&, double absoluteTime);
  58. #endif
  59. static void run();
  60. void stop();
  61. class TimerBase {
  62. friend class RunLoop;
  63. public:
  64. TimerBase(RunLoop*);
  65. virtual ~TimerBase();
  66. void startRepeating(double repeatInterval) { start(repeatInterval, true); }
  67. void startOneShot(double interval) { start(interval, false); }
  68. void stop();
  69. bool isActive() const;
  70. virtual void fired() = 0;
  71. private:
  72. void start(double nextFireInterval, bool repeat);
  73. RunLoop* m_runLoop;
  74. #if PLATFORM(WIN)
  75. static void timerFired(RunLoop*, uint64_t ID);
  76. uint64_t m_ID;
  77. bool m_isRepeating;
  78. #elif PLATFORM(MAC)
  79. static void timerFired(CFRunLoopTimerRef, void*);
  80. CFRunLoopTimerRef m_timer;
  81. #elif PLATFORM(QT)
  82. static void timerFired(RunLoop*, int ID);
  83. int m_ID;
  84. bool m_isRepeating;
  85. #elif PLATFORM(GTK)
  86. static gboolean timerFiredCallback(RunLoop::TimerBase*);
  87. static void destroyNotifyCallback(RunLoop::TimerBase*);
  88. gboolean isRepeating() const { return m_isRepeating; }
  89. void clearTimerSource();
  90. GRefPtr<GSource> m_timerSource;
  91. gboolean m_isRepeating;
  92. #endif
  93. };
  94. template <typename TimerFiredClass>
  95. class Timer : public TimerBase {
  96. public:
  97. typedef void (TimerFiredClass::*TimerFiredFunction)();
  98. Timer(RunLoop* runLoop, TimerFiredClass* o, TimerFiredFunction f)
  99. : TimerBase(runLoop)
  100. , m_object(o)
  101. , m_function(f)
  102. {
  103. }
  104. private:
  105. virtual void fired() { (m_object->*m_function)(); }
  106. TimerFiredClass* m_object;
  107. TimerFiredFunction m_function;
  108. };
  109. private:
  110. friend class WTF::ThreadSpecific<RunLoop>;
  111. RunLoop();
  112. ~RunLoop();
  113. void performWork();
  114. void wakeUp();
  115. Mutex m_workItemQueueLock;
  116. Vector<OwnPtr<WorkItem> > m_workItemQueue;
  117. #if PLATFORM(WIN)
  118. static bool registerRunLoopMessageWindowClass();
  119. static LRESULT CALLBACK RunLoopWndProc(HWND, UINT, WPARAM, LPARAM);
  120. LRESULT wndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
  121. HWND m_runLoopMessageWindow;
  122. typedef HashMap<uint64_t, TimerBase*> TimerMap;
  123. TimerMap m_activeTimers;
  124. #elif PLATFORM(MAC)
  125. static void performWork(void*);
  126. CFRunLoopRef m_runLoop;
  127. CFRunLoopSourceRef m_runLoopSource;
  128. #elif PLATFORM(QT)
  129. typedef HashMap<int, TimerBase*> TimerMap;
  130. TimerMap m_activeTimers;
  131. class TimerObject;
  132. TimerObject* m_timerObject;
  133. #elif PLATFORM(GTK)
  134. public:
  135. static gboolean queueWork(RunLoop*);
  136. GMainLoop* mainLoop();
  137. private:
  138. GMainContext* m_runLoopContext;
  139. GMainLoop* m_runLoopMain;
  140. #endif
  141. };
  142. #endif // RunLoop_h