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