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"
|
|
|
|
|
2020-08-14 10:10:38 +00:00
|
|
|
#include "include/libplatform/libplatform.h"
|
|
|
|
#include "src/flags/flags.h"
|
|
|
|
|
2016-09-30 15:52:31 +00:00
|
|
|
#if !defined(_WIN32) && !defined(_WIN64)
|
|
|
|
#include <unistd.h> // NOLINT
|
|
|
|
#endif // !defined(_WIN32) && !defined(_WIN64)
|
|
|
|
|
2020-10-27 13:52:22 +00:00
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
|
2016-09-30 15:52:31 +00:00
|
|
|
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());
|
2020-11-09 14:18:44 +00:00
|
|
|
fprintf(stderr, "Unhandled exception: %s @%s[%d]\n", message.data(),
|
2017-05-18 23:11:20 +00:00
|
|
|
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,
|
2020-11-09 13:52:29 +00:00
|
|
|
CatchExceptions catch_exceptions,
|
2017-05-16 23:14:46 +00:00
|
|
|
v8::base::Semaphore* ready_semaphore,
|
2020-11-09 13:52:29 +00:00
|
|
|
v8::StartupData* startup_data,
|
|
|
|
WithInspector 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
|
|
|
}
|
|
|
|
|
2021-03-26 13:10:54 +00:00
|
|
|
TaskRunner::~TaskRunner() {}
|
2017-05-16 23:14:46 +00:00
|
|
|
|
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_) {
|
2020-11-05 16:15:32 +00:00
|
|
|
std::unique_ptr<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());
|
2020-11-12 12:57:48 +00:00
|
|
|
v8::TryCatch try_catch(isolate());
|
|
|
|
if (catch_exceptions_ == kStandardPropagateUncaughtExceptions) {
|
|
|
|
try_catch.SetVerbose(true);
|
2016-09-30 15:52:31 +00:00
|
|
|
}
|
2020-11-12 12:57:48 +00:00
|
|
|
task->Run(data_.get());
|
|
|
|
if (catch_exceptions_ == kFailOnUncaughtExceptions &&
|
|
|
|
try_catch.HasCaught()) {
|
|
|
|
ReportUncaughtException(isolate(), try_catch);
|
|
|
|
base::OS::ExitProcess(0);
|
|
|
|
}
|
|
|
|
try_catch.Reset();
|
2020-11-05 16:15:32 +00:00
|
|
|
task.reset();
|
2020-08-14 10:10:38 +00:00
|
|
|
// Also pump isolate's foreground task queue to ensure progress.
|
|
|
|
// This can be removed once https://crbug.com/v8/10747 is fixed.
|
|
|
|
// TODO(10748): Enable --stress-incremental-marking after the existing
|
|
|
|
// tests are fixed.
|
|
|
|
if (!i::FLAG_stress_incremental_marking) {
|
|
|
|
while (v8::platform::PumpMessageLoop(
|
2020-11-30 13:45:16 +00:00
|
|
|
v8::internal::V8::GetCurrentPlatform(), isolate(),
|
|
|
|
isolate()->HasPendingBackgroundTasks()
|
|
|
|
? platform::MessageLoopBehavior::kWaitForWork
|
|
|
|
: platform::MessageLoopBehavior::kDoNotWait)) {
|
2020-08-14 10:10:38 +00:00
|
|
|
}
|
|
|
|
}
|
2016-09-30 15:52:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-24 10:08:09 +00:00
|
|
|
static void RunMessageLoopInInterrupt(v8::Isolate* isolate, void* task_runner) {
|
|
|
|
TaskRunner* runner = reinterpret_cast<TaskRunner*>(task_runner);
|
|
|
|
runner->RunMessageLoop(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TaskRunner::InterruptForMessages() {
|
|
|
|
isolate()->RequestInterrupt(&RunMessageLoopInInterrupt, this);
|
|
|
|
}
|
|
|
|
|
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_;
|
|
|
|
}
|
|
|
|
|
2020-11-05 16:15:32 +00:00
|
|
|
void TaskRunner::Append(std::unique_ptr<Task> task) {
|
|
|
|
queue_.Enqueue(std::move(task));
|
2016-09-30 15:52:31 +00:00
|
|
|
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_++;
|
2021-01-11 16:07:23 +00:00
|
|
|
isolate()->TerminateExecution();
|
2016-10-19 02:04:34 +00:00
|
|
|
process_queue_semaphore_.Signal();
|
|
|
|
}
|
|
|
|
|
2020-11-05 16:15:32 +00:00
|
|
|
std::unique_ptr<TaskRunner::Task> TaskRunner::GetNext(bool only_protocol) {
|
2016-09-30 15:52:31 +00:00
|
|
|
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) {
|
2020-11-05 16:15:32 +00:00
|
|
|
std::unique_ptr<Task> task;
|
2016-09-30 15:52:31 +00:00
|
|
|
if (queue_.Dequeue(&task)) {
|
2017-05-22 20:46:42 +00:00
|
|
|
if (task->is_priority_task()) return task;
|
2020-11-05 16:16:29 +00:00
|
|
|
deferred_queue_.Enqueue(std::move(task));
|
2016-09-30 15:52:31 +00:00
|
|
|
}
|
|
|
|
} else {
|
2020-11-05 16:15:32 +00:00
|
|
|
std::unique_ptr<Task> task;
|
2020-11-05 16:16:29 +00:00
|
|
|
if (deferred_queue_.Dequeue(&task)) return task;
|
2016-09-30 15:52:31 +00:00
|
|
|
if (queue_.Dequeue(&task)) return task;
|
|
|
|
}
|
|
|
|
process_queue_semaphore_.Wait();
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
2020-10-27 13:52:22 +00:00
|
|
|
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|