Debian Codegraph @

js/src/vm/OffThreadPromiseRuntimeState.h

raw
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 #ifndef vm_OffThreadPromiseRuntimeState_h
8 #define vm_OffThreadPromiseRuntimeState_h
9
10 #include <stddef.h> // size_t
11
12 #include "jstypes.h" // JS_PUBLIC_API
13
14 #include "ds/Fifo.h" // js::Fifo
15 #include "ds/PriorityQueue.h" // js::PriorityQueue
16 #include "js/AllocPolicy.h" // js::SystemAllocPolicy
17 #include "js/HashTable.h" // js::DefaultHasher, js::HashSet
18 #include "js/Promise.h" // JS::Dispatchable, JS::Dispatchable::MaybeShuttingDown,
19 // JS::DispatchToEventLoopCallback,
20 // JS::DelayedDispatchToEventLoopCallback
21 #include "js/RootingAPI.h" // JS::Handle, JS::PersistentRooted
22 #include "threading/ConditionVariable.h" // js::ConditionVariable
23 #include "vm/PromiseObject.h" // js::PromiseObject
24
25 struct JS_PUBLIC_API JSContext;
26 struct JS_PUBLIC_API JSRuntime;
27
28 namespace js {
29
30 class AutoLockHelperThreadState;
31 class OffThreadPromiseRuntimeState;
32
33 // [SMDOC] OffThreadPromiseTask: an off-main-thread task that resolves a promise
34 //
35 // An OffThreadPromiseTask is an abstract base class holding a JavaScript
36 // promise that will be resolved (fulfilled or rejected) with the results of a
37 // task possibly performed by some other thread. OffThreadPromiseTasks can be
38 // undispatched (meaning that they can be dropped if the JSContext owning the
39 // promise shuts down before the tasks resolves) or dispatched (meaning that
40 // shutdown should wait for the task to join).
41 //
42 // An OffThreadPromiseTask's lifecycle is as follows:
43 //
44 // - Some JavaScript native wishes to return a promise of the result of some
45 // computation that might be performed by other threads (say, helper threads
46 // or the embedding's I/O threads), so it creates a PromiseObject to represent
47 // the result, and an OffThreadPromiseTask referring to it. After handing the
48 // OffThreadPromiseTask to the code doing the actual work, the native is free
49 // to return the PromiseObject to its caller.
50 //
51 // - When the computation is done, successfully or otherwise, it populates the
52 // OffThreadPromiseTask—which is actually an instance of some concrete
53 // subclass specific to the task—with the information needed to resolve the
54 // promise, and calls OffThreadPromiseTask::dispatchResolveAndDestroy. This
55 // enqueues a runnable on the JavaScript thread to which the promise belongs.
56 //
57 // - When it gets around to the runnable, the JavaScript thread calls the
58 // OffThreadPromiseTask's `resolve` method, which the concrete subclass has
59 // overriden to resolve the promise appropriately. This probably enqueues a
60 // promise reaction job.
61 //
62 // - The JavaScript thread then deletes the OffThreadPromiseTask.
63 //
64 // During shutdown, the process is slightly different. Enqueuing runnables to
65 // the JavaScript thread begins to fail. Undispatched tasks are immediately
66 // destroyed. JSRuntime shutdown waits for all outstanding dispatched tasks
67 // to call their run methods, and then deletes them on the main thread,
68 // without calling `resolve`.
69 //
70 // For example, the JavaScript function WebAssembly.compile uses
71 // OffThreadPromiseTask to manage the result of a helper thread task, accepting
72 // binary WebAssembly code and returning a promise of a compiled
73 // WebAssembly.Module. It would like to do this compilation work on a helper
74 // thread. When called by JavaScript, WebAssembly.compile creates a promise,
75 // builds a CompileBufferTask (the OffThreadPromiseTask concrete subclass) to
76 // keep track of it, and then hands that to a helper thread. When the helper
77 // thread is done, successfully or otherwise, it calls the CompileBufferTask's
78 // dispatchResolveAndDestroy method, which enqueues a runnable to the JavaScript
79 // thread to resolve the promise and delete the CompileBufferTask.
80 // (CompileBufferTask actually implements PromiseHelperTask, which implements
81 // OffThreadPromiseTask; PromiseHelperTask is what our helper thread scheduler
82 // requires.)
83 //
84 // OffThreadPromiseTasks are not limited to use with helper threads. For
85 // example, a function returning a promise of the result of a network operation
86 // could provide the code collecting the incoming data with an
87 // OffThreadPromiseTask for the promise, and let the embedding's network I/O
88 // threads call dispatchResolveAndDestroy.
89 //
90 // OffThreadPromiseTask may also be used purely on the main thread, as a way to
91 // "queue a task" in HTML terms. Note that a "task" is not the same as a
92 // "microtask" and there are separate queues for tasks and microtasks that are
93 // drained at separate times in the browser. The task queue is implemented by
94 // the browser's main event loop. The microtask queue is implemented
95 // by JS::JobQueue, used for promises and gets drained before returning to
96 // the event loop. Thus OffThreadPromiseTask can only be used when the spec
97 // says "queue a task", as the WebAssembly APIs do. In some cases, like
98 // Atomics.waitAsync, the choice between queuing a task or a microtask depends
99 // on whether the promise is being resolved from the owning thread or another
100 // thread. In such cases, ExtractAndForget can be used from the owning thread to
101 // cancel the task and return the underlying promise, which can then be resolved
102 // the normal way.
103 //
104 // An OffThreadPromiseTask has a JSContext, and must be constructed and have its
105 // 'init' method called on that JSContext's thread. Once
106 // initialized, its dispatchResolveAndDestroy method may be called from any
107 // thread. Other than calling `ExtractAndForget`, or `DestroyUndispatchedTask`
108 // during shutdown, this is the only safe way to destruct an
109 // OffThreadPromiseTask; doing so ensures the OffThreadPromiseTask's destructor
110 // will run on the JSContext's thread, either from the event loop or during
111 // shutdown.
112 //
113 // OffThreadPromiseTask::dispatchResolveAndDestroy uses the
114 // JS::DispatchToEventLoopCallback provided by the embedding to enqueue
115 // runnables on the JavaScript thread. See the comments for
116 // DispatchToEventLoopCallback for details.
117
118 class OffThreadPromiseTask : public JS::Dispatchable {
119 friend class OffThreadPromiseRuntimeState;
120
121 JSRuntime* runtime_;
122 JS::PersistentRooted<PromiseObject*> promise_;
123
124 // The registered_ flag indicates that this task is counted as part of
125 // numRegistered_ of OffThreadPromiseRuntimeState, which will wait untill
126 // all registered tasks have been run or destroyed.
127 bool registered_;
128
129 // Indicates that this is an undispatched cancellable task, which is a member
130 // of the Cancellable list. If cancellable is set to false, we can no longer
131 // terminate the task early, this means it is no longer tracked by the
132 // Cancellable list, and a dispatch has been attempted.
133 bool cancellable_;
134
135 void operator=(const OffThreadPromiseTask&) = delete;
136 OffThreadPromiseTask(const OffThreadPromiseTask&) = delete;
137
138 void unregister(OffThreadPromiseRuntimeState& state);
139
140 protected:
141 OffThreadPromiseTask(JSContext* cx, JS::Handle<PromiseObject*> promise);
142
143 // To be called by OffThreadPromiseTask and implemented by the derived class.
144 virtual bool resolve(JSContext* cx, JS::Handle<PromiseObject*> promise) {
145 MOZ_CRASH("Tasks should override resolve");
146 };
147
148 // JS::Dispatchable override implementation.
149 // Runs the task, and ends with 'js_delete(this)'.
150 void run(JSContext* cx, MaybeShuttingDown maybeShuttingDown) final;
151
152 // JS::Dispatchable override implementation, moves ownership to
153 // OffThreadPromiseRuntimeState's failed_ list, for cleanup on shutdown.
154 void transferToRuntime() final;
155
156 // To be called by `destroy` during shutdown and implemented by the derived
157 // class (for undispatched tasks only). Gives the task a chance to clean up
158 // before being deleted.
159 virtual void prepareForCancel() {
160 MOZ_CRASH("Undispatched tasks should override prepareForCancel");
161 }
162
163 public:
164 ~OffThreadPromiseTask() override;
165 static void DestroyUndispatchedTask(OffThreadPromiseTask* task);
166
167 JSRuntime* runtime() { return runtime_; }
168
169 // Calling `init` on an OffThreadPromiseTask informs the runtime that it must
170 // wait on shutdown for this task to rejoin the active JSContext by calling
171 // dispatchResolveAndDestroy().
172 bool init(JSContext* cx);
173 bool init(JSContext* cx, const AutoLockHelperThreadState& lock);
174
175 // Cancellable initialization track the task in case it needs to be aborted
176 // before it is dispatched. Cancellable tasks are owned by the cancellable_
177 // hashMap until they are dispatched.
178 static bool InitCancellable(JSContext* cx,
179 js::UniquePtr<OffThreadPromiseTask>&& task);
180 static bool InitCancellable(JSContext* cx,
181 const AutoLockHelperThreadState& lock,
182 js::UniquePtr<OffThreadPromiseTask>&& task);
183
184 // Remove the cancellable task from the runtime cancellable list and
185 // call DispatchResolveAndDestroy with a newly created UniquePtr,
186 // so that ownership moves to the embedding.
187 //
188 // If a task is never removed from the cancellable list, it is deleted on
189 // shutdown without running.
190 void removeFromCancellableListAndDispatch();
191 void removeFromCancellableListAndDispatch(
192 const AutoLockHelperThreadState& lock);
193
194 // These first two methods will wrap a pointer in a uniquePtr for the purpose
195 // of passing it's ownership eventually to the embedding. These are used by
196 // WASM and PromiseHelperTask.
197 void dispatchResolveAndDestroy();
198 void dispatchResolveAndDestroy(const AutoLockHelperThreadState& lock);
199
200 // An initialized OffThreadPromiseTask can be dispatched to an active
201 // JSContext of its Promise's JSRuntime from any thread. Normally, this will
202 // lead to resolve() being called on JSContext thread, given the Promise.
203 // However, if shutdown interrupts, resolve() may not be called, though the
204 // OffThreadPromiseTask will be destroyed on a JSContext thread.
205 static void DispatchResolveAndDestroy(
206 js::UniquePtr<OffThreadPromiseTask>&& task);
207 static void DispatchResolveAndDestroy(
208 js::UniquePtr<OffThreadPromiseTask>&& task,
209 const AutoLockHelperThreadState& lock);
210
211 static PromiseObject* ExtractAndForget(OffThreadPromiseTask* task,
212 const AutoLockHelperThreadState& lock);
213 };
214
215 using OffThreadPromiseTaskSet =
216 HashSet<OffThreadPromiseTask*, DefaultHasher<OffThreadPromiseTask*>,
217 SystemAllocPolicy>;
218
219 using DispatchableFifo =
220 Fifo<js::UniquePtr<JS::Dispatchable>, 0, SystemAllocPolicy>;
221
222 class DelayedDispatchable {
223 js::UniquePtr<JS::Dispatchable> dispatchable_;
224 mozilla::TimeStamp endTime_;
225
226 public:
227 DelayedDispatchable(DelayedDispatchable&& other)
228 : dispatchable_(other.dispatchable()), endTime_(other.endTime()) {}
229
230 DelayedDispatchable(js::UniquePtr<JS::Dispatchable>&& dispatchable,
231 mozilla::TimeStamp endTime)
232 : dispatchable_(std::move(dispatchable)), endTime_(endTime) {}
233
234 void operator=(DelayedDispatchable&& other) {
235 dispatchable_ = other.dispatchable();
236 endTime_ = other.endTime();
237 }
238 js::UniquePtr<JS::Dispatchable> dispatchable() {
239 return std::move(dispatchable_);
240 }
241 mozilla::TimeStamp endTime() const { return endTime_; }
242
243 static bool higherPriority(const DelayedDispatchable& a,
244 const DelayedDispatchable& b) {
245 return a.endTime_ < b.endTime_;
246 }
247 };
248
249 using DelayedDispatchablePriorityQueue =
250 PriorityQueue<DelayedDispatchable, DelayedDispatchable, 0,
251 SystemAllocPolicy>;
252
253 class OffThreadPromiseRuntimeState {
254 friend class OffThreadPromiseTask;
255
256 // These fields are initialized once before any off-thread usage and thus do
257 // not require a lock.
258 JS::DispatchToEventLoopCallback dispatchToEventLoopCallback_;
259 JS::DelayedDispatchToEventLoopCallback delayedDispatchToEventLoopCallback_;
260 void* dispatchToEventLoopClosure_;
261
262 // A set of all OffThreadPromiseTasks that have successfully called 'init'.
263 // This set doesn't own tasks. OffThreadPromiseTask's destructor removes them
264 // from the set.
265 HelperThreadLockData<size_t> numRegistered_;
266
267 // The cancellable hashmap tracks the registered, but thusfar
268 // undispatched tasks. Not all undispatched tasks are cancellable, namely
269 // webassembly helpers are held in an undispatched state, but are not
270 // cancellable. Until the task is dispatched, this hashmap acts as the owner
271 // of the task. In order to place something in the cancellable list, use
272 // InitCancellable. Any task owned by the cancellable list cannot be
273 // dispatched using DispatchResolveAndDestroy. Instead, use the method on
274 // OffThreadPromiseTask removeFromCancellableAndDispatch.
275 HelperThreadLockData<OffThreadPromiseTaskSet> cancellable_;
276
277 // This list owns tasks that have failed to dispatch or failed to execute.
278 // The list is cleared on shutdown.
279 HelperThreadLockData<DispatchableFifo> failed_;
280
281 // The allFailed_ condition is waited on and notified during engine
282 // shutdown, communicating when all off-thread tasks in failed_ are safe to be
283 // destroyed from the (shutting down) main thread. This condition is met when
284 // numRegistered_ == failed().count(), where the collection of failed tasks
285 // mean "the DispatchToEventLoopCallback failed after this task was dispatched
286 // for execution".
287 HelperThreadLockData<ConditionVariable> allFailed_;
288 HelperThreadLockData<size_t> numFailed_;
289
290 // The queue of JS::Dispatchables used by the DispatchToEventLoopCallback that
291 // calling js::UseInternalJobQueues installs.
292 HelperThreadLockData<DispatchableFifo> internalDispatchQueue_;
293 HelperThreadLockData<ConditionVariable> internalDispatchQueueAppended_;
294 HelperThreadLockData<bool> internalDispatchQueueClosed_;
295 HelperThreadLockData<DelayedDispatchablePriorityQueue>
296 internalDelayedDispatchPriorityQueue_;
297
298 ConditionVariable& allFailed() { return allFailed_.ref(); }
299
300 DispatchableFifo& failed() { return failed_.ref(); }
301 OffThreadPromiseTaskSet& cancellable() { return cancellable_.ref(); }
302
303 DispatchableFifo& internalDispatchQueue() {
304 return internalDispatchQueue_.ref();
305 }
306 ConditionVariable& internalDispatchQueueAppended() {
307 return internalDispatchQueueAppended_.ref();
308 }
309 DelayedDispatchablePriorityQueue& internalDelayedDispatchPriorityQueue() {
310 return internalDelayedDispatchPriorityQueue_.ref();
311 }
312
313 void dispatchDelayedTasks();
314
315 static bool internalDispatchToEventLoop(void*,
316 js::UniquePtr<JS::Dispatchable>&&);
317 static bool internalDelayedDispatchToEventLoop(
318 void*, js::UniquePtr<JS::Dispatchable>&&, uint32_t);
319 bool usingInternalDispatchQueue() const;
320
321 void operator=(const OffThreadPromiseRuntimeState&) = delete;
322 OffThreadPromiseRuntimeState(const OffThreadPromiseRuntimeState&) = delete;
323
324 public:
325 OffThreadPromiseRuntimeState();
326 ~OffThreadPromiseRuntimeState();
327 void init(JS::DispatchToEventLoopCallback callback,
328 JS::DelayedDispatchToEventLoopCallback delayCallback,
329 void* closure);
330 void initInternalDispatchQueue();
331 bool initialized() const;
332
333 // If initInternalDispatchQueue() was called, internalDrain() can be
334 // called to periodically drain the dispatch queue before shutdown.
335 void internalDrain(JSContext* cx);
336 bool internalHasPending();
337 bool internalHasPending(AutoLockHelperThreadState& lock);
338
339 void stealFailedTask(JS::Dispatchable* dispatchable);
340
341 bool dispatchToEventLoop(js::UniquePtr<JS::Dispatchable>&& dispatchable);
342 bool delayedDispatchToEventLoop(
343 js::UniquePtr<JS::Dispatchable>&& dispatchable, uint32_t delay);
344
345 // shutdown() must be called by the JSRuntime while the JSRuntime is valid.
346 void shutdown(JSContext* cx);
347 };
348
349 } // namespace js
350
351 #endif // vm_OffThreadPromiseRuntimeState_h