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

#ifndef V8_TEST_INSPECTOR_PROTOCOL_TASK_RUNNER_H_
#define V8_TEST_INSPECTOR_PROTOCOL_TASK_RUNNER_H_

8
#include <map>
9
#include <memory>
10

11 12 13 14
#include "include/v8-inspector.h"
#include "include/v8-platform.h"
#include "src/base/macros.h"
#include "src/base/platform/platform.h"
15
#include "src/base/vector.h"
16
#include "src/utils/locked-queue-inl.h"
17
#include "test/inspector/isolate-data.h"
18

19
namespace v8 {
20 21 22

class StartupData;

23 24
namespace internal {

25 26 27 28
enum CatchExceptions {
  kFailOnUncaughtExceptions,
  kStandardPropagateUncaughtExceptions,
  kSuppressUncaughtExceptions
29 30
};

31 32 33 34
class TaskRunner : public v8::base::Thread {
 public:
  class Task {
   public:
35
    virtual ~Task() = default;
36
    virtual bool is_priority_task() = 0;
37
    virtual void Run(InspectorIsolateData* data) = 0;
38 39
  };

40
  TaskRunner(InspectorIsolateData::SetupGlobalTasks setup_global_tasks,
41 42 43
             CatchExceptions catch_exceptions,
             v8::base::Semaphore* ready_semaphore,
             v8::StartupData* startup_data, WithInspector with_inspector);
44
  ~TaskRunner() override;
45 46
  TaskRunner(const TaskRunner&) = delete;
  TaskRunner& operator=(const TaskRunner&) = delete;
47
  InspectorIsolateData* data() const { return data_.get(); }
48 49 50 51 52 53 54 55

  // Thread implementation.
  void Run() override;

  // Should be called from the same thread and only from task.
  void RunMessageLoop(bool only_protocol);
  void QuitMessageLoop();

56
  void Append(std::unique_ptr<Task>);
57
  void InterruptForMessages();
58 59
  void Terminate();

60
 private:
61
  std::unique_ptr<Task> GetNext(bool only_protocol);
62
  v8::Isolate* isolate() const { return data_->isolate(); }
63

64
  InspectorIsolateData::SetupGlobalTasks setup_global_tasks_;
65
  v8::StartupData* startup_data_;
66 67
  WithInspector with_inspector_;
  CatchExceptions catch_exceptions_;
68
  v8::base::Semaphore* ready_semaphore_;
69
  std::unique_ptr<InspectorIsolateData> data_;
70 71

  // deferred_queue_ combined with queue_ (in this order) have all tasks in the
72 73
  // correct order. Sometimes we skip non-protocol tasks by moving them from
  // queue_ to deferred_queue_.
74
  v8::internal::LockedQueue<std::unique_ptr<Task>> queue_;
75
  v8::internal::LockedQueue<std::unique_ptr<Task>> deferred_queue_;
76 77 78
  v8::base::Semaphore process_queue_semaphore_;

  int nested_loop_count_;
79
  std::atomic<int> is_terminated_;
80 81
};

82 83 84
}  // namespace internal
}  // namespace v8

85
#endif  //  V8_TEST_INSPECTOR_PROTOCOL_TASK_RUNNER_H_