This source file includes following definitions.
- mainThreadSharedTimer
- m_pendingSharedTimerFireTime
- setSharedTimer
- updateSharedTimer
- sharedTimerFired
- sharedTimerFiredInternal
- fireTimersInNestedEventLoop
#include "config.h"
#include "platform/ThreadTimers.h"
#include "platform/SharedTimer.h"
#include "platform/PlatformThreadData.h"
#include "platform/Timer.h"
#include "platform/TraceEvent.h"
#include "wtf/CurrentTime.h"
#include "wtf/MainThread.h"
using namespace std;
namespace WebCore {
static const double maxDurationOfFiringTimers = 0.050;
static MainThreadSharedTimer* mainThreadSharedTimer()
{
static MainThreadSharedTimer* timer = new MainThreadSharedTimer;
return timer;
}
ThreadTimers::ThreadTimers()
: m_sharedTimer(0)
, m_firingTimers(false)
, m_pendingSharedTimerFireTime(0)
{
if (isMainThread())
setSharedTimer(mainThreadSharedTimer());
}
void ThreadTimers::setSharedTimer(SharedTimer* sharedTimer)
{
if (m_sharedTimer) {
m_sharedTimer->setFiredFunction(0);
m_sharedTimer->stop();
m_pendingSharedTimerFireTime = 0;
}
m_sharedTimer = sharedTimer;
if (sharedTimer) {
m_sharedTimer->setFiredFunction(ThreadTimers::sharedTimerFired);
updateSharedTimer();
}
}
void ThreadTimers::updateSharedTimer()
{
if (!m_sharedTimer)
return;
if (m_firingTimers || m_timerHeap.isEmpty()) {
m_pendingSharedTimerFireTime = 0;
m_sharedTimer->stop();
} else {
double nextFireTime = m_timerHeap.first()->m_nextFireTime;
double currentMonotonicTime = monotonicallyIncreasingTime();
if (m_pendingSharedTimerFireTime) {
if (m_pendingSharedTimerFireTime <= currentMonotonicTime && nextFireTime <= currentMonotonicTime)
return;
}
m_pendingSharedTimerFireTime = nextFireTime;
m_sharedTimer->setFireInterval(max(nextFireTime - currentMonotonicTime, 0.0));
}
}
void ThreadTimers::sharedTimerFired()
{
TRACE_EVENT_SET_SAMPLING_STATE("Blink", "BlinkInternal");
PlatformThreadData::current().threadTimers().sharedTimerFiredInternal();
TRACE_EVENT_SET_SAMPLING_STATE("Blink", "Sleeping");
}
void ThreadTimers::sharedTimerFiredInternal()
{
if (m_firingTimers)
return;
m_firingTimers = true;
m_pendingSharedTimerFireTime = 0;
double fireTime = monotonicallyIncreasingTime();
double timeToQuit = fireTime + maxDurationOfFiringTimers;
while (!m_timerHeap.isEmpty() && m_timerHeap.first()->m_nextFireTime <= fireTime) {
TimerBase& timer = *m_timerHeap.first();
timer.m_nextFireTime = 0;
timer.m_unalignedNextFireTime = 0;
timer.heapDeleteMin();
double interval = timer.repeatInterval();
timer.setNextFireTime(interval ? fireTime + interval : 0);
TRACE_EVENT2("blink", "ThreadTimers::sharedTimerFiredInternal",
"src_file", timer.location().fileName(),
"src_func", timer.location().functionName());
timer.fired();
if (!m_firingTimers || timeToQuit < monotonicallyIncreasingTime())
break;
}
m_firingTimers = false;
updateSharedTimer();
}
void ThreadTimers::fireTimersInNestedEventLoop()
{
m_firingTimers = false;
updateSharedTimer();
}
}