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-09-03 22:13:26 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
  kStackSizeMb,
20
  kTotalResourceLimitCount
21
};
22
23
// A worker thread, as represented in its parent thread.
24
class Worker : public AsyncWrap {
25
 public:
26
  Worker(Environment* env,
27
         v8::Local<v8::Object> wrap,
28
         const std::string& url,
29
         std::shared_ptr<PerIsolateOptions> per_isolate_opts,
30
         std::vector<std::string>&& exec_argv,
31
         std::shared_ptr<KVStore> env_vars);
32
  ~Worker() override;
33
34
  // Run the worker. This is only called from the worker thread.
35
  void Run();
36
37
  // Forcibly exit the thread with a specified exit code. This may be called
38
  // from any thread. `error_code` and `error_message` can be used to create
39
  // a custom `'error'` event before emitting `'exit'`.
40
  void Exit(int code,
41
            const char* error_code = nullptr,
42
            const char* error_message = nullptr);
43
44
  // Wait for the worker thread to stop (in a blocking manner).
45
  void JoinThread();
46
47
  template <typename Fn>
48
  inline bool RequestInterrupt(Fn&& cb);
49
50
  void MemoryInfo(MemoryTracker* tracker) const override;
51
  SET_MEMORY_INFO_NAME(Worker)
52
  SET_SELF_SIZE(Worker)
53
54
  bool is_stopped() const;
55
56
  static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
57
  static void CloneParentEnvVars(
58
      const v8::FunctionCallbackInfo<v8::Value>& args);
59
  static void SetEnvVars(const v8::FunctionCallbackInfo<v8::Value>& args);
60
  static void StartThread(const v8::FunctionCallbackInfo<v8::Value>& args);
61
  static void StopThread(const v8::FunctionCallbackInfo<v8::Value>& args);
62
  static void Ref(const v8::FunctionCallbackInfo<v8::Value>& args);
63
  static void Unref(const v8::FunctionCallbackInfo<v8::Value>& args);
64
  static void GetResourceLimits(
65
      const v8::FunctionCallbackInfo<v8::Value>& args);
66
  v8::Local<v8::Float64Array> GetResourceLimits(v8::Isolate* isolate) const;
67
  static void TakeHeapSnapshot(const v8::FunctionCallbackInfo<v8::Value>& args);
68
69
 private:
70
  void CreateEnvMessagePort(Environment* env);
71
  static size_t NearHeapLimit(void* data, size_t current_heap_limit,
72
                              size_t initial_heap_limit);
73
74
  std::shared_ptr<PerIsolateOptions> per_isolate_opts_;
75
  std::vector<std::string> exec_argv_;
76
  std::vector<std::string> argv_;
77
78
  MultiIsolatePlatform* platform_;
79
  v8::Isolate* isolate_ = nullptr;
80
  uv_thread_t tid_;
81
82
  std::unique_ptr<InspectorParentHandle> inspector_parent_handle_;
83
84
  // This mutex protects access to all variables listed below it.
85
  mutable Mutex mutex_;
86
87
  bool thread_joined_ = true;
88
  const char* custom_error_ = nullptr;
89
  std::string custom_error_str_;
90
  int exit_code_ = 0;
91
  ThreadId thread_id_;
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
  size_t stack_size_ = 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
  uint64_t environment_flags_ = EnvironmentFlags::kNoFlags;
118
119
  // The real Environment of the worker object. It has a lesser
120
  // lifespan than the worker object itself - comes to life
121
  // when the worker thread creates a new Environment, and gets
122
  // destroyed alongwith the worker thread.
123
  Environment* env_ = nullptr;
124
125
  friend class WorkerThreadData;
126
};
127
128
template <typename Fn>
129
2
bool Worker::RequestInterrupt(Fn&& cb) {
130
4
  Mutex::ScopedLock lock(mutex_);
131
2
  if (env_ == nullptr) return false;
132
2
  env_->RequestInterrupt(std::move(cb));
133
2
  return true;
134
}
135
136
}  // namespace worker
137
}  // namespace node
138
139
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
140
141
142
#endif  // SRC_NODE_WORKER_H_