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: 6 11 54.5 %
Date: 2019-09-26 22:31:05 Branches: 1 2 50.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
10380
  ~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
5848
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 v8::TaskRunner,
56
    public std::enable_shared_from_this<PerIsolatePlatformData> {
57
 public:
58
  PerIsolatePlatformData(v8::Isolate* isolate, uv_loop_t* loop);
59
  ~PerIsolatePlatformData() override;
60
61
  void PostTask(std::unique_ptr<v8::Task> task) override;
62
  void PostIdleTask(std::unique_ptr<v8::IdleTask> task) override;
63
  void PostDelayedTask(std::unique_ptr<v8::Task> task,
64
                       double delay_in_seconds) override;
65
  bool IdleTasksEnabled() override { return false; }
66
67
  // Non-nestable tasks are treated like regular tasks.
68
709
  bool NonNestableTasksEnabled() const override { return true; }
69
409
  bool NonNestableDelayedTasksEnabled() const override { return true; }
70
  void PostNonNestableTask(std::unique_ptr<v8::Task> task) override;
71
  void PostNonNestableDelayedTask(std::unique_ptr<v8::Task> task,
72
                                  double delay_in_seconds) override;
73
74
  void AddShutdownCallback(void (*callback)(void*), void* data);
75
  void Shutdown();
76
77
  // Returns true if work was dispatched or executed. New tasks that are
78
  // posted during flushing of the queue are postponed until the next
79
  // flushing.
80
  bool FlushForegroundTasksInternal();
81
  void CancelPendingDelayedTasks();
82
83
  const uv_loop_t* event_loop() const { return loop_; }
84
85
 private:
86
  void DeleteFromScheduledTasks(DelayedTask* task);
87
88
  static void FlushTasks(uv_async_t* handle);
89
  static void RunForegroundTask(std::unique_ptr<v8::Task> task);
90
  static void RunForegroundTask(uv_timer_t* timer);
91
92
  struct ShutdownCallback {
93
    void (*cb)(void*);
94
    void* data;
95
  };
96
  typedef std::vector<ShutdownCallback> ShutdownCbList;
97
  ShutdownCbList shutdown_callbacks_;
98
99
  uv_loop_t* const loop_;
100
  uv_async_t* flush_tasks_ = nullptr;
101
  TaskQueue<v8::Task> foreground_tasks_;
102
  TaskQueue<DelayedTask> foreground_delayed_tasks_;
103
104
  // Use a custom deleter because libuv needs to close the handle first.
105
  typedef std::unique_ptr<DelayedTask, std::function<void(DelayedTask*)>>
106
      DelayedTaskPointer;
107
  std::vector<DelayedTaskPointer> scheduled_delayed_tasks_;
108
};
109
110
// This acts as the single worker thread task runner for all Isolates.
111
4891
class WorkerThreadsTaskRunner {
112
 public:
113
  explicit WorkerThreadsTaskRunner(int thread_pool_size);
114
115
  void PostTask(std::unique_ptr<v8::Task> task);
116
  void PostDelayedTask(std::unique_ptr<v8::Task> task,
117
                       double delay_in_seconds);
118
119
  void BlockingDrain();
120
  void Shutdown();
121
122
  int NumberOfWorkerThreads() const;
123
124
 private:
125
  TaskQueue<v8::Task> pending_worker_tasks_;
126
127
  class DelayedTaskScheduler;
128
  std::unique_ptr<DelayedTaskScheduler> delayed_task_scheduler_;
129
130
  std::vector<std::unique_ptr<uv_thread_t>> threads_;
131
};
132
133
class NodePlatform : public MultiIsolatePlatform {
134
 public:
135
  NodePlatform(int thread_pool_size,
136
               node::tracing::TracingController* tracing_controller);
137
9782
  ~NodePlatform() override = default;
138
139
  void DrainTasks(v8::Isolate* isolate) override;
140
  void CancelPendingDelayedTasks(v8::Isolate* isolate) override;
141
  void Shutdown();
142
143
  // v8::Platform implementation.
144
  int NumberOfWorkerThreads() override;
145
  void CallOnWorkerThread(std::unique_ptr<v8::Task> task) override;
146
  void CallDelayedOnWorkerThread(std::unique_ptr<v8::Task> task,
147
                                 double delay_in_seconds) override;
148
  void CallOnForegroundThread(v8::Isolate* isolate, v8::Task* task) override {
149
    UNREACHABLE();
150
  }
151
  void CallDelayedOnForegroundThread(v8::Isolate* isolate,
152
                                     v8::Task* task,
153
                                     double delay_in_seconds) override {
154
    UNREACHABLE();
155
  }
156
  bool IdleTasksEnabled(v8::Isolate* isolate) override;
157
  double MonotonicallyIncreasingTime() override;
158
  double CurrentClockTimeMillis() override;
159
  node::tracing::TracingController* GetTracingController() override;
160
  bool FlushForegroundTasks(v8::Isolate* isolate) override;
161
162
  void RegisterIsolate(v8::Isolate* isolate, uv_loop_t* loop) override;
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
 private:
171
  std::shared_ptr<PerIsolatePlatformData> ForIsolate(v8::Isolate* isolate);
172
173
  Mutex per_isolate_mutex_;
174
  std::unordered_map<v8::Isolate*,
175
                     std::shared_ptr<PerIsolatePlatformData>> per_isolate_;
176
177
  node::tracing::TracingController* tracing_controller_;
178
  std::shared_ptr<WorkerThreadsTaskRunner> worker_thread_task_runner_;
179
};
180
181
}  // namespace node
182
183
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
184
185
#endif  // SRC_NODE_PLATFORM_H_