1/*
2 * Copyright (C) 2017 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
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.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "WasmWorklist.h"
28
29#if ENABLE(WEBASSEMBLY)
30
31#include "CPU.h"
32#include "WasmPlan.h"
33
34namespace JSC { namespace Wasm {
35
36namespace WasmWorklistInternal {
37static const bool verbose = false;
38}
39
40const char* Worklist::priorityString(Priority priority)
41{
42 switch (priority) {
43 case Priority::Preparation: return "Preparation";
44 case Priority::Shutdown: return "Shutdown";
45 case Priority::Compilation: return "Compilation";
46 case Priority::Synchronous: return "Synchronous";
47 }
48 RELEASE_ASSERT_NOT_REACHED();
49}
50
51// The Thread class is designed to prevent threads from blocking when there is still work
52// in the queue. Wasm's Plans have some phases, Validiation, Preparation, and Completion,
53// that can only be done by one thread, and another phase, Compilation, that can be done
54// many threads. In order to stop a thread from wasting time we remove any plan that is
55// is currently in a single threaded state from the work queue so other plans can run.
56class Worklist::Thread final : public AutomaticThread {
57public:
58 using Base = AutomaticThread;
59 Thread(const AbstractLocker& locker, Worklist& work)
60 : Base(locker, work.m_lock, work.m_planEnqueued.copyRef())
61 , worklist(work)
62 {
63
64 }
65
66protected:
67 PollResult poll(const AbstractLocker&) override
68 {
69 auto& queue = worklist.m_queue;
70 synchronize.notifyAll();
71
72 while (!queue.isEmpty()) {
73 Priority priority = queue.peek().priority;
74 if (priority == Worklist::Priority::Shutdown)
75 return PollResult::Stop;
76
77 element = queue.peek();
78 // Only one thread should validate/prepare.
79 if (!queue.peek().plan->multiThreaded())
80 queue.dequeue();
81
82 if (element.plan->hasWork())
83 return PollResult::Work;
84
85 // There must be a another thread linking this plan so we can deque and see if there is other work.
86 queue.dequeue();
87 element = QueueElement();
88 }
89 return PollResult::Wait;
90 }
91
92 WorkResult work() override
93 {
94 auto complete = [&] (const AbstractLocker&) {
95 // We need to hold the lock to release our plan otherwise the main thread, while canceling plans
96 // might use after free the plan we are looking at
97 element = QueueElement();
98 return WorkResult::Continue;
99 };
100
101 Plan* plan = element.plan.get();
102 ASSERT(plan);
103
104 bool wasMultiThreaded = plan->multiThreaded();
105 plan->work(Plan::Partial);
106
107 ASSERT(!plan->hasWork() || plan->multiThreaded());
108 if (plan->hasWork() && !wasMultiThreaded && plan->multiThreaded()) {
109 LockHolder locker(*worklist.m_lock);
110 element.setToNextPriority();
111 worklist.m_queue.enqueue(WTFMove(element));
112 worklist.m_planEnqueued->notifyAll(locker);
113 return complete(locker);
114 }
115
116 return complete(holdLock(*worklist.m_lock));
117 }
118
119 const char* name() const override
120 {
121 return "Wasm Worklist Helper Thread";
122 }
123
124public:
125 Condition synchronize;
126 Worklist& worklist;
127 // We can only modify element when holding the lock. A synchronous compile might look at each thread's tasks in order to boost the priority.
128 QueueElement element;
129};
130
131void Worklist::QueueElement::setToNextPriority()
132{
133 switch (priority) {
134 case Priority::Preparation:
135 priority = Priority::Compilation;
136 return;
137 case Priority::Synchronous:
138 return;
139 default:
140 break;
141 }
142 RELEASE_ASSERT_NOT_REACHED();
143}
144
145void Worklist::enqueue(Ref<Plan> plan)
146{
147 LockHolder locker(*m_lock);
148
149 if (!ASSERT_DISABLED) {
150 for (const auto& element : m_queue)
151 ASSERT_UNUSED(element, element.plan.get() != &plan.get());
152 }
153
154 dataLogLnIf(WasmWorklistInternal::verbose, "Enqueuing plan");
155 m_queue.enqueue({ Priority::Preparation, nextTicket(), WTFMove(plan) });
156 m_planEnqueued->notifyOne(locker);
157}
158
159void Worklist::completePlanSynchronously(Plan& plan)
160{
161 {
162 LockHolder locker(*m_lock);
163 m_queue.decreaseKey([&] (QueueElement& element) {
164 if (element.plan == &plan) {
165 element.priority = Priority::Synchronous;
166 return true;
167 }
168 return false;
169 });
170
171 for (auto& thread : m_threads) {
172 if (thread->element.plan == &plan)
173 thread->element.priority = Priority::Synchronous;
174 }
175 }
176
177 plan.waitForCompletion();
178}
179
180void Worklist::stopAllPlansForContext(Context& context)
181{
182 LockHolder locker(*m_lock);
183 Vector<QueueElement> elements;
184 while (!m_queue.isEmpty()) {
185 QueueElement element = m_queue.dequeue();
186 bool didCancel = element.plan->tryRemoveContextAndCancelIfLast(context);
187 if (!didCancel)
188 elements.append(WTFMove(element));
189 }
190
191 for (auto& element : elements)
192 m_queue.enqueue(WTFMove(element));
193
194 for (auto& thread : m_threads) {
195 if (thread->element.plan) {
196 bool didCancel = thread->element.plan->tryRemoveContextAndCancelIfLast(context);
197 if (didCancel) {
198 // We don't have to worry about the deadlocking since the thread can't block without checking for a new plan and must hold the lock to do so.
199 thread->synchronize.wait(*m_lock);
200 }
201 }
202 }
203}
204
205Worklist::Worklist()
206 : m_lock(Box<Lock>::create())
207 , m_planEnqueued(AutomaticThreadCondition::create())
208{
209 unsigned numberOfCompilationThreads = Options::useConcurrentJIT() ? kernTCSMAwareNumberOfProcessorCores() : 1;
210 m_threads.reserveCapacity(numberOfCompilationThreads);
211 LockHolder locker(*m_lock);
212 for (unsigned i = 0; i < numberOfCompilationThreads; i++)
213 m_threads.uncheckedAppend(std::make_unique<Worklist::Thread>(locker, *this));
214}
215
216Worklist::~Worklist()
217{
218 {
219 LockHolder locker(*m_lock);
220 m_queue.enqueue({ Priority::Shutdown, nextTicket(), nullptr });
221 m_planEnqueued->notifyAll(locker);
222 }
223 for (unsigned i = 0; i < m_threads.size(); ++i)
224 m_threads[i]->join();
225}
226
227static Worklist* globalWorklist;
228
229Worklist* existingWorklistOrNull() { return globalWorklist; }
230Worklist& ensureWorklist()
231{
232 static std::once_flag initializeWorklist;
233 std::call_once(initializeWorklist, [] {
234 Worklist* worklist = new Worklist();
235 WTF::storeStoreFence();
236 globalWorklist = worklist;
237 });
238 return *globalWorklist;
239}
240
241} } // namespace JSC::Wasm
242
243#endif // ENABLE(WEBASSEMBLY)
244