#ifndef Timer_h
#define Timer_h
#include <wtf/Noncopyable.h>
#include <wtf/Threading.h>
#include <wtf/Vector.h>
namespace WebCore {
class TimerHeapElement;
class TimerBase {
WTF_MAKE_NONCOPYABLE(TimerBase); WTF_MAKE_FAST_ALLOCATED;
public:
TimerBase();
virtual ~TimerBase();
void start(double nextFireInterval, double repeatInterval);
void startRepeating(double repeatInterval) { start(repeatInterval, repeatInterval); }
void startOneShot(double interval) { start(interval, 0); }
void stop();
bool isActive() const;
double nextFireInterval() const;
double nextUnalignedFireInterval() const;
double repeatInterval() const { return m_repeatInterval; }
void augmentFireInterval(double delta) { setNextFireTime(m_nextFireTime + delta); }
void augmentRepeatInterval(double delta) { augmentFireInterval(delta); m_repeatInterval += delta; }
void didChangeAlignmentInterval();
static void fireTimersInNestedEventLoop();
private:
virtual void fired() = 0;
virtual double alignedFireTime(double fireTime) const { return fireTime; }
void checkConsistency() const;
void checkHeapIndex() const;
void setNextFireTime(double);
bool inHeap() const { return m_heapIndex != -1; }
bool hasValidHeapPosition() const;
void updateHeapIfNeeded(double oldTime);
void heapDecreaseKey();
void heapDelete();
void heapDeleteMin();
void heapIncreaseKey();
void heapInsert();
void heapPop();
void heapPopMin();
Vector<TimerBase*>& timerHeap() const { ASSERT(m_cachedThreadGlobalTimerHeap); return *m_cachedThreadGlobalTimerHeap; }
double m_nextFireTime; double m_unalignedNextFireTime; double m_repeatInterval; int m_heapIndex; unsigned m_heapInsertionOrder; Vector<TimerBase*>* m_cachedThreadGlobalTimerHeap;
#ifndef NDEBUG
ThreadIdentifier m_thread;
#endif
friend class ThreadTimers;
friend class TimerHeapLessThanFunction;
friend class TimerHeapReference;
};
template <typename TimerFiredClass> class Timer : public TimerBase {
public:
typedef void (TimerFiredClass::*TimerFiredFunction)(Timer*);
Timer(TimerFiredClass* o, TimerFiredFunction f)
: m_object(o), m_function(f) { }
private:
virtual void fired() { (m_object->*m_function)(this); }
TimerFiredClass* m_object;
TimerFiredFunction m_function;
};
inline bool TimerBase::isActive() const
{
ASSERT(m_thread == currentThread());
return m_nextFireTime;
}
template <typename TimerFiredClass> class DeferrableOneShotTimer : protected TimerBase {
public:
typedef void (TimerFiredClass::*TimerFiredFunction)(DeferrableOneShotTimer*);
DeferrableOneShotTimer(TimerFiredClass* o, TimerFiredFunction f, double delay)
: m_object(o)
, m_function(f)
, m_delay(delay)
, m_shouldRestartWhenTimerFires(false)
{
}
void restart()
{
if (isActive()) {
m_shouldRestartWhenTimerFires = true;
return;
}
startOneShot(m_delay);
}
using TimerBase::stop;
using TimerBase::isActive;
private:
virtual void fired()
{
if (m_shouldRestartWhenTimerFires) {
m_shouldRestartWhenTimerFires = false;
startOneShot(m_delay);
return;
}
(m_object->*m_function)(this);
}
TimerFiredClass* m_object;
TimerFiredFunction m_function;
double m_delay;
bool m_shouldRestartWhenTimerFires;
};
}
#endif