task-runner.cc 3.35 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// 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);
17 18
  std::string message =
      *v8::String::Utf8Value(isolate, try_catch.Message()->Get());
19 20 21
  int line = try_catch.Message()
                 ->GetLineNumber(isolate->GetCurrentContext())
                 .FromJust();
22 23 24 25
  std::string source_line = *v8::String::Utf8Value(
      isolate, try_catch.Message()
                   ->GetSourceLine(isolate->GetCurrentContext())
                   .ToLocalChecked());
26 27
  fprintf(stderr, "Unhandle exception: %s @%s[%d]\n", message.data(),
          source_line.data(), line);
28 29 30 31
}

}  //  namespace

32 33 34
TaskRunner::TaskRunner(IsolateData::SetupGlobalTasks setup_global_tasks,
                       bool catch_exceptions,
                       v8::base::Semaphore* ready_semaphore,
35
                       v8::StartupData* startup_data, bool with_inspector)
36 37 38
    : Thread(Options("Task Runner")),
      setup_global_tasks_(std::move(setup_global_tasks)),
      startup_data_(startup_data),
39
      with_inspector_(with_inspector),
40 41 42 43
      catch_exceptions_(catch_exceptions),
      ready_semaphore_(ready_semaphore),
      data_(nullptr),
      process_queue_semaphore_(0),
44 45
      nested_loop_count_(0),
      is_terminated_(0) {
46
  Start();
47 48
}

49 50
TaskRunner::~TaskRunner() { Join(); }

51
void TaskRunner::Run() {
52
  data_.reset(new IsolateData(this, std::move(setup_global_tasks_),
53
                              startup_data_, with_inspector_));
54
  if (ready_semaphore_) ready_semaphore_->Signal();
55 56 57 58 59
  RunMessageLoop(false);
}

void TaskRunner::RunMessageLoop(bool only_protocol) {
  int loop_number = ++nested_loop_count_;
60
  while (nested_loop_count_ == loop_number && !is_terminated_) {
61
    TaskRunner::Task* task = GetNext(only_protocol);
62
    if (!task) return;
63
    v8::Isolate::Scope isolate_scope(isolate());
64
    if (catch_exceptions_) {
65
      v8::TryCatch try_catch(isolate());
66
      task->Run(data_.get());
67 68
      delete task;
      if (try_catch.HasCaught()) {
69
        ReportUncaughtException(isolate(), try_catch);
70 71 72 73 74
        fflush(stdout);
        fflush(stderr);
        _exit(0);
      }
    } else {
75
      task->Run(data_.get());
76
      delete task;
77 78 79 80 81
    }
  }
}

void TaskRunner::QuitMessageLoop() {
82
  DCHECK_LT(0, nested_loop_count_);
83 84 85 86 87 88 89 90
  --nested_loop_count_;
}

void TaskRunner::Append(Task* task) {
  queue_.Enqueue(task);
  process_queue_semaphore_.Signal();
}

91
void TaskRunner::Terminate() {
92
  is_terminated_++;
93 94 95
  process_queue_semaphore_.Signal();
}

96 97
TaskRunner::Task* TaskRunner::GetNext(bool only_protocol) {
  for (;;) {
98
    if (is_terminated_) return nullptr;
99 100 101
    if (only_protocol) {
      Task* task = nullptr;
      if (queue_.Dequeue(&task)) {
102
        if (task->is_priority_task()) return task;
103 104 105 106 107 108 109 110 111 112 113
        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;
}