GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage/nodes/benchmark/out/../src/node_platform.h Lines: 3 3 100.0 %
Date: 2017-11-19 Branches: 1 2 50.0 %

Line Branch Exec Source
1
#ifndef SRC_NODE_PLATFORM_H_
2
#define SRC_NODE_PLATFORM_H_
3
4
#include <queue>
5
#include <unordered_map>
6
#include <vector>
7
#include <functional>
8
9
#include "libplatform/libplatform.h"
10
#include "node.h"
11
#include "node_mutex.h"
12
#include "uv.h"
13
14
namespace node {
15
16
class NodePlatform;
17
class IsolateData;
18
class PerIsolatePlatformData;
19
20
template <class T>
21
class TaskQueue {
22
 public:
23
  TaskQueue();
24
8859
  ~TaskQueue() {}
25
26
  void Push(std::unique_ptr<T> task);
27
  std::unique_ptr<T> Pop();
28
  std::unique_ptr<T> BlockingPop();
29
  void NotifyOfCompletion();
30
  void BlockingDrain();
31
  void Stop();
32
33
 private:
34
  Mutex lock_;
35
  ConditionVariable tasks_available_;
36
  ConditionVariable tasks_drained_;
37
  int outstanding_tasks_;
38
  bool stopped_;
39
  std::queue<std::unique_ptr<T>> task_queue_;
40
};
41
42
71
struct DelayedTask {
43
  std::unique_ptr<v8::Task> task;
44
  uv_timer_t timer;
45
  double timeout;
46
  PerIsolatePlatformData* platform_data;
47
};
48
49
class PerIsolatePlatformData {
50
 public:
51
  PerIsolatePlatformData(v8::Isolate* isolate, uv_loop_t* loop);
52
  ~PerIsolatePlatformData();
53
54
  void CallOnForegroundThread(std::unique_ptr<v8::Task> task);
55
  void CallDelayedOnForegroundThread(std::unique_ptr<v8::Task> task,
56
    double delay_in_seconds);
57
58
  void Shutdown();
59
60
  void ref();
61
  int unref();
62
63
  // Returns true iff work was dispatched or executed.
64
  bool FlushForegroundTasksInternal();
65
  void CancelPendingDelayedTasks();
66
67
 private:
68
  void DeleteFromScheduledTasks(DelayedTask* task);
69
70
  static void FlushTasks(uv_async_t* handle);
71
  static void RunForegroundTask(std::unique_ptr<v8::Task> task);
72
  static void RunForegroundTask(uv_timer_t* timer);
73
74
  int ref_count_ = 1;
75
  v8::Isolate* isolate_;
76
  uv_loop_t* const loop_;
77
  uv_async_t* flush_tasks_ = nullptr;
78
  TaskQueue<v8::Task> foreground_tasks_;
79
  TaskQueue<DelayedTask> foreground_delayed_tasks_;
80
81
  // Use a custom deleter because libuv needs to close the handle first.
82
  typedef std::unique_ptr<DelayedTask, std::function<void(DelayedTask*)>>
83
      DelayedTaskPointer;
84
  std::vector<DelayedTaskPointer> scheduled_delayed_tasks_;
85
};
86
87
class NodePlatform : public MultiIsolatePlatform {
88
 public:
89
  NodePlatform(int thread_pool_size, v8::TracingController* tracing_controller);
90
5922
  virtual ~NodePlatform() {}
91
92
  void DrainBackgroundTasks(v8::Isolate* isolate) override;
93
  void CancelPendingDelayedTasks(v8::Isolate* isolate) override;
94
  void Shutdown();
95
96
  // v8::Platform implementation.
97
  size_t NumberOfAvailableBackgroundThreads() override;
98
  void CallOnBackgroundThread(v8::Task* task,
99
                              ExpectedRuntime expected_runtime) override;
100
  void CallOnForegroundThread(v8::Isolate* isolate, v8::Task* task) override;
101
  void CallDelayedOnForegroundThread(v8::Isolate* isolate, v8::Task* task,
102
                                     double delay_in_seconds) override;
103
  bool IdleTasksEnabled(v8::Isolate* isolate) override;
104
  double MonotonicallyIncreasingTime() override;
105
  v8::TracingController* GetTracingController() override;
106
107
  void FlushForegroundTasks(v8::Isolate* isolate);
108
109
  void RegisterIsolate(IsolateData* isolate_data, uv_loop_t* loop) override;
110
  void UnregisterIsolate(IsolateData* isolate_data) override;
111
112
 private:
113
  PerIsolatePlatformData* ForIsolate(v8::Isolate* isolate);
114
115
  Mutex per_isolate_mutex_;
116
  std::unordered_map<v8::Isolate*, PerIsolatePlatformData*> per_isolate_;
117
  TaskQueue<v8::Task> background_tasks_;
118
  std::vector<std::unique_ptr<uv_thread_t>> threads_;
119
120
  std::unique_ptr<v8::TracingController> tracing_controller_;
121
};
122
123
}  // namespace node
124
125
#endif  // SRC_NODE_PLATFORM_H_