GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/node_worker.h Lines: 5 7 71.4 %
Date: 2020-02-27 22:14:15 Branches: 1 2 50.0 %

Line Branch Exec Source
1
#ifndef SRC_NODE_WORKER_H_
2
#define SRC_NODE_WORKER_H_
3
4
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
5
6
#include <unordered_map>
7
#include "node_messaging.h"
8
#include "uv.h"
9
10
namespace node {
11
namespace worker {
12
13
class WorkerThreadData;
14
15
enum ResourceLimits {
16
  kMaxYoungGenerationSizeMb,
17
  kMaxOldGenerationSizeMb,
18
  kCodeRangeSizeMb,
19
  kTotalResourceLimitCount
20
};
21
22
// A worker thread, as represented in its parent thread.
23
class Worker : public AsyncWrap {
24
 public:
25
  Worker(Environment* env,
26
         v8::Local<v8::Object> wrap,
27
         const std::string& url,
28
         std::shared_ptr<PerIsolateOptions> per_isolate_opts,
29
         std::vector<std::string>&& exec_argv,
30
         std::shared_ptr<KVStore> env_vars);
31
  ~Worker() override;
32
33
  // Run the worker. This is only called from the worker thread.
34
  void Run();
35
36
  // Forcibly exit the thread with a specified exit code. This may be called
37
  // from any thread.
38
  void Exit(int code);
39
40
  // Wait for the worker thread to stop (in a blocking manner).
41
  void JoinThread();
42
43
  template <typename Fn>
44
  inline bool RequestInterrupt(Fn&& cb);
45
46
  void MemoryInfo(MemoryTracker* tracker) const override;
47
  SET_MEMORY_INFO_NAME(Worker)
48
  SET_SELF_SIZE(Worker)
49
50
  bool is_stopped() const;
51
52
  static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
53
  static void CloneParentEnvVars(
54
      const v8::FunctionCallbackInfo<v8::Value>& args);
55
  static void SetEnvVars(const v8::FunctionCallbackInfo<v8::Value>& args);
56
  static void StartThread(const v8::FunctionCallbackInfo<v8::Value>& args);
57
  static void StopThread(const v8::FunctionCallbackInfo<v8::Value>& args);
58
  static void Ref(const v8::FunctionCallbackInfo<v8::Value>& args);
59
  static void Unref(const v8::FunctionCallbackInfo<v8::Value>& args);
60
  static void GetResourceLimits(
61
      const v8::FunctionCallbackInfo<v8::Value>& args);
62
  v8::Local<v8::Float64Array> GetResourceLimits(v8::Isolate* isolate) const;
63
  static void TakeHeapSnapshot(const v8::FunctionCallbackInfo<v8::Value>& args);
64
65
 private:
66
  void CreateEnvMessagePort(Environment* env);
67
  static size_t NearHeapLimit(void* data, size_t current_heap_limit,
68
                              size_t initial_heap_limit);
69
70
  std::shared_ptr<PerIsolateOptions> per_isolate_opts_;
71
  std::vector<std::string> exec_argv_;
72
  std::vector<std::string> argv_;
73
74
  MultiIsolatePlatform* platform_;
75
  v8::Isolate* isolate_ = nullptr;
76
  bool start_profiler_idle_notifier_;
77
  uv_thread_t tid_;
78
79
#if HAVE_INSPECTOR
80
  std::unique_ptr<inspector::ParentInspectorHandle> inspector_parent_handle_;
81
#endif
82
83
  // This mutex protects access to all variables listed below it.
84
  mutable Mutex mutex_;
85
86
  bool thread_joined_ = true;
87
  const char* custom_error_ = nullptr;
88
  std::string custom_error_str_;
89
  bool loop_init_failed_ = false;
90
  int exit_code_ = 0;
91
  uint64_t thread_id_ = -1;
92
  uintptr_t stack_base_ = 0;
93
94
  // Custom resource constraints:
95
  double resource_limits_[kTotalResourceLimitCount];
96
  void UpdateResourceConstraints(v8::ResourceConstraints* constraints);
97
98
  // Full size of the thread's stack.
99
  static constexpr size_t kStackSize = 4 * 1024 * 1024;
100
  // Stack buffer size that is not available to the JS engine.
101
  static constexpr size_t kStackBufferSize = 192 * 1024;
102
103
  std::unique_ptr<MessagePortData> child_port_data_;
104
  std::shared_ptr<KVStore> env_vars_;
105
106
  // This is always kept alive because the JS object associated with the Worker
107
  // instance refers to it via its [kPort] property.
108
  MessagePort* parent_port_ = nullptr;
109
110
  // A raw flag that is used by creator and worker threads to
111
  // sync up on pre-mature termination of worker  - while in the
112
  // warmup phase.  Once the worker is fully warmed up, use the
113
  // async handle of the worker's Environment for the same purpose.
114
  bool stopped_ = true;
115
116
  bool has_ref_ = true;
117
118
  // The real Environment of the worker object. It has a lesser
119
  // lifespan than the worker object itself - comes to life
120
  // when the worker thread creates a new Environment, and gets
121
  // destroyed alongwith the worker thread.
122
  Environment* env_ = nullptr;
123
124
  friend class WorkerThreadData;
125
};
126
127
template <typename Fn>
128
2
bool Worker::RequestInterrupt(Fn&& cb) {
129
4
  Mutex::ScopedLock lock(mutex_);
130
2
  if (env_ == nullptr) return false;
131
2
  env_->RequestInterrupt(std::move(cb));
132
2
  return true;
133
}
134
135
}  // namespace worker
136
}  // namespace node
137
138
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
139
140
141
#endif  // SRC_NODE_WORKER_H_