WebKit2/Platform/RunLoop.h
changeset 0 4f2f89ce4247
equal deleted inserted replaced
-1:000000000000 0:4f2f89ce4247
       
     1 /*
       
     2  * Copyright (C) 2010 Apple Inc. All rights reserved.
       
     3  * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)
       
     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. AND ITS CONTRIBUTORS ``AS IS''
       
    15  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
       
    16  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
       
    17  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
       
    18  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
       
    19  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
       
    20  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
       
    21  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
       
    22  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
       
    23  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
       
    24  * THE POSSIBILITY OF SUCH DAMAGE.
       
    25  */
       
    26 
       
    27 #ifndef RunLoop_h
       
    28 #define RunLoop_h
       
    29 
       
    30 #include <wtf/HashMap.h>
       
    31 #include <wtf/PassOwnPtr.h>
       
    32 #include <wtf/ThreadSpecific.h>
       
    33 #include <wtf/Threading.h>
       
    34 #include <wtf/Vector.h>
       
    35 
       
    36 class WorkItem;
       
    37 
       
    38 class RunLoop {
       
    39 public:
       
    40     // Must be called from the main thread.
       
    41     static void initializeMainRunLoop();
       
    42 
       
    43     static RunLoop* current();
       
    44     static RunLoop* main();
       
    45 
       
    46     void scheduleWork(PassOwnPtr<WorkItem>);
       
    47 
       
    48     static void run();
       
    49     void stop();
       
    50 
       
    51     class TimerBase {
       
    52         friend class RunLoop;
       
    53     public:
       
    54         TimerBase(RunLoop*);
       
    55         virtual ~TimerBase();
       
    56 
       
    57         void startRepeating(double repeatInterval) { start(repeatInterval, repeatInterval); }
       
    58         void startOneShot(double interval) { start(interval, 0); }
       
    59 
       
    60         void stop();
       
    61         bool isActive() const;
       
    62 
       
    63         virtual void fired() = 0;
       
    64 
       
    65     private:
       
    66         void start(double nextFireInterval, double repeatInterval);
       
    67 
       
    68         RunLoop* m_runLoop;
       
    69 
       
    70 #if PLATFORM(WIN)
       
    71         static void timerFired(RunLoop*, uint64_t ID);
       
    72         uint64_t m_ID;
       
    73 #elif PLATFORM(MAC)
       
    74         static void timerFired(CFRunLoopTimerRef, void*);
       
    75         CFRunLoopTimerRef m_timer;
       
    76 #elif PLATFORM(QT)
       
    77         static void timerFired(RunLoop*, int ID);
       
    78         int m_ID;
       
    79 #endif
       
    80     };
       
    81 
       
    82     template <typename TimerFiredClass>
       
    83     class Timer : public TimerBase {
       
    84     public:
       
    85         typedef void (TimerFiredClass::*TimerFiredFunction)();
       
    86 
       
    87         Timer(RunLoop* runLoop, TimerFiredClass* o, TimerFiredFunction f)
       
    88             : TimerBase(runLoop)
       
    89             , m_object(o)
       
    90             , m_function(f)
       
    91         {
       
    92         }
       
    93 
       
    94     private:
       
    95         virtual void fired() { (m_object->*m_function)(); }
       
    96 
       
    97         TimerFiredClass* m_object;
       
    98         TimerFiredFunction m_function;
       
    99     };
       
   100 
       
   101 private:
       
   102     friend class WTF::ThreadSpecific<RunLoop>;
       
   103 
       
   104     RunLoop();
       
   105     ~RunLoop();
       
   106 
       
   107     void performWork();
       
   108     void wakeUp();
       
   109 
       
   110     Mutex m_workItemQueueLock;
       
   111     Vector<WorkItem*> m_workItemQueue;
       
   112 
       
   113 #if PLATFORM(WIN)
       
   114     static bool registerRunLoopMessageWindowClass();
       
   115     static LRESULT CALLBACK RunLoopWndProc(HWND, UINT, WPARAM, LPARAM);
       
   116     LRESULT wndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
       
   117     HWND m_runLoopMessageWindow;
       
   118 
       
   119     typedef HashMap<uint64_t, TimerBase*> TimerMap;
       
   120     TimerMap m_activeTimers;
       
   121 #elif PLATFORM(MAC)
       
   122     static void performWork(void*);
       
   123     CFRunLoopRef m_runLoop;
       
   124     CFRunLoopSourceRef m_runLoopSource;
       
   125 #elif PLATFORM(QT)
       
   126     typedef HashMap<int, TimerBase*> TimerMap;
       
   127     TimerMap m_activeTimers;
       
   128     class TimerObject;
       
   129     TimerObject* m_timerObject;
       
   130 #endif
       
   131 };
       
   132 
       
   133 #endif // RunLoop_h