2
* Copyright (C) 2019 Apple Inc. All rights reserved.
4
* Redistribution and use in source and binary forms, with or without
5
* modification, are permitted provided that the following conditions
7
* 1. Redistributions of source code must retain the above copyright
8
* notice, this list of conditions and the following disclaimer.
9
* 2. Redistributions in binary form must reproduce the above copyright
10
* notice, this list of conditions and the following disclaimer in the
11
* documentation and/or other materials provided with the distribution.
13
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23
* THE POSSIBILITY OF SUCH DAMAGE.
27
#include "EventLoop.h"
29
#include "Microtasks.h"
33
void EventLoop::queueTask(std::unique_ptr<EventLoopTask>&& task)
35
ASSERT(task->taskSource() != TaskSource::Microtask);
36
ASSERT(task->group());
37
ASSERT(isContextThread());
38
scheduleToRunIfNeeded();
39
m_tasks.append(WTFMove(task));
42
void EventLoop::queueMicrotask(std::unique_ptr<EventLoopTask>&& microtask)
44
ASSERT(microtask->taskSource() == TaskSource::Microtask);
45
microtaskQueue().append(WTFMove(microtask));
46
scheduleToRunIfNeeded(); // FIXME: Remove this once everything is integrated with the event loop.
49
void EventLoop::performMicrotaskCheckpoint()
51
microtaskQueue().performMicrotaskCheckpoint();
54
void EventLoop::resumeGroup(EventLoopTaskGroup& group)
56
ASSERT(isContextThread());
57
if (!m_groupsWithSuspenedTasks.contains(group))
59
scheduleToRunIfNeeded();
62
void EventLoop::stopGroup(EventLoopTaskGroup& group)
64
ASSERT(isContextThread());
65
m_tasks.removeAllMatching([&group] (auto& task) {
66
return group.matchesTask(*task);
70
void EventLoop::scheduleToRunIfNeeded()
72
if (m_isScheduledToRun)
74
m_isScheduledToRun = true;
80
m_isScheduledToRun = false;
81
bool didPerformMicrotaskCheckpoint = false;
83
if (!m_tasks.isEmpty()) {
84
auto tasks = std::exchange(m_tasks, { });
85
m_groupsWithSuspenedTasks.clear();
86
Vector<std::unique_ptr<EventLoopTask>> remainingTasks;
87
for (auto& task : tasks) {
88
auto* group = task->group();
89
if (!group || group->isStoppedPermanently())
92
if (group->isSuspended()) {
93
m_groupsWithSuspenedTasks.add(group);
94
remainingTasks.append(WTFMove(task));
99
didPerformMicrotaskCheckpoint = true;
100
microtaskQueue().performMicrotaskCheckpoint();
102
for (auto& task : m_tasks)
103
remainingTasks.append(WTFMove(task));
104
m_tasks = WTFMove(remainingTasks);
107
// FIXME: Remove this once everything is integrated with the event loop.
108
if (!didPerformMicrotaskCheckpoint)
109
microtaskQueue().performMicrotaskCheckpoint();
112
void EventLoop::clearAllTasks()
115
m_groupsWithSuspenedTasks.clear();
118
void EventLoopTaskGroup::queueTask(std::unique_ptr<EventLoopTask>&& task)
120
if (m_state == State::Stopped || !m_eventLoop)
122
ASSERT(task->group() == this);
123
m_eventLoop->queueTask(WTFMove(task));
126
class EventLoopFunctionDispatchTask : public EventLoopTask {
128
EventLoopFunctionDispatchTask(TaskSource source, EventLoopTaskGroup& group, EventLoop::TaskFunction&& function)
129
: EventLoopTask(source, group)
130
, m_function(WTFMove(function))
134
void execute() final { m_function(); }
137
EventLoop::TaskFunction m_function;
140
void EventLoopTaskGroup::queueTask(TaskSource source, EventLoop::TaskFunction&& function)
142
return queueTask(makeUnique<EventLoopFunctionDispatchTask>(source, *this, WTFMove(function)));
145
void EventLoopTaskGroup::queueMicrotask(EventLoop::TaskFunction&& function)
147
if (m_state == State::Stopped || !m_eventLoop)
149
m_eventLoop->queueMicrotask(makeUnique<EventLoopFunctionDispatchTask>(TaskSource::Microtask, *this, WTFMove(function)));
152
void EventLoopTaskGroup::performMicrotaskCheckpoint()
155
m_eventLoop->performMicrotaskCheckpoint();
158
} // namespace WebCore