GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/node_platform.h Lines: 4 6 66.7 %
Date: 2020-05-27 22:15:15 Branches: 0 0 - %

Line Branch Exec Source
1
#ifndef SRC_NODE_PLATFORM_H_
2
#define SRC_NODE_PLATFORM_H_
3
4
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
5
6
#include <queue>
7
#include <unordered_map>
8
#include <vector>
9
#include <functional>
10
11
#include "libplatform/libplatform.h"
12
#include "node.h"
13
#include "node_mutex.h"
14
#include "tracing/agent.h"
15
#include "uv.h"
16
17
namespace node {
18
19
class NodePlatform;
20
class IsolateData;
21
class PerIsolatePlatformData;
22
23
template <class T>
24
class TaskQueue {
25
 public:
26
  TaskQueue();
27
9258
  ~TaskQueue() = default;
28
29
  void Push(std::unique_ptr<T> task);
30
  std::unique_ptr<T> Pop();
31
  std::unique_ptr<T> BlockingPop();
32
  std::queue<std::unique_ptr<T>> PopAll();
33
  void NotifyOfCompletion();
34
  void BlockingDrain();
35
  void Stop();
36
37
 private:
38
  Mutex lock_;
39
  ConditionVariable tasks_available_;
40
  ConditionVariable tasks_drained_;
41
  int outstanding_tasks_;
42
  bool stopped_;
43
  std::queue<std::unique_ptr<T>> task_queue_;
44
};
45
46
4903
struct DelayedTask {
47
  std::unique_ptr<v8::Task> task;
48
  uv_timer_t timer;
49
  double timeout;
50
  std::shared_ptr<PerIsolatePlatformData> platform_data;
51
};
52
53
// This acts as the foreground task runner for a given Isolate.
54
class PerIsolatePlatformData :
55
    public IsolatePlatformDelegate,
56
    public v8::TaskRunner,
57
    public std::enable_shared_from_this<PerIsolatePlatformData> {
58
 public:
59
  PerIsolatePlatformData(v8::Isolate* isolate, uv_loop_t* loop);
60
  ~PerIsolatePlatformData() override;
61
62
  std::shared_ptr<v8::TaskRunner> GetForegroundTaskRunner() override;
63
  void PostTask(std::unique_ptr<v8::Task> task) override;
64
  void PostIdleTask(std::unique_ptr<v8::IdleTask> task) override;
65
  void PostDelayedTask(std::unique_ptr<v8::Task> task,
66
                       double delay_in_seconds) override;
67
  bool IdleTasksEnabled() override { return false; }
68
69
  // Non-nestable tasks are treated like regular tasks.
70
7345
  bool NonNestableTasksEnabled() const override { return true; }
71
  bool NonNestableDelayedTasksEnabled() const override { return true; }
72
  void PostNonNestableTask(std::unique_ptr<v8::Task> task) override;
73
  void PostNonNestableDelayedTask(std::unique_ptr<v8::Task> task,
74
                                  double delay_in_seconds) override;
75
76
  void AddShutdownCallback(void (*callback)(void*), void* data);
77
  void Shutdown();
78
79
  // Returns true if work was dispatched or executed. New tasks that are
80
  // posted during flushing of the queue are postponed until the next
81
  // flushing.
82
  bool FlushForegroundTasksInternal();
83
84
  const uv_loop_t* event_loop() const { return loop_; }
85
86
 private:
87
  void DeleteFromScheduledTasks(DelayedTask* task);
88
  void DecreaseHandleCount();
89
90
  static void FlushTasks(uv_async_t* handle);
91
  void RunForegroundTask(std::unique_ptr<v8::Task> task);
92
  static void RunForegroundTask(uv_timer_t* timer);
93
94
  struct ShutdownCallback {
95
    void (*cb)(void*);
96
    void* data;
97
  };
98
  typedef std::vector<ShutdownCallback> ShutdownCbList;
99
  ShutdownCbList shutdown_callbacks_;
100
  // shared_ptr to self to keep this object alive during shutdown.
101
  std::shared_ptr<PerIsolatePlatformData> self_reference_;
102
  uint32_t uv_handle_count_ = 1;  // 1 = flush_tasks_
103
104
  v8::Isolate* const isolate_;
105
  uv_loop_t* const loop_;
106
  uv_async_t* flush_tasks_ = nullptr;
107
  TaskQueue<v8::Task> foreground_tasks_;
108
  TaskQueue<DelayedTask> foreground_delayed_tasks_;
109
110
  // Use a custom deleter because libuv needs to close the handle first.
111
  typedef std::unique_ptr<DelayedTask, void(*)(DelayedTask*)>
112
      DelayedTaskPointer;
113
  std::vector<DelayedTaskPointer> scheduled_delayed_tasks_;
114
};
115
116
// This acts as the single worker thread task runner for all Isolates.
117
4264
class WorkerThreadsTaskRunner {
118
 public:
119
  explicit WorkerThreadsTaskRunner(int thread_pool_size);
120
121
  void PostTask(std::unique_ptr<v8::Task> task);
122
  void PostDelayedTask(std::unique_ptr<v8::Task> task,
123
                       double delay_in_seconds);
124
125
  void BlockingDrain();
126
  void Shutdown();
127
128
  int NumberOfWorkerThreads() const;
129
130
 private:
131
  TaskQueue<v8::Task> pending_worker_tasks_;
132
133
  class DelayedTaskScheduler;
134
  std::unique_ptr<DelayedTaskScheduler> delayed_task_scheduler_;
135
136
  std::vector<std::unique_ptr<uv_thread_t>> threads_;
137
};
138
139
class NodePlatform : public MultiIsolatePlatform {
140
 public:
141
  NodePlatform(int thread_pool_size,
142
               v8::TracingController* tracing_controller);
143
  ~NodePlatform() override;
144
145
  void DrainTasks(v8::Isolate* isolate) override;
146
  void Shutdown();
147
148
  // v8::Platform implementation.
149
  int NumberOfWorkerThreads() override;
150
  void CallOnWorkerThread(std::unique_ptr<v8::Task> task) override;
151
  void CallDelayedOnWorkerThread(std::unique_ptr<v8::Task> task,
152
                                 double delay_in_seconds) override;
153
  bool IdleTasksEnabled(v8::Isolate* isolate) override;
154
  double MonotonicallyIncreasingTime() override;
155
  double CurrentClockTimeMillis() override;
156
  v8::TracingController* GetTracingController() override;
157
  bool FlushForegroundTasks(v8::Isolate* isolate) override;
158
159
  void RegisterIsolate(v8::Isolate* isolate, uv_loop_t* loop) override;
160
  void RegisterIsolate(v8::Isolate* isolate,
161
                       IsolatePlatformDelegate* delegate) override;
162
163
  void UnregisterIsolate(v8::Isolate* isolate) override;
164
  void AddIsolateFinishedCallback(v8::Isolate* isolate,
165
                                  void (*callback)(void*), void* data) override;
166
167
  std::shared_ptr<v8::TaskRunner> GetForegroundTaskRunner(
168
      v8::Isolate* isolate) override;
169
170
  Platform::StackTracePrinter GetStackTracePrinter() override;
171
172
 private:
173
  IsolatePlatformDelegate* ForIsolate(v8::Isolate* isolate);
174
  std::shared_ptr<PerIsolatePlatformData> ForNodeIsolate(v8::Isolate* isolate);
175
176
  Mutex per_isolate_mutex_;
177
  using DelegatePair = std::pair<
178
    IsolatePlatformDelegate*, std::shared_ptr<PerIsolatePlatformData>>;
179
  std::unordered_map<v8::Isolate*, DelegatePair> per_isolate_;
180
181
  v8::TracingController* tracing_controller_;
182
  std::shared_ptr<WorkerThreadsTaskRunner> worker_thread_task_runner_;
183
  bool has_shut_down_ = false;
184
};
185
186
}  // namespace node
187
188
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
189
190
#endif  // SRC_NODE_PLATFORM_H_