]> git.saurik.com Git - apple/javascriptcore.git/blame - dfg/DFGWorklist.h
JavaScriptCore-7600.1.4.15.12.tar.gz
[apple/javascriptcore.git] / dfg / DFGWorklist.h
CommitLineData
81345200
A
1/*
2 * Copyright (C) 2013, 2014 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#ifndef DFGWorklist_h
27#define DFGWorklist_h
28
29#if ENABLE(DFG_JIT)
30
31#include "DFGPlan.h"
32#include "DFGThreadData.h"
33#include <wtf/Deque.h>
34#include <wtf/HashMap.h>
35#include <wtf/Noncopyable.h>
36#include <wtf/PassOwnPtr.h>
37#include <wtf/ThreadingPrimitives.h>
38
39namespace JSC {
40
41class CodeBlockSet;
42class SlotVisitor;
43
44namespace DFG {
45
46class Worklist : public RefCounted<Worklist> {
47public:
48 enum State { NotKnown, Compiling, Compiled };
49
50 ~Worklist();
51
52 static PassRefPtr<Worklist> create(CString worklistName, unsigned numberOfThreads, int relativePriority = 0);
53
54 void enqueue(PassRefPtr<Plan>);
55
56 // This is equivalent to:
57 // worklist->waitUntilAllPlansForVMAreReady(vm);
58 // worklist->completeAllReadyPlansForVM(vm);
59 void completeAllPlansForVM(VM&);
60
61 void waitUntilAllPlansForVMAreReady(VM&);
62 State completeAllReadyPlansForVM(VM&, CompilationKey = CompilationKey());
63 void removeAllReadyPlansForVM(VM&);
64
65 State compilationState(CompilationKey);
66
67 size_t queueLength();
68
69 void suspendAllThreads();
70 void resumeAllThreads();
71
72 bool isActiveForVM(VM&) const;
73
74 // Only called on the main thread after suspending all threads.
75 void visitWeakReferences(SlotVisitor&, CodeBlockSet&);
76 void removeDeadPlans(VM&);
77
78 void dump(PrintStream&) const;
79
80private:
81 Worklist(CString worklistName);
82 void finishCreation(unsigned numberOfThreads, int);
83
84 void runThread(ThreadData*);
85 static void threadFunction(void* argument);
86
87 void removeAllReadyPlansForVM(VM&, Vector<RefPtr<Plan>, 8>&);
88
89 void dump(const MutexLocker&, PrintStream&) const;
90
91 CString m_threadName;
92
93 // Used to inform the thread about what work there is left to do.
94 Deque<RefPtr<Plan>> m_queue;
95
96 // Used to answer questions about the current state of a code block. This
97 // is particularly great for the cti_optimize OSR slow path, which wants
98 // to know: did I get here because a better version of me just got
99 // compiled?
100 typedef HashMap<CompilationKey, RefPtr<Plan>> PlanMap;
101 PlanMap m_plans;
102
103 // Used to quickly find which plans have been compiled and are ready to
104 // be completed.
105 Vector<RefPtr<Plan>, 16> m_readyPlans;
106
107 Mutex m_suspensionLock;
108
109 mutable Mutex m_lock;
110 ThreadCondition m_planEnqueued;
111 ThreadCondition m_planCompiled;
112
113 Vector<std::unique_ptr<ThreadData>> m_threads;
114 unsigned m_numberOfActiveThreads;
115};
116
117// For DFGMode compilations.
118Worklist* ensureGlobalDFGWorklist();
119Worklist* existingGlobalDFGWorklistOrNull();
120
121// For FTLMode and FTLForOSREntryMode compilations.
122Worklist* ensureGlobalFTLWorklist();
123Worklist* existingGlobalFTLWorklistOrNull();
124
125Worklist* ensureGlobalWorklistFor(CompilationMode);
126
127// Simplify doing things for all worklists.
128inline unsigned numberOfWorklists() { return 2; }
129inline Worklist* worklistForIndexOrNull(unsigned index)
130{
131 switch (index) {
132 case 0:
133 return existingGlobalDFGWorklistOrNull();
134 case 1:
135 return existingGlobalFTLWorklistOrNull();
136 default:
137 RELEASE_ASSERT_NOT_REACHED();
138 return 0;
139 }
140}
141
142} } // namespace JSC::DFG
143
144#endif // ENABLE(DFG_JIT)
145
146#endif // DFGWorklist_h
147