• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   bool IsNotIndicativeOfMemoryLeakAtExit() const override;
54 
55   bool is_stopped() const;
56 
57   static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
58   static void CloneParentEnvVars(
59       const v8::FunctionCallbackInfo<v8::Value>& args);
60   static void SetEnvVars(const v8::FunctionCallbackInfo<v8::Value>& args);
61   static void StartThread(const v8::FunctionCallbackInfo<v8::Value>& args);
62   static void StopThread(const v8::FunctionCallbackInfo<v8::Value>& args);
63   static void Ref(const v8::FunctionCallbackInfo<v8::Value>& args);
64   static void Unref(const v8::FunctionCallbackInfo<v8::Value>& args);
65   static void GetResourceLimits(
66       const v8::FunctionCallbackInfo<v8::Value>& args);
67   v8::Local<v8::Float64Array> GetResourceLimits(v8::Isolate* isolate) const;
68   static void TakeHeapSnapshot(const v8::FunctionCallbackInfo<v8::Value>& args);
69   static void LoopIdleTime(const v8::FunctionCallbackInfo<v8::Value>& args);
70   static void LoopStartTime(const v8::FunctionCallbackInfo<v8::Value>& args);
71 
72  private:
73   bool CreateEnvMessagePort(Environment* env);
74   static size_t NearHeapLimit(void* data, size_t current_heap_limit,
75                               size_t initial_heap_limit);
76 
77   std::shared_ptr<PerIsolateOptions> per_isolate_opts_;
78   std::vector<std::string> exec_argv_;
79   std::vector<std::string> argv_;
80 
81   MultiIsolatePlatform* platform_;
82   v8::Isolate* isolate_ = nullptr;
83   uv_thread_t tid_;
84 
85   std::unique_ptr<InspectorParentHandle> inspector_parent_handle_;
86 
87   // This mutex protects access to all variables listed below it.
88   mutable Mutex mutex_;
89 
90   bool thread_joined_ = true;
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>
RequestInterrupt(Fn && cb)132 bool Worker::RequestInterrupt(Fn&& cb) {
133   Mutex::ScopedLock lock(mutex_);
134   if (env_ == nullptr) return false;
135   env_->RequestInterrupt(std::move(cb));
136   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_
146