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: 7 11 63.6 %
Date: 2020-02-27 22:14: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
8826
  ~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
4722
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
30900
  bool IdleTasksEnabled() override { return false; }
68
69
  // Non-nestable tasks are treated like regular tasks.
70
181
  bool NonNestableTasksEnabled() const override { return true; }
71
70
  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
  static 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
  uv_loop_t* const loop_;
105
  uv_async_t* flush_tasks_ = nullptr;
106
  TaskQueue<v8::Task> foreground_tasks_;
107
  TaskQueue<DelayedTask> foreground_delayed_tasks_;
108
109
  // Use a custom deleter because libuv needs to close the handle first.
110
  typedef std::unique_ptr<DelayedTask, void(*)(DelayedTask*)>
111
      DelayedTaskPointer;
112
  std::vector<DelayedTaskPointer> scheduled_delayed_tasks_;
113
};
114
115
// This acts as the single worker thread task runner for all Isolates.
116
4143
class WorkerThreadsTaskRunner {
117
 public:
118
  explicit WorkerThreadsTaskRunner(int thread_pool_size);
119
120
  void PostTask(std::unique_ptr<v8::Task> task);
121
  void PostDelayedTask(std::unique_ptr<v8::Task> task,
122
                       double delay_in_seconds);
123
124
  void BlockingDrain();
125
  void Shutdown();
126
127
  int NumberOfWorkerThreads() const;
128
129
 private:
130
  TaskQueue<v8::Task> pending_worker_tasks_;
131
132
  class DelayedTaskScheduler;
133
  std::unique_ptr<DelayedTaskScheduler> delayed_task_scheduler_;
134
135
  std::vector<std::unique_ptr<uv_thread_t>> threads_;
136
};
137
138
class NodePlatform : public MultiIsolatePlatform {
139
 public:
140
  NodePlatform(int thread_pool_size,
141
               node::tracing::TracingController* tracing_controller);
142
8286
  ~NodePlatform() override = default;
143
144
  void DrainTasks(v8::Isolate* isolate) override;
145
  void Shutdown();
146
147
  // v8::Platform implementation.
148
  int NumberOfWorkerThreads() override;
149
  void CallOnWorkerThread(std::unique_ptr<v8::Task> task) override;
150
  void CallDelayedOnWorkerThread(std::unique_ptr<v8::Task> task,
151
                                 double delay_in_seconds) override;
152
  void CallOnForegroundThread(v8::Isolate* isolate, v8::Task* task) override {
153
    UNREACHABLE();
154
  }
155
  void CallDelayedOnForegroundThread(v8::Isolate* isolate,
156
                                     v8::Task* task,
157
                                     double delay_in_seconds) override {
158
    UNREACHABLE();
159
  }
160
  bool IdleTasksEnabled(v8::Isolate* isolate) override;
161
  double MonotonicallyIncreasingTime() override;
162
  double CurrentClockTimeMillis() override;
163
  node::tracing::TracingController* GetTracingController() override;
164
  bool FlushForegroundTasks(v8::Isolate* isolate) override;
165
166
  void RegisterIsolate(v8::Isolate* isolate, uv_loop_t* loop) override;
167
  void RegisterIsolate(v8::Isolate* isolate,
168
                       IsolatePlatformDelegate* delegate) override;
169
170
  void UnregisterIsolate(v8::Isolate* isolate) override;
171
  void AddIsolateFinishedCallback(v8::Isolate* isolate,
172
                                  void (*callback)(void*), void* data) override;
173
174
  std::shared_ptr<v8::TaskRunner> GetForegroundTaskRunner(
175
      v8::Isolate* isolate) override;
176
177
  Platform::StackTracePrinter GetStackTracePrinter() override;
178
179
 private:
180
  IsolatePlatformDelegate* ForIsolate(v8::Isolate* isolate);
181
  std::shared_ptr<PerIsolatePlatformData> ForNodeIsolate(v8::Isolate* isolate);
182
183
  Mutex per_isolate_mutex_;
184
  using DelegatePair = std::pair<
185
    IsolatePlatformDelegate*, std::shared_ptr<PerIsolatePlatformData>>;
186
  std::unordered_map<v8::Isolate*, DelegatePair> per_isolate_;
187
188
  node::tracing::TracingController* tracing_controller_;
189
  std::shared_ptr<WorkerThreadsTaskRunner> worker_thread_task_runner_;
190
};
191
192
}  // namespace node
193
194
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
195
196
#endif  // SRC_NODE_PLATFORM_H_