Index: src/compiler-dispatcher/compiler-dispatcher.cc |
diff --git a/src/compiler-dispatcher/compiler-dispatcher.cc b/src/compiler-dispatcher/compiler-dispatcher.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..228fe49dc45f3356a5711c8b553f89efcca9ae07 |
--- /dev/null |
+++ b/src/compiler-dispatcher/compiler-dispatcher.cc |
@@ -0,0 +1,122 @@ |
+// Copyright 2016 the V8 project authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "src/compiler-dispatcher/compiler-dispatcher.h" |
+ |
+#include "src/compiler-dispatcher/compiler-dispatcher-job.h" |
+#include "src/compiler-dispatcher/compiler-dispatcher-tracer.h" |
+#include "src/objects-inl.h" |
+ |
+namespace v8 { |
+namespace internal { |
+ |
+CompilerDispatcher::CompilerDispatcher(Isolate* isolate, size_t max_stack_size) |
+ : isolate_(isolate), |
+ max_stack_size_(max_stack_size), |
+ tracer_(new CompilerDispatcherTracer(isolate_)) {} |
+ |
+CompilerDispatcher::~CompilerDispatcher() {} |
+ |
+bool CompilerDispatcher::Enqueue(Handle<SharedFunctionInfo> function) { |
+ DCHECK(function->script()->IsScript()); |
+ if (IsEnqueued(function)) return true; |
+ std::unique_ptr<CompilerDispatcherJob> job(new CompilerDispatcherJob( |
marja
2016/12/12 09:00:02
Nit: no need to construct job if we do the early r
|
+ isolate_, tracer_.get(), function, max_stack_size_)); |
+ std::pair<int, int> key(Script::cast(function->script())->id(), |
+ function->function_literal_id()); |
+ // Script::id() is not guaranteed to be unique, so even if the function isn't |
+ // enqueued, we might not be able to enqueue it. |
marja
2016/12/12 09:00:02
Hmm, this is weird. So we cannot enqueue things be
rmcilroy
2016/12/12 09:50:39
Also, does this mean we are only able to enqueue o
marja
2016/12/12 09:54:59
I don't think it means that, since the key is the
rmcilroy
2016/12/12 10:08:30
Ahh right enough, I was thinking the pair was for
jochen (gone - plz use gerrit)
2016/12/12 10:51:17
done
|
+ if (jobs_.find(key) != jobs_.end()) return false; |
+ jobs_.insert(std::make_pair(key, std::move(job))); |
+ return true; |
+} |
+ |
+bool CompilerDispatcher::IsEnqueued(Handle<SharedFunctionInfo> function) const { |
+ if (!function->script()->IsScript()) return false; |
+ std::pair<int, int> key(Script::cast(function->script())->id(), |
+ function->function_literal_id()); |
+ auto job = jobs_.find(key); |
+ if (job == jobs_.end()) return false; |
+ return job->second->IsProcessing(function); |
+} |
+ |
+bool CompilerDispatcher::FinishNow(Handle<SharedFunctionInfo> function) { |
+ if (!IsEnqueued(function)) return false; |
+ std::pair<int, int> key(Script::cast(function->script())->id(), |
marja
2016/12/12 09:00:02
How about having a job-finding helper function - i
jochen (gone - plz use gerrit)
2016/12/12 10:51:17
done
|
+ function->function_literal_id()); |
+ auto job = jobs_.find(key); |
+ |
+ // TODO(jochen): Check if there's an in-flight background task working on this |
rmcilroy
2016/12/12 09:50:39
Could you DCHECK there is no background thread pro
jochen (gone - plz use gerrit)
2016/12/12 10:51:17
there's currently no way to check whether a job is
|
+ // job. |
+ while (!IsDone(job->second.get())) { |
+ DoNextStepOnMainThread(job->second.get()); |
+ } |
+ bool result = job->second->status() != CompileJobStatus::kFailed; |
+ jobs_.erase(job); |
+ return result; |
+} |
+ |
+void CompilerDispatcher::Abort(Handle<SharedFunctionInfo> function, |
+ BlockingBehavior blocking) { |
+ USE(blocking); |
+ if (!IsEnqueued(function)) return; |
marja
2016/12/12 09:00:02
Ditto
|
+ std::pair<int, int> key(Script::cast(function->script())->id(), |
+ function->function_literal_id()); |
+ auto job = jobs_.find(key); |
+ |
+ // TODO(jochen): Check if there's an in-flight background task working on this |
rmcilroy
2016/12/12 09:50:39
Ditto (and below).
|
+ // job. |
+ jobs_.erase(job); |
+} |
+ |
+void CompilerDispatcher::AbortAll(BlockingBehavior blocking) { |
+ USE(blocking); |
+ // TODO(jochen): Check if there's an in-flight background task working on this |
+ // job. |
+ jobs_.clear(); |
+} |
+ |
+// static |
+bool CompilerDispatcher::DoNextStepOnMainThread(CompilerDispatcherJob* job) { |
+ switch (job->status()) { |
+ case CompileJobStatus::kInitial: |
+ job->PrepareToParseOnMainThread(); |
+ break; |
+ |
+ case CompileJobStatus::kReadyToParse: |
+ job->Parse(); |
+ break; |
+ |
+ case CompileJobStatus::kParsed: |
+ job->FinalizeParsingOnMainThread(); |
+ break; |
+ |
+ case CompileJobStatus::kReadyToAnalyse: |
+ job->PrepareToCompileOnMainThread(); |
+ break; |
+ |
+ case CompileJobStatus::kReadyToCompile: |
+ job->Compile(); |
+ break; |
+ |
+ case CompileJobStatus::kCompiled: |
+ job->FinalizeCompilingOnMainThread(); |
+ break; |
+ |
+ case CompileJobStatus::kFailed: |
+ case CompileJobStatus::kDone: |
+ break; |
+ } |
+ |
+ return job->status() != CompileJobStatus::kFailed; |
+} |
+ |
+// static |
+bool CompilerDispatcher::IsDone(CompilerDispatcherJob* job) { |
+ return job->status() == CompileJobStatus::kDone || |
+ job->status() == CompileJobStatus::kFailed; |
+} |
+ |
+} // namespace internal |
+} // namespace v8 |