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 |
|
|
// A worker thread, as represented in its parent thread. |
16 |
|
|
class Worker : public AsyncWrap { |
17 |
|
|
public: |
18 |
|
|
Worker(Environment* env, |
19 |
|
|
v8::Local<v8::Object> wrap, |
20 |
|
|
const std::string& url, |
21 |
|
|
std::shared_ptr<PerIsolateOptions> per_isolate_opts, |
22 |
|
|
std::vector<std::string>&& exec_argv); |
23 |
|
|
~Worker() override; |
24 |
|
|
|
25 |
|
|
// Run the worker. This is only called from the worker thread. |
26 |
|
|
void Run(); |
27 |
|
|
|
28 |
|
|
// Forcibly exit the thread with a specified exit code. This may be called |
29 |
|
|
// from any thread. |
30 |
|
|
void Exit(int code); |
31 |
|
|
|
32 |
|
|
// Wait for the worker thread to stop (in a blocking manner). |
33 |
|
|
void JoinThread(); |
34 |
|
|
|
35 |
|
4 |
void MemoryInfo(MemoryTracker* tracker) const override { |
36 |
|
4 |
tracker->TrackField("parent_port", parent_port_); |
37 |
|
4 |
tracker->TrackInlineField(&on_thread_finished_, "on_thread_finished_"); |
38 |
|
4 |
} |
39 |
|
|
|
40 |
|
4 |
SET_MEMORY_INFO_NAME(Worker) |
41 |
|
4 |
SET_SELF_SIZE(Worker) |
42 |
|
|
|
43 |
|
|
bool is_stopped() const; |
44 |
|
|
std::shared_ptr<ArrayBufferAllocator> array_buffer_allocator(); |
45 |
|
|
|
46 |
|
|
static void New(const v8::FunctionCallbackInfo<v8::Value>& args); |
47 |
|
|
static void CloneParentEnvVars( |
48 |
|
|
const v8::FunctionCallbackInfo<v8::Value>& args); |
49 |
|
|
static void SetEnvVars(const v8::FunctionCallbackInfo<v8::Value>& args); |
50 |
|
|
static void StartThread(const v8::FunctionCallbackInfo<v8::Value>& args); |
51 |
|
|
static void StopThread(const v8::FunctionCallbackInfo<v8::Value>& args); |
52 |
|
|
static void Ref(const v8::FunctionCallbackInfo<v8::Value>& args); |
53 |
|
|
static void Unref(const v8::FunctionCallbackInfo<v8::Value>& args); |
54 |
|
|
|
55 |
|
|
private: |
56 |
|
|
void CreateEnvMessagePort(Environment* env); |
57 |
|
|
|
58 |
|
|
std::shared_ptr<PerIsolateOptions> per_isolate_opts_; |
59 |
|
|
std::vector<std::string> exec_argv_; |
60 |
|
|
std::vector<std::string> argv_; |
61 |
|
|
|
62 |
|
|
MultiIsolatePlatform* platform_; |
63 |
|
|
std::shared_ptr<ArrayBufferAllocator> array_buffer_allocator_; |
64 |
|
|
v8::Isolate* isolate_ = nullptr; |
65 |
|
|
bool start_profiler_idle_notifier_; |
66 |
|
|
uv_thread_t tid_; |
67 |
|
|
|
68 |
|
|
#if NODE_USE_V8_PLATFORM && HAVE_INSPECTOR |
69 |
|
|
std::unique_ptr<inspector::ParentInspectorHandle> inspector_parent_handle_; |
70 |
|
|
#endif |
71 |
|
|
|
72 |
|
|
// This mutex protects access to all variables listed below it. |
73 |
|
|
mutable Mutex mutex_; |
74 |
|
|
|
75 |
|
|
bool thread_joined_ = true; |
76 |
|
|
int exit_code_ = 0; |
77 |
|
|
uint64_t thread_id_ = -1; |
78 |
|
|
uintptr_t stack_base_ = 0; |
79 |
|
|
|
80 |
|
|
// Full size of the thread's stack. |
81 |
|
|
static constexpr size_t kStackSize = 4 * 1024 * 1024; |
82 |
|
|
// Stack buffer size that is not available to the JS engine. |
83 |
|
|
static constexpr size_t kStackBufferSize = 192 * 1024; |
84 |
|
|
|
85 |
|
|
std::unique_ptr<MessagePortData> child_port_data_; |
86 |
|
|
std::shared_ptr<KVStore> env_vars_; |
87 |
|
|
|
88 |
|
|
// The child port is kept alive by the child Environment's persistent |
89 |
|
|
// handle to it, as long as that child Environment exists. |
90 |
|
|
MessagePort* child_port_ = nullptr; |
91 |
|
|
// This is always kept alive because the JS object associated with the Worker |
92 |
|
|
// instance refers to it via its [kPort] property. |
93 |
|
|
MessagePort* parent_port_ = nullptr; |
94 |
|
|
|
95 |
|
|
AsyncRequest on_thread_finished_; |
96 |
|
|
|
97 |
|
|
// A raw flag that is used by creator and worker threads to |
98 |
|
|
// sync up on pre-mature termination of worker - while in the |
99 |
|
|
// warmup phase. Once the worker is fully warmed up, use the |
100 |
|
|
// async handle of the worker's Environment for the same purpose. |
101 |
|
|
bool stopped_ = true; |
102 |
|
|
|
103 |
|
|
// The real Environment of the worker object. It has a lesser |
104 |
|
|
// lifespan than the worker object itself - comes to life |
105 |
|
|
// when the worker thread creates a new Environment, and gets |
106 |
|
|
// destroyed alongwith the worker thread. |
107 |
|
|
Environment* env_ = nullptr; |
108 |
|
|
|
109 |
|
|
friend class WorkerThreadData; |
110 |
|
|
}; |
111 |
|
|
|
112 |
|
|
} // namespace worker |
113 |
|
|
} // namespace node |
114 |
|
|
|
115 |
|
|
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS |
116 |
|
|
|
117 |
|
|
|
118 |
|
|
#endif // SRC_NODE_WORKER_H_ |