#include "config.h"
#include "RunLoop.h"
#include <QAbstractEventDispatcher>
#include <QCoreApplication>
#include <QMetaMethod>
#include <QMetaObject>
#include <QObject>
#include <QTimerEvent>
namespace WebCore {
class RunLoop::TimerObject : public QObject {
Q_OBJECT
public:
TimerObject(RunLoop* runLoop)
: m_runLoop(runLoop)
, m_pendingPerformWorkInvocations(0)
{
int methodIndex = metaObject()->indexOfMethod("performWork()");
m_method = metaObject()->method(methodIndex);
}
Q_SLOT void performWork() {
m_pendingPerformWorkInvocations++;
if (m_pendingPerformWorkInvocations > 1)
return;
while (m_pendingPerformWorkInvocations) {
m_runLoop->performWork();
m_pendingPerformWorkInvocations--;
}
}
inline void wakeUp() { m_method.invoke(this, Qt::QueuedConnection); }
protected:
virtual void timerEvent(QTimerEvent* event)
{
RunLoop::TimerBase::timerFired(m_runLoop, event->timerId());
}
private:
RunLoop* m_runLoop;
QMetaMethod m_method;
int m_pendingPerformWorkInvocations;
};
static QEventLoop* currentEventLoop;
void RunLoop::run()
{
static bool mainEventLoopIsRunning = false;
if (!mainEventLoopIsRunning) {
mainEventLoopIsRunning = true;
QCoreApplication::exec();
mainEventLoopIsRunning = false;
} else {
QEventLoop eventLoop;
QEventLoop* previousEventLoop = currentEventLoop;
currentEventLoop = &eventLoop;
eventLoop.exec();
currentEventLoop = previousEventLoop;
}
}
void RunLoop::stop()
{
if (currentEventLoop)
currentEventLoop->exit();
else
QCoreApplication::exit();
}
RunLoop::RunLoop()
: m_timerObject(new TimerObject(this))
{
}
RunLoop::~RunLoop()
{
delete m_timerObject;
}
void RunLoop::wakeUp()
{
m_timerObject->wakeUp();
}
void RunLoop::TimerBase::timerFired(RunLoop* runLoop, int ID)
{
TimerMap::iterator it = runLoop->m_activeTimers.find(ID);
ASSERT(it != runLoop->m_activeTimers.end());
TimerBase* timer = it->second;
if (!timer->m_isRepeating) {
runLoop->m_activeTimers.remove(it);
runLoop->m_timerObject->killTimer(timer->m_ID);
timer->m_ID = 0;
}
timer->fired();
}
RunLoop::TimerBase::TimerBase(RunLoop* runLoop)
: m_runLoop(runLoop)
, m_ID(0)
, m_isRepeating(false)
{
}
RunLoop::TimerBase::~TimerBase()
{
stop();
}
void RunLoop::TimerBase::start(double nextFireInterval, bool repeat)
{
stop();
int millis = static_cast<int>(nextFireInterval * 1000);
m_isRepeating = repeat;
m_ID = m_runLoop->m_timerObject->startTimer(millis);
ASSERT(m_ID);
m_runLoop->m_activeTimers.set(m_ID, this);
}
void RunLoop::TimerBase::stop()
{
if (!m_ID)
return;
TimerMap::iterator it = m_runLoop->m_activeTimers.find(m_ID);
if (it == m_runLoop->m_activeTimers.end())
return;
m_runLoop->m_activeTimers.remove(it);
m_runLoop->m_timerObject->killTimer(m_ID);
m_ID = 0;
}
bool RunLoop::TimerBase::isActive() const
{
return m_ID;
}
#include "RunLoopQt.moc"
}