#ifndef Timer_h
#define Timer_h
#include "platform/PlatformExport.h"
#include "platform/TraceLocation.h"
#include "wtf/Noncopyable.h"
#include "wtf/Threading.h"
#include "wtf/Vector.h"
namespace WebCore {
class TimerHeapElement;
class PLATFORM_EXPORT TimerBase {
WTF_MAKE_NONCOPYABLE(TimerBase); WTF_MAKE_FAST_ALLOCATED;
public:
TimerBase();
virtual ~TimerBase();
void start(double nextFireInterval, double repeatInterval, const TraceLocation&);
void startRepeating(double repeatInterval, const TraceLocation& caller)
{
start(repeatInterval, repeatInterval, caller);
}
void startOneShot(double interval, const TraceLocation& caller)
{
start(interval, 0, caller);
}
void stop();
bool isActive() const;
const TraceLocation& location() const { return m_location; }
double nextFireInterval() const;
double nextUnalignedFireInterval() const;
double repeatInterval() const { return m_repeatInterval; }
void augmentRepeatInterval(double delta) {
setNextFireTime(m_nextFireTime + 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;
TraceLocation m_location;
#ifndef NDEBUG
ThreadIdentifier m_thread;
#endif
friend class ThreadTimers;
friend class TimerHeapLessThanFunction;
friend class TimerHeapReference;
};
template <typename TimerFiredClass>
class Timer FINAL : public TimerBase {
public:
typedef void (TimerFiredClass::*TimerFiredFunction)(Timer*);
Timer(TimerFiredClass* o, TimerFiredFunction f)
: m_object(o), m_function(f) { }
private:
virtual void fired() OVERRIDE { (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 FINAL : private 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(const TraceLocation& caller)
{
if (isActive()) {
m_shouldRestartWhenTimerFires = true;
return;
}
startOneShot(m_delay, caller);
}
using TimerBase::stop;
using TimerBase::isActive;
private:
virtual void fired() OVERRIDE
{
if (m_shouldRestartWhenTimerFires) {
m_shouldRestartWhenTimerFires = false;
startOneShot(m_delay, FROM_HERE);
return;
}
(m_object->*m_function)(this);
}
TimerFiredClass* m_object;
TimerFiredFunction m_function;
double m_delay;
bool m_shouldRestartWhenTimerFires;
};
}
#endif