#pragma once
#include "TaskSource.h"
#include <wtf/Function.h>
#include <wtf/RefCounted.h>
#include <wtf/StdLibExtras.h>
#include <wtf/WeakHashSet.h>
#include <wtf/WeakPtr.h>
namespace WebCore {
class ActiveDOMCallbackMicrotask;
class EventLoopTaskGroup;
class EventTarget;
class Microtask;
class MicrotaskQueue;
class ScriptExecutionContext;
class EventLoopTask {
WTF_MAKE_NONCOPYABLE(EventLoopTask);
WTF_MAKE_FAST_ALLOCATED;
public:
virtual ~EventLoopTask() = default;
TaskSource taskSource() { return m_taskSource; }
virtual void execute() = 0;
EventLoopTaskGroup* group() const { return m_group.get(); }
protected:
EventLoopTask(TaskSource, EventLoopTaskGroup&);
private:
const TaskSource m_taskSource;
WeakPtr<EventLoopTaskGroup> m_group;
};
class EventLoop : public RefCounted<EventLoop>, public CanMakeWeakPtr<EventLoop> {
public:
virtual ~EventLoop() = default;
typedef Function<void ()> TaskFunction;
void queueTask(std::unique_ptr<EventLoopTask>&&);
void queueMicrotask(std::unique_ptr<EventLoopTask>&&);
void performMicrotaskCheckpoint();
virtual MicrotaskQueue& microtaskQueue() = 0;
void resumeGroup(EventLoopTaskGroup&);
void stopGroup(EventLoopTaskGroup&);
protected:
EventLoop() = default;
void run();
void clearAllTasks();
private:
void scheduleToRunIfNeeded();
virtual void scheduleToRun() = 0;
virtual bool isContextThread() const = 0;
Vector<std::unique_ptr<EventLoopTask>> m_tasks;
WeakHashSet<EventLoopTaskGroup> m_groupsWithSuspenedTasks;
bool m_isScheduledToRun { false };
};
class EventLoopTaskGroup : public CanMakeWeakPtr<EventLoopTaskGroup> {
WTF_MAKE_NONCOPYABLE(EventLoopTaskGroup);
WTF_MAKE_FAST_ALLOCATED;
public:
EventLoopTaskGroup(EventLoop& eventLoop)
: m_eventLoop(makeWeakPtr(eventLoop))
{
}
bool hasSameEventLoopAs(EventLoopTaskGroup& otherGroup)
{
ASSERT(m_eventLoop);
return m_eventLoop == otherGroup.m_eventLoop;
}
bool matchesTask(EventLoopTask& task) const
{
auto* group = task.group();
return group == this;
}
void stopAndDiscardAllTasks()
{
m_state = State::Stopped;
if (auto* eventLoop = m_eventLoop.get())
eventLoop->stopGroup(*this);
}
void suspend()
{
ASSERT(m_state != State::Stopped);
m_state = State::Suspended;
}
void resume()
{
ASSERT(m_state != State::Stopped);
m_state = State::Running;
if (auto* eventLoop = m_eventLoop.get())
eventLoop->resumeGroup(*this);
}
bool isStoppedPermanently() { return m_state == State::Stopped; }
bool isSuspended() { return m_state == State::Suspended; }
void queueTask(std::unique_ptr<EventLoopTask>&&);
WEBCORE_EXPORT void queueTask(TaskSource, EventLoop::TaskFunction&&);
WEBCORE_EXPORT void queueMicrotask(EventLoop::TaskFunction&&);
MicrotaskQueue& microtaskQueue() { return m_eventLoop->microtaskQueue(); }
void performMicrotaskCheckpoint();
private:
enum class State : uint8_t { Running, Suspended, Stopped };
WeakPtr<EventLoop> m_eventLoop;
State m_state { State::Running };
};
inline EventLoopTask::EventLoopTask(TaskSource source, EventLoopTaskGroup& group)
: m_taskSource(source)
, m_group(makeWeakPtr(group))
{ }
}