Home | History | Annotate | Download | only in platform
      1 /*
      2  * Copyright (C) 2006 Apple Computer, Inc.  All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions
      6  * are met:
      7  * 1. Redistributions of source code must retain the above copyright
      8  *    notice, this list of conditions and the following disclaimer.
      9  * 2. Redistributions in binary form must reproduce the above copyright
     10  *    notice, this list of conditions and the following disclaimer in the
     11  *    documentation and/or other materials provided with the distribution.
     12  *
     13  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
     14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
     17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
     21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     24  */
     25 
     26 #ifndef Timer_h
     27 #define Timer_h
     28 
     29 #include "wtf/Noncopyable.h"
     30 #include "wtf/Threading.h"
     31 #include "wtf/Vector.h"
     32 
     33 namespace WebCore {
     34 
     35 // Time intervals are all in seconds.
     36 
     37 class TimerHeapElement;
     38 
     39 class TimerBase {
     40     WTF_MAKE_NONCOPYABLE(TimerBase); WTF_MAKE_FAST_ALLOCATED;
     41 public:
     42     TimerBase();
     43     virtual ~TimerBase();
     44 
     45     void start(double nextFireInterval, double repeatInterval);
     46 
     47     void startRepeating(double repeatInterval) { start(repeatInterval, repeatInterval); }
     48     void startOneShot(double interval) { start(interval, 0); }
     49 
     50     void stop();
     51     bool isActive() const;
     52 
     53     double nextFireInterval() const;
     54     double nextUnalignedFireInterval() const;
     55     double repeatInterval() const { return m_repeatInterval; }
     56 
     57     void augmentRepeatInterval(double delta) {
     58         setNextFireTime(m_nextFireTime + delta);
     59         m_repeatInterval += delta;
     60     }
     61 
     62     void didChangeAlignmentInterval();
     63 
     64     static void fireTimersInNestedEventLoop();
     65 
     66 private:
     67     virtual void fired() = 0;
     68 
     69     virtual double alignedFireTime(double fireTime) const { return fireTime; }
     70 
     71     void checkConsistency() const;
     72     void checkHeapIndex() const;
     73 
     74     void setNextFireTime(double);
     75 
     76     bool inHeap() const { return m_heapIndex != -1; }
     77 
     78     bool hasValidHeapPosition() const;
     79     void updateHeapIfNeeded(double oldTime);
     80 
     81     void heapDecreaseKey();
     82     void heapDelete();
     83     void heapDeleteMin();
     84     void heapIncreaseKey();
     85     void heapInsert();
     86     void heapPop();
     87     void heapPopMin();
     88 
     89     Vector<TimerBase*>& timerHeap() const { ASSERT(m_cachedThreadGlobalTimerHeap); return *m_cachedThreadGlobalTimerHeap; }
     90 
     91     double m_nextFireTime; // 0 if inactive
     92     double m_unalignedNextFireTime; // m_nextFireTime not considering alignment interval
     93     double m_repeatInterval; // 0 if not repeating
     94     int m_heapIndex; // -1 if not in heap
     95     unsigned m_heapInsertionOrder; // Used to keep order among equal-fire-time timers
     96     Vector<TimerBase*>* m_cachedThreadGlobalTimerHeap;
     97 
     98 #ifndef NDEBUG
     99     ThreadIdentifier m_thread;
    100 #endif
    101 
    102     friend class ThreadTimers;
    103     friend class TimerHeapLessThanFunction;
    104     friend class TimerHeapReference;
    105 };
    106 
    107 template <typename TimerFiredClass> class Timer : public TimerBase {
    108 public:
    109     typedef void (TimerFiredClass::*TimerFiredFunction)(Timer*);
    110 
    111     Timer(TimerFiredClass* o, TimerFiredFunction f)
    112         : m_object(o), m_function(f) { }
    113 
    114 private:
    115     virtual void fired() { (m_object->*m_function)(this); }
    116 
    117     TimerFiredClass* m_object;
    118     TimerFiredFunction m_function;
    119 };
    120 
    121 inline bool TimerBase::isActive() const
    122 {
    123     ASSERT(m_thread == currentThread());
    124     return m_nextFireTime;
    125 }
    126 
    127 template <typename TimerFiredClass> class DeferrableOneShotTimer : private TimerBase {
    128 public:
    129     typedef void (TimerFiredClass::*TimerFiredFunction)(DeferrableOneShotTimer*);
    130 
    131     DeferrableOneShotTimer(TimerFiredClass* o, TimerFiredFunction f, double delay)
    132         : m_object(o)
    133         , m_function(f)
    134         , m_delay(delay)
    135         , m_shouldRestartWhenTimerFires(false)
    136     {
    137     }
    138 
    139     void restart()
    140     {
    141         // Setting this boolean is much more efficient than calling startOneShot
    142         // again, which might result in rescheduling the system timer which
    143         // can be quite expensive.
    144 
    145         if (isActive()) {
    146             m_shouldRestartWhenTimerFires = true;
    147             return;
    148         }
    149         startOneShot(m_delay);
    150     }
    151 
    152     using TimerBase::stop;
    153     using TimerBase::isActive;
    154 private:
    155     virtual void fired()
    156     {
    157         if (m_shouldRestartWhenTimerFires) {
    158             m_shouldRestartWhenTimerFires = false;
    159             startOneShot(m_delay);
    160             return;
    161         }
    162 
    163         (m_object->*m_function)(this);
    164     }
    165 
    166     TimerFiredClass* m_object;
    167     TimerFiredFunction m_function;
    168 
    169     double m_delay;
    170     bool m_shouldRestartWhenTimerFires;
    171 };
    172 
    173 }
    174 
    175 #endif
    176