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