GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: node_worker.h Lines: 5 7 71.4 %
Date: 2022-02-02 04:14:51 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 Ref(const v8::FunctionCallbackInfo<v8::Value>& args);
65
  static void Unref(const v8::FunctionCallbackInfo<v8::Value>& args);
66
  static void GetResourceLimits(
67
      const v8::FunctionCallbackInfo<v8::Value>& args);
68
  v8::Local<v8::Float64Array> GetResourceLimits(v8::Isolate* isolate) const;
69
  static void TakeHeapSnapshot(const v8::FunctionCallbackInfo<v8::Value>& args);
70
  static void LoopIdleTime(const v8::FunctionCallbackInfo<v8::Value>& args);
71
  static void LoopStartTime(const v8::FunctionCallbackInfo<v8::Value>& args);
72
73
 private:
74
  bool CreateEnvMessagePort(Environment* env);
75
  static size_t NearHeapLimit(void* data, size_t current_heap_limit,
76
                              size_t initial_heap_limit);
77
78
  std::shared_ptr<PerIsolateOptions> per_isolate_opts_;
79
  std::vector<std::string> exec_argv_;
80
  std::vector<std::string> argv_;
81
82
  MultiIsolatePlatform* platform_;
83
  v8::Isolate* isolate_ = nullptr;
84
  std::optional<uv_thread_t> tid_;  // Set while the thread is running
85
86
  std::unique_ptr<InspectorParentHandle> inspector_parent_handle_;
87
88
  // This mutex protects access to all variables listed below it.
89
  mutable Mutex mutex_;
90
91
  const char* custom_error_ = nullptr;
92
  std::string custom_error_str_;
93
  int exit_code_ = 0;
94
  ThreadId thread_id_;
95
  uintptr_t stack_base_ = 0;
96
97
  // Custom resource constraints:
98
  double resource_limits_[kTotalResourceLimitCount];
99
  void UpdateResourceConstraints(v8::ResourceConstraints* constraints);
100
101
  // Full size of the thread's stack.
102
  size_t stack_size_ = 4 * 1024 * 1024;
103
  // Stack buffer size that is not available to the JS engine.
104
  static constexpr size_t kStackBufferSize = 192 * 1024;
105
106
  std::unique_ptr<MessagePortData> child_port_data_;
107
  std::shared_ptr<KVStore> env_vars_;
108
109
  // This is always kept alive because the JS object associated with the Worker
110
  // instance refers to it via its [kPort] property.
111
  MessagePort* parent_port_ = nullptr;
112
113
  // A raw flag that is used by creator and worker threads to
114
  // sync up on pre-mature termination of worker  - while in the
115
  // warmup phase.  Once the worker is fully warmed up, use the
116
  // async handle of the worker's Environment for the same purpose.
117
  bool stopped_ = true;
118
119
  bool has_ref_ = true;
120
  uint64_t environment_flags_ = EnvironmentFlags::kNoFlags;
121
122
  // The real Environment of the worker object. It has a lesser
123
  // lifespan than the worker object itself - comes to life
124
  // when the worker thread creates a new Environment, and gets
125
  // destroyed alongwith the worker thread.
126
  Environment* env_ = nullptr;
127
128
  friend class WorkerThreadData;
129
};
130
131
template <typename Fn>
132
2
bool Worker::RequestInterrupt(Fn&& cb) {
133
4
  Mutex::ScopedLock lock(mutex_);
134
2
  if (env_ == nullptr) return false;
135
2
  env_->RequestInterrupt(std::move(cb));
136
2
  return true;
137
}
138
139
}  // namespace worker
140
}  // namespace node
141
142
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
143
144
145
#endif  // SRC_NODE_WORKER_H_