2016-09-30 15:52:31 +00:00
|
|
|
// 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 "test/inspector/task-runner.h"
|
|
|
|
|
|
|
|
#if !defined(_WIN32) && !defined(_WIN64)
|
|
|
|
#include <unistd.h> // NOLINT
|
|
|
|
#endif // !defined(_WIN32) && !defined(_WIN64)
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
void ReportUncaughtException(v8::Isolate* isolate,
|
|
|
|
const v8::TryCatch& try_catch) {
|
|
|
|
CHECK(try_catch.HasCaught());
|
|
|
|
v8::HandleScope handle_scope(isolate);
|
2017-08-24 21:49:48 +00:00
|
|
|
std::string message =
|
|
|
|
*v8::String::Utf8Value(isolate, try_catch.Message()->Get());
|
2017-05-18 23:11:20 +00:00
|
|
|
int line = try_catch.Message()
|
|
|
|
->GetLineNumber(isolate->GetCurrentContext())
|
|
|
|
.FromJust();
|
2017-08-24 21:49:48 +00:00
|
|
|
std::string source_line = *v8::String::Utf8Value(
|
|
|
|
isolate, try_catch.Message()
|
|
|
|
->GetSourceLine(isolate->GetCurrentContext())
|
|
|
|
.ToLocalChecked());
|
2017-05-18 23:11:20 +00:00
|
|
|
fprintf(stderr, "Unhandle exception: %s @%s[%d]\n", message.data(),
|
|
|
|
source_line.data(), line);
|
2016-09-30 15:52:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2017-05-16 23:14:46 +00:00
|
|
|
TaskRunner::TaskRunner(IsolateData::SetupGlobalTasks setup_global_tasks,
|
|
|
|
bool catch_exceptions,
|
|
|
|
v8::base::Semaphore* ready_semaphore,
|
2017-05-22 20:46:42 +00:00
|
|
|
v8::StartupData* startup_data, bool with_inspector)
|
2017-05-16 23:14:46 +00:00
|
|
|
: Thread(Options("Task Runner")),
|
|
|
|
setup_global_tasks_(std::move(setup_global_tasks)),
|
|
|
|
startup_data_(startup_data),
|
2017-05-22 20:46:42 +00:00
|
|
|
with_inspector_(with_inspector),
|
2017-05-16 23:14:46 +00:00
|
|
|
catch_exceptions_(catch_exceptions),
|
|
|
|
ready_semaphore_(ready_semaphore),
|
|
|
|
data_(nullptr),
|
|
|
|
process_queue_semaphore_(0),
|
2018-07-11 16:37:32 +00:00
|
|
|
nested_loop_count_(0),
|
|
|
|
is_terminated_(0) {
|
2019-07-29 13:09:02 +00:00
|
|
|
CHECK(Start());
|
2016-09-30 15:52:31 +00:00
|
|
|
}
|
|
|
|
|
2017-05-16 23:14:46 +00:00
|
|
|
TaskRunner::~TaskRunner() { Join(); }
|
|
|
|
|
2016-09-30 15:52:31 +00:00
|
|
|
void TaskRunner::Run() {
|
2017-05-18 23:11:20 +00:00
|
|
|
data_.reset(new IsolateData(this, std::move(setup_global_tasks_),
|
2017-05-22 20:46:42 +00:00
|
|
|
startup_data_, with_inspector_));
|
2017-05-16 23:14:46 +00:00
|
|
|
if (ready_semaphore_) ready_semaphore_->Signal();
|
2016-09-30 15:52:31 +00:00
|
|
|
RunMessageLoop(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TaskRunner::RunMessageLoop(bool only_protocol) {
|
|
|
|
int loop_number = ++nested_loop_count_;
|
2018-07-11 16:37:32 +00:00
|
|
|
while (nested_loop_count_ == loop_number && !is_terminated_) {
|
2016-09-30 15:52:31 +00:00
|
|
|
TaskRunner::Task* task = GetNext(only_protocol);
|
2016-10-19 02:04:34 +00:00
|
|
|
if (!task) return;
|
2017-05-16 23:14:46 +00:00
|
|
|
v8::Isolate::Scope isolate_scope(isolate());
|
2016-10-05 15:07:57 +00:00
|
|
|
if (catch_exceptions_) {
|
2017-05-16 23:14:46 +00:00
|
|
|
v8::TryCatch try_catch(isolate());
|
2017-05-22 20:46:42 +00:00
|
|
|
task->Run(data_.get());
|
2016-10-05 15:07:57 +00:00
|
|
|
delete task;
|
|
|
|
if (try_catch.HasCaught()) {
|
2017-05-16 23:14:46 +00:00
|
|
|
ReportUncaughtException(isolate(), try_catch);
|
2016-10-05 15:07:57 +00:00
|
|
|
fflush(stdout);
|
|
|
|
fflush(stderr);
|
|
|
|
_exit(0);
|
|
|
|
}
|
|
|
|
} else {
|
2017-05-22 20:46:42 +00:00
|
|
|
task->Run(data_.get());
|
2016-10-05 15:07:57 +00:00
|
|
|
delete task;
|
2016-09-30 15:52:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TaskRunner::QuitMessageLoop() {
|
2017-09-25 10:50:12 +00:00
|
|
|
DCHECK_LT(0, nested_loop_count_);
|
2016-09-30 15:52:31 +00:00
|
|
|
--nested_loop_count_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TaskRunner::Append(Task* task) {
|
|
|
|
queue_.Enqueue(task);
|
|
|
|
process_queue_semaphore_.Signal();
|
|
|
|
}
|
|
|
|
|
2016-10-19 02:04:34 +00:00
|
|
|
void TaskRunner::Terminate() {
|
2018-07-11 16:37:32 +00:00
|
|
|
is_terminated_++;
|
2016-10-19 02:04:34 +00:00
|
|
|
process_queue_semaphore_.Signal();
|
|
|
|
}
|
|
|
|
|
2016-09-30 15:52:31 +00:00
|
|
|
TaskRunner::Task* TaskRunner::GetNext(bool only_protocol) {
|
|
|
|
for (;;) {
|
2018-07-11 16:37:32 +00:00
|
|
|
if (is_terminated_) return nullptr;
|
2016-09-30 15:52:31 +00:00
|
|
|
if (only_protocol) {
|
|
|
|
Task* task = nullptr;
|
|
|
|
if (queue_.Dequeue(&task)) {
|
2017-05-22 20:46:42 +00:00
|
|
|
if (task->is_priority_task()) return task;
|
2016-09-30 15:52:31 +00:00
|
|
|
deffered_queue_.Enqueue(task);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Task* task = nullptr;
|
|
|
|
if (deffered_queue_.Dequeue(&task)) return task;
|
|
|
|
if (queue_.Dequeue(&task)) return task;
|
|
|
|
}
|
|
|
|
process_queue_semaphore_.Wait();
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|