#include "config.h"
#include "Microtasks.h"
#include "CommonVM.h"
#include "EventLoop.h"
#include "WorkerGlobalScope.h"
#include <wtf/MainThread.h>
#include <wtf/NeverDestroyed.h>
#include <wtf/SetForScope.h>
namespace WebCore {
MicrotaskQueue::MicrotaskQueue(JSC::VM& vm)
: m_vm(makeRef(vm))
{
}
MicrotaskQueue::~MicrotaskQueue() = default;
void MicrotaskQueue::append(std::unique_ptr<EventLoopTask>&& task)
{
m_microtaskQueue.append(WTFMove(task));
}
void MicrotaskQueue::performMicrotaskCheckpoint()
{
if (m_performingMicrotaskCheckpoint)
return;
SetForScope<bool> change(m_performingMicrotaskCheckpoint, true);
JSC::JSLockHolder locker(vm());
Vector<std::unique_ptr<EventLoopTask>> toKeep;
while (!m_microtaskQueue.isEmpty()) {
Vector<std::unique_ptr<EventLoopTask>> queue = WTFMove(m_microtaskQueue);
for (auto& task : queue) {
auto* group = task->group();
if (!group || group->isStoppedPermanently())
continue;
if (group->isSuspended())
toKeep.append(WTFMove(task));
else
task->execute();
}
}
vm().finalizeSynchronousJSExecution();
m_microtaskQueue = WTFMove(toKeep);
}
}