1/*
2 * Copyright (C) 2013 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#include "config.h"
27#include "DFGWorklist.h"
28
29#if ENABLE(DFG_JIT)
30
31#include "CodeBlock.h"
32#include "DFGLongLivedState.h"
33
34namespace JSC { namespace DFG {
35
36Worklist::Worklist()
37{
38}
39
40Worklist::~Worklist()
41{
42 m_queue.append(nullptr); // Use null plan to indicate that we want the thread to terminate.
43 waitForThreadCompletion(m_thread);
44}
45
46void Worklist::finishCreation()
47{
48 m_thread = createThread(threadFunction, this, "JSC Compilation Thread");
49}
50
51PassRefPtr<Worklist> Worklist::create()
52{
53 RefPtr<Worklist> result = adoptRef(new Worklist());
54 result->finishCreation();
55 return result;
56}
57
58void Worklist::enqueue(PassRefPtr<Plan> passedPlan)
59{
60 RefPtr<Plan> plan = passedPlan;
61 MutexLocker locker(m_lock);
62 ASSERT(m_plans.find(plan->key()) == m_plans.end());
63 m_plans.add(plan->key(), plan);
64 m_queue.append(plan);
65 m_condition.broadcast();
66}
67
68CompilationResult Worklist::completeAndScheduleOSR(CodeBlock* profiledBlock)
69{
70 RefPtr<Plan> plan;
71
72 {
73 MutexLocker locker(m_lock);
74 plan = m_plans.take(profiledBlock);
75 }
76
77 ASSERT(plan);
78 ASSERT(plan->isCompiled);
79
80 CompilationResult result = profiledBlock->replaceWithDeferredOptimizedCode(plan);
81
82 RELEASE_ASSERT(result != CompilationDeferred);
83 profiledBlock->setOptimizationThresholdBasedOnCompilationResult(result);
84
85 return result;
86}
87
88Worklist::State Worklist::compilationState(CodeBlock* profiledBlock)
89{
90 MutexLocker locker(m_lock);
91 PlanMap::iterator iter = m_plans.find(profiledBlock);
92 if (iter == m_plans.end())
93 return NotKnown;
94 return iter->value->isCompiled ? Compiled : Compiling;
95}
96
97Vector<RefPtr<CodeBlock> > Worklist::waitUntilAllForVMAreComplete(VM& vm)
98{
99 // Wait for all of the plans for the given VM to complete, and collect the
100 // CodeBlock*'s that are the keys to those plans. The idea here is that we
101 // want all of the caller VM's plans to be done. We don't care about any
102 // other VM's plans, and we won't attempt to complete those. After we
103 // release this lock, we know that although other VMs may still be adding
104 // plans, our VM will not be.
105 Vector<RefPtr<CodeBlock> > keyBlocks;
106 {
107 MutexLocker locker(m_lock);
108 for (;;) {
109 bool allAreCompleted = true;
110 PlanMap::iterator end = m_plans.end();
111 for (PlanMap::iterator iter = m_plans.begin(); iter != end; ++iter) {
112 if (&iter->value->vm != &vm)
113 continue;
114 if (!iter->value->isCompiled) {
115 allAreCompleted = false;
116 break;
117 }
118 }
119
120 if (allAreCompleted)
121 break;
122
123 m_condition.wait(m_lock);
124 }
125
126 PlanMap::iterator end = m_plans.end();
127 for (PlanMap::iterator iter = m_plans.begin(); iter != end; ++iter) {
128 if (&iter->value->vm != &vm)
129 continue;
130 keyBlocks.append(iter->key);
131 }
132 }
133 return keyBlocks;
134}
135
136void Worklist::completeAllForVM(VM& vm)
137{
138 Vector<RefPtr<CodeBlock> > keyBlocks = waitUntilAllForVMAreComplete(vm);
139
140 // Now complete all of the plans for this VM.
141 for (unsigned i = keyBlocks.size(); i--;)
142 completeAndScheduleOSR(keyBlocks[i].get());
143}
144
145void Worklist::runThread()
146{
147 LongLivedState longLivedState;
148
149 for (;;) {
150 RefPtr<Plan> plan;
151 {
152 MutexLocker locker(m_lock);
153 while (m_queue.isEmpty())
154 m_condition.wait(m_lock);
155 plan = m_queue.takeFirst();
156 }
157
158 if (!plan)
159 return;
160
161 plan->compileInThread(longLivedState);
162 completeAsynchronousPlan(plan.get());
163 }
164}
165
166void Worklist::completeAsynchronousPlan(Plan* plan)
167{
168 MutexLocker locker(m_lock);
169 plan->key()->forceOptimizationSlowPathConcurrently();
170 plan->isCompiled = true;
171 m_condition.broadcast();
172}
173
174void Worklist::threadFunction(void* argument)
175{
176 static_cast<Worklist*>(argument)->runThread();
177}
178
179static pthread_once_t initializeGlobalWorklistKeyOnce = PTHREAD_ONCE_INIT;
180static Worklist* theGlobalWorklist;
181
182static void initializeGlobalWorklistOnce()
183{
184 theGlobalWorklist = Worklist::create().leakRef();
185}
186
187Worklist* globalWorklist()
188{
189 pthread_once(&initializeGlobalWorklistKeyOnce, initializeGlobalWorklistOnce);
190 return theGlobalWorklist;
191}
192
193} } // namespace JSC::DFG
194
195#endif // ENABLE(DFG_JIT)
196