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#pragma once
27
28#if ENABLE(WEBASSEMBLY)
29
30#include <queue>
31
32#include <wtf/AutomaticThread.h>
33#include <wtf/PriorityQueue.h>
34#include <wtf/Vector.h>
35
36namespace JSC {
37
38namespace Wasm {
39
40struct Context;
41class Plan;
42
43class Worklist {
44 WTF_MAKE_FAST_ALLOCATED;
45public:
46 Worklist();
47 ~Worklist();
48
49 JS_EXPORT_PRIVATE void enqueue(Ref<Plan>);
50 void stopAllPlansForContext(Context&);
51
52 JS_EXPORT_PRIVATE void completePlanSynchronously(Plan&);
53
54 enum class Priority {
55 Shutdown,
56 Synchronous,
57 Compilation,
58 Preparation
59 };
60 const char* priorityString(Priority);
61
62private:
63 class Thread;
64 friend class Thread;
65
66 typedef uint64_t Ticket;
67 Ticket nextTicket() { return m_lastGrantedTicket++; }
68
69 struct QueueElement {
70 Priority priority;
71 Ticket ticket;
72 RefPtr<Plan> plan;
73
74 void setToNextPriority();
75 };
76
77 static bool isHigherPriority(const QueueElement& left, const QueueElement& right)
78 {
79 if (left.priority == right.priority)
80 return left.ticket > right.ticket;
81 return left.priority > right.priority;
82 }
83
84 Box<Lock> m_lock;
85 Ref<AutomaticThreadCondition> m_planEnqueued;
86 // Technically, this could overflow but that's unlikely. Even if it did, we will just compile things of the same
87 // Priority it the wrong order, which isn't wrong, just suboptimal.
88 Ticket m_lastGrantedTicket { 0 };
89 PriorityQueue<QueueElement, isHigherPriority, 10> m_queue;
90 Vector<std::unique_ptr<Thread>> m_threads;
91};
92
93Worklist* existingWorklistOrNull();
94JS_EXPORT_PRIVATE Worklist& ensureWorklist();
95
96} } // namespace JSC::Wasm
97
98#endif // ENABLE(WEBASSEMBLY)
99