task-runner.cc 4.57 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
#include "include/libplatform/libplatform.h"
8 9 10
#include "include/v8-exception.h"
#include "include/v8-local-handle.h"
#include "include/v8-primitive.h"
11 12
#include "src/flags/flags.h"

13
#if !defined(_WIN32) && !defined(_WIN64)
14 15
#include <unistd.h>
#endif  // !defined(_WIN32) && !defined(_WIN64)
16

17 18 19
namespace v8 {
namespace internal {

20 21 22 23 24 25
namespace {

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

}  //  namespace

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

58
TaskRunner::~TaskRunner() {}
59

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

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

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

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

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

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

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

}  // namespace internal
}  // namespace v8