task-runner.cc 4.51 KB
Newer Older
1 2 3 4 5 6
// 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"

7 8 9
#include "include/libplatform/libplatform.h"
#include "src/flags/flags.h"

10
#if !defined(_WIN32) && !defined(_WIN64)
11 12
#include <unistd.h>
#endif  // !defined(_WIN32) && !defined(_WIN64)
13

14 15 16
namespace v8 {
namespace internal {

17 18 19 20 21 22
namespace {

void ReportUncaughtException(v8::Isolate* isolate,
                             const v8::TryCatch& try_catch) {
  CHECK(try_catch.HasCaught());
  v8::HandleScope handle_scope(isolate);
23 24
  std::string message =
      *v8::String::Utf8Value(isolate, try_catch.Message()->Get());
25 26 27
  int line = try_catch.Message()
                 ->GetLineNumber(isolate->GetCurrentContext())
                 .FromJust();
28 29 30 31
  std::string source_line = *v8::String::Utf8Value(
      isolate, try_catch.Message()
                   ->GetSourceLine(isolate->GetCurrentContext())
                   .ToLocalChecked());
32
  fprintf(stderr, "Unhandled exception: %s @%s[%d]\n", message.data(),
33
          source_line.data(), line);
34 35 36 37
}

}  //  namespace

38
TaskRunner::TaskRunner(IsolateData::SetupGlobalTasks setup_global_tasks,
39
                       CatchExceptions catch_exceptions,
40
                       v8::base::Semaphore* ready_semaphore,
41 42
                       v8::StartupData* startup_data,
                       WithInspector with_inspector)
43 44 45
    : Thread(Options("Task Runner")),
      setup_global_tasks_(std::move(setup_global_tasks)),
      startup_data_(startup_data),
46
      with_inspector_(with_inspector),
47 48 49 50
      catch_exceptions_(catch_exceptions),
      ready_semaphore_(ready_semaphore),
      data_(nullptr),
      process_queue_semaphore_(0),
51 52
      nested_loop_count_(0),
      is_terminated_(0) {
53
  CHECK(Start());
54 55
}

56
TaskRunner::~TaskRunner() {}
57

58
void TaskRunner::Run() {
59
  data_.reset(new IsolateData(this, std::move(setup_global_tasks_),
60
                              startup_data_, with_inspector_));
61
  if (ready_semaphore_) ready_semaphore_->Signal();
62 63 64 65 66
  RunMessageLoop(false);
}

void TaskRunner::RunMessageLoop(bool only_protocol) {
  int loop_number = ++nested_loop_count_;
67
  while (nested_loop_count_ == loop_number && !is_terminated_) {
68
    std::unique_ptr<TaskRunner::Task> task = GetNext(only_protocol);
69
    if (!task) return;
70
    v8::Isolate::Scope isolate_scope(isolate());
71 72 73
    v8::TryCatch try_catch(isolate());
    if (catch_exceptions_ == kStandardPropagateUncaughtExceptions) {
      try_catch.SetVerbose(true);
74
    }
75 76 77 78 79 80 81
    task->Run(data_.get());
    if (catch_exceptions_ == kFailOnUncaughtExceptions &&
        try_catch.HasCaught()) {
      ReportUncaughtException(isolate(), try_catch);
      base::OS::ExitProcess(0);
    }
    try_catch.Reset();
82
    task.reset();
83 84 85 86 87 88
    // 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(
89 90 91 92
          v8::internal::V8::GetCurrentPlatform(), isolate(),
          isolate()->HasPendingBackgroundTasks()
              ? platform::MessageLoopBehavior::kWaitForWork
              : platform::MessageLoopBehavior::kDoNotWait)) {
93 94
      }
    }
95 96 97
  }
}

98 99 100 101 102 103 104 105 106
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);
}

107
void TaskRunner::QuitMessageLoop() {
108
  DCHECK_LT(0, nested_loop_count_);
109 110 111
  --nested_loop_count_;
}

112 113
void TaskRunner::Append(std::unique_ptr<Task> task) {
  queue_.Enqueue(std::move(task));
114 115 116
  process_queue_semaphore_.Signal();
}

117
void TaskRunner::Terminate() {
118
  is_terminated_++;
119
  isolate()->TerminateExecution();
120 121 122
  process_queue_semaphore_.Signal();
}

123
std::unique_ptr<TaskRunner::Task> TaskRunner::GetNext(bool only_protocol) {
124
  for (;;) {
125
    if (is_terminated_) return nullptr;
126
    if (only_protocol) {
127
      std::unique_ptr<Task> task;
128
      if (queue_.Dequeue(&task)) {
129
        if (task->is_priority_task()) return task;
130
        deferred_queue_.Enqueue(std::move(task));
131 132
      }
    } else {
133
      std::unique_ptr<Task> task;
134
      if (deferred_queue_.Dequeue(&task)) return task;
135 136 137 138 139
      if (queue_.Dequeue(&task)) return task;
    }
    process_queue_semaphore_.Wait();
  }
}
140 141 142

}  // namespace internal
}  // namespace v8