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/ThreadingPrimitives.h>
45 class Worklist
: public RefCounted
<Worklist
> {
47 enum State
{ NotKnown
, Compiling
, Compiled
};
51 static Ref
<Worklist
> create(CString worklistName
, unsigned numberOfThreads
, int relativePriority
= 0);
53 void enqueue(PassRefPtr
<Plan
>);
55 // This is equivalent to:
56 // worklist->waitUntilAllPlansForVMAreReady(vm);
57 // worklist->completeAllReadyPlansForVM(vm);
58 void completeAllPlansForVM(VM
&);
60 void waitUntilAllPlansForVMAreReady(VM
&);
61 State
completeAllReadyPlansForVM(VM
&, CompilationKey
= CompilationKey());
62 void removeAllReadyPlansForVM(VM
&);
64 State
compilationState(CompilationKey
);
68 void suspendAllThreads();
69 void resumeAllThreads();
71 bool isActiveForVM(VM
&) const;
73 // Only called on the main thread after suspending all threads.
74 void visitWeakReferences(SlotVisitor
&, CodeBlockSet
&);
75 void removeDeadPlans(VM
&);
77 void dump(PrintStream
&) const;
80 Worklist(CString worklistName
);
81 void finishCreation(unsigned numberOfThreads
, int);
83 void runThread(ThreadData
*);
84 static void threadFunction(void* argument
);
86 void removeAllReadyPlansForVM(VM
&, Vector
<RefPtr
<Plan
>, 8>&);
88 void dump(const MutexLocker
&, PrintStream
&) const;
92 // Used to inform the thread about what work there is left to do.
93 Deque
<RefPtr
<Plan
>> m_queue
;
95 // Used to answer questions about the current state of a code block. This
96 // is particularly great for the cti_optimize OSR slow path, which wants
97 // to know: did I get here because a better version of me just got
99 typedef HashMap
<CompilationKey
, RefPtr
<Plan
>> PlanMap
;
102 // Used to quickly find which plans have been compiled and are ready to
104 Vector
<RefPtr
<Plan
>, 16> m_readyPlans
;
106 Mutex m_suspensionLock
;
108 mutable Mutex m_lock
;
109 ThreadCondition m_planEnqueued
;
110 ThreadCondition m_planCompiled
;
112 Vector
<std::unique_ptr
<ThreadData
>> m_threads
;
113 unsigned m_numberOfActiveThreads
;
116 // For DFGMode compilations.
117 Worklist
* ensureGlobalDFGWorklist();
118 Worklist
* existingGlobalDFGWorklistOrNull();
120 // For FTLMode and FTLForOSREntryMode compilations.
121 Worklist
* ensureGlobalFTLWorklist();
122 Worklist
* existingGlobalFTLWorklistOrNull();
124 Worklist
* ensureGlobalWorklistFor(CompilationMode
);
126 // Simplify doing things for all worklists.
127 inline unsigned numberOfWorklists() { return 2; }
128 inline Worklist
* worklistForIndexOrNull(unsigned index
)
132 return existingGlobalDFGWorklistOrNull();
134 return existingGlobalFTLWorklistOrNull();
136 RELEASE_ASSERT_NOT_REACHED();
141 } } // namespace JSC::DFG
143 #endif // ENABLE(DFG_JIT)
145 #endif // DFGWorklist_h