#pragma once
#include <functional>
#include <wtf/Function.h>
#include <wtf/MonotonicTime.h>
#include <wtf/Noncopyable.h>
#include <wtf/Optional.h>
#include <wtf/Seconds.h>
#include <wtf/Threading.h>
#include <wtf/Vector.h>
#if PLATFORM(IOS)
#include "WebCoreThread.h"
#endif
namespace WebCore {
class TimerHeapElement;
class TimerBase {
WTF_MAKE_NONCOPYABLE(TimerBase);
WTF_MAKE_FAST_ALLOCATED;
public:
WEBCORE_EXPORT TimerBase();
WEBCORE_EXPORT virtual ~TimerBase();
WEBCORE_EXPORT void start(Seconds nextFireInterval, Seconds repeatInterval);
void startRepeating(Seconds repeatInterval) { start(repeatInterval, repeatInterval); }
void startOneShot(Seconds interval) { start(interval, 0_s); }
WEBCORE_EXPORT void stop();
bool isActive() const;
Seconds nextFireInterval() const;
Seconds nextUnalignedFireInterval() const;
Seconds repeatInterval() const { return m_repeatInterval; }
void augmentFireInterval(Seconds delta) { setNextFireTime(m_nextFireTime + delta); }
void augmentRepeatInterval(Seconds delta) { augmentFireInterval(delta); m_repeatInterval += delta; }
void didChangeAlignmentInterval();
static void fireTimersInNestedEventLoop();
private:
virtual void fired() = 0;
virtual std::optional<MonotonicTime> alignedFireTime(MonotonicTime) const { return std::nullopt; }
void checkConsistency() const;
void checkHeapIndex() const;
void setNextFireTime(MonotonicTime);
bool inHeap() const { return m_heapIndex != -1; }
bool hasValidHeapPosition() const;
void updateHeapIfNeeded(MonotonicTime 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; }
MonotonicTime m_nextFireTime; MonotonicTime m_unalignedNextFireTime; Seconds m_repeatInterval; int m_heapIndex { -1 }; unsigned m_heapInsertionOrder; bool m_wasDeleted { false };
Vector<TimerBase*>* m_cachedThreadGlobalTimerHeap { nullptr };
Ref<Thread> m_thread { Thread::current() };
friend class ThreadTimers;
friend class TimerHeapLessThanFunction;
friend class TimerHeapReference;
};
class Timer : public TimerBase {
WTF_MAKE_FAST_ALLOCATED;
public:
template <typename TimerFiredClass, typename TimerFiredBaseClass>
Timer(TimerFiredClass& object, void (TimerFiredBaseClass::*function)())
: m_function(std::bind(function, &object))
{
}
Timer(WTF::Function<void ()>&& function)
: m_function(WTFMove(function))
{
}
private:
void fired() override
{
m_function();
}
WTF::Function<void ()> m_function;
};
inline bool TimerBase::isActive() const
{
#if !PLATFORM(IOS)
ASSERT(m_thread.ptr() == &Thread::current());
#else
ASSERT(WebThreadIsCurrent() || pthread_main_np() || m_thread.ptr() == &Thread::current());
#endif // PLATFORM(IOS)
return static_cast<bool>(m_nextFireTime);
}
class DeferrableOneShotTimer : protected TimerBase {
public:
template<typename TimerFiredClass>
DeferrableOneShotTimer(TimerFiredClass& object, void (TimerFiredClass::*function)(), Seconds delay)
: DeferrableOneShotTimer(std::bind(function, &object), delay)
{
}
DeferrableOneShotTimer(WTF::Function<void ()>&& function, Seconds delay)
: m_function(WTFMove(function))
, m_delay(delay)
, m_shouldRestartWhenTimerFires(false)
{
}
void restart()
{
if (isActive()) {
m_shouldRestartWhenTimerFires = true;
return;
}
startOneShot(m_delay);
}
void stop()
{
m_shouldRestartWhenTimerFires = false;
TimerBase::stop();
}
using TimerBase::isActive;
private:
void fired() override
{
if (m_shouldRestartWhenTimerFires) {
m_shouldRestartWhenTimerFires = false;
startOneShot(m_delay);
return;
}
m_function();
}
WTF::Function<void ()> m_function;
Seconds m_delay;
bool m_shouldRestartWhenTimerFires;
};
}