2 * Copyright (C) 2013, 2014 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. ``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.
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>
46 class Worklist
: public RefCounted
<Worklist
> {
48 enum State
{ NotKnown
, Compiling
, Compiled
};
52 static PassRefPtr
<Worklist
> create(CString worklistName
, unsigned numberOfThreads
, int relativePriority
= 0);
54 void enqueue(PassRefPtr
<Plan
>);
56 // This is equivalent to:
57 // worklist->waitUntilAllPlansForVMAreReady(vm);
58 // worklist->completeAllReadyPlansForVM(vm);
59 void completeAllPlansForVM(VM
&);
61 void waitUntilAllPlansForVMAreReady(VM
&);
62 State
completeAllReadyPlansForVM(VM
&, CompilationKey
= CompilationKey());
63 void removeAllReadyPlansForVM(VM
&);
65 State
compilationState(CompilationKey
);
69 void suspendAllThreads();
70 void resumeAllThreads();
72 bool isActiveForVM(VM
&) const;
74 // Only called on the main thread after suspending all threads.
75 void visitWeakReferences(SlotVisitor
&, CodeBlockSet
&);
76 void removeDeadPlans(VM
&);
78 void dump(PrintStream
&) const;
81 Worklist(CString worklistName
);
82 void finishCreation(unsigned numberOfThreads
, int);
84 void runThread(ThreadData
*);
85 static void threadFunction(void* argument
);
87 void removeAllReadyPlansForVM(VM
&, Vector
<RefPtr
<Plan
>, 8>&);
89 void dump(const MutexLocker
&, PrintStream
&) const;
93 // Used to inform the thread about what work there is left to do.
94 Deque
<RefPtr
<Plan
>> m_queue
;
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
100 typedef HashMap
<CompilationKey
, RefPtr
<Plan
>> PlanMap
;
103 // Used to quickly find which plans have been compiled and are ready to
105 Vector
<RefPtr
<Plan
>, 16> m_readyPlans
;
107 Mutex m_suspensionLock
;
109 mutable Mutex m_lock
;
110 ThreadCondition m_planEnqueued
;
111 ThreadCondition m_planCompiled
;
113 Vector
<std::unique_ptr
<ThreadData
>> m_threads
;
114 unsigned m_numberOfActiveThreads
;
117 // For DFGMode compilations.
118 Worklist
* ensureGlobalDFGWorklist();
119 Worklist
* existingGlobalDFGWorklistOrNull();
121 // For FTLMode and FTLForOSREntryMode compilations.
122 Worklist
* ensureGlobalFTLWorklist();
123 Worklist
* existingGlobalFTLWorklistOrNull();
125 Worklist
* ensureGlobalWorklistFor(CompilationMode
);
127 // Simplify doing things for all worklists.
128 inline unsigned numberOfWorklists() { return 2; }
129 inline Worklist
* worklistForIndexOrNull(unsigned index
)
133 return existingGlobalDFGWorklistOrNull();
135 return existingGlobalFTLWorklistOrNull();
137 RELEASE_ASSERT_NOT_REACHED();
142 } } // namespace JSC::DFG
144 #endif // ENABLE(DFG_JIT)
146 #endif // DFGWorklist_h