mirror of
https://github.com/nodejs/node.git
synced 2026-11-09 15:11:11 -05:00
- Use camel case names for memory retainers inherited from AsyncWrap instead of their provider names (which are all in upper case) - Assign class names to wraps so that they appear in the heap snapshot as nodes with class names as node names. Previously some nodes are named with reference names, which are supposed to be edge names instead. PR-URL: https://github.com/nodejs/node/pull/21939 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de>
94 lines
2.8 KiB
C++
94 lines
2.8 KiB
C++
#ifndef SRC_NODE_WORKER_H_
|
|
#define SRC_NODE_WORKER_H_
|
|
|
|
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
|
|
|
|
#include "node_messaging.h"
|
|
#include <unordered_map>
|
|
|
|
namespace node {
|
|
namespace worker {
|
|
|
|
// A worker thread, as represented in its parent thread.
|
|
class Worker : public AsyncWrap {
|
|
public:
|
|
Worker(Environment* env, v8::Local<v8::Object> wrap);
|
|
~Worker();
|
|
|
|
// Run the worker. This is only called from the worker thread.
|
|
void Run();
|
|
|
|
// Forcibly exit the thread with a specified exit code. This may be called
|
|
// from any thread.
|
|
void Exit(int code);
|
|
|
|
// Wait for the worker thread to stop (in a blocking manner).
|
|
void JoinThread();
|
|
|
|
void MemoryInfo(MemoryTracker* tracker) const override {
|
|
tracker->TrackThis(this);
|
|
tracker->TrackFieldWithSize("isolate_data", sizeof(IsolateData));
|
|
tracker->TrackFieldWithSize("env", sizeof(Environment));
|
|
tracker->TrackFieldWithSize("thread_exit_async", sizeof(uv_async_t));
|
|
tracker->TrackField("parent_port", parent_port_);
|
|
}
|
|
|
|
|
|
ADD_MEMORY_INFO_NAME(Worker)
|
|
|
|
bool is_stopped() const;
|
|
|
|
static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
|
|
static void StartThread(const v8::FunctionCallbackInfo<v8::Value>& args);
|
|
static void StopThread(const v8::FunctionCallbackInfo<v8::Value>& args);
|
|
static void GetMessagePort(const v8::FunctionCallbackInfo<v8::Value>& args);
|
|
static void Ref(const v8::FunctionCallbackInfo<v8::Value>& args);
|
|
static void Unref(const v8::FunctionCallbackInfo<v8::Value>& args);
|
|
|
|
private:
|
|
void OnThreadStopped();
|
|
void DisposeIsolate();
|
|
|
|
uv_loop_t loop_;
|
|
DeleteFnPtr<IsolateData, FreeIsolateData> isolate_data_;
|
|
DeleteFnPtr<Environment, FreeEnvironment> env_;
|
|
v8::Isolate* isolate_ = nullptr;
|
|
DeleteFnPtr<ArrayBufferAllocator, FreeArrayBufferAllocator>
|
|
array_buffer_allocator_;
|
|
uv_thread_t tid_;
|
|
|
|
// This mutex protects access to all variables listed below it.
|
|
mutable Mutex mutex_;
|
|
|
|
// Currently only used for telling the parent thread that the child
|
|
// thread exited.
|
|
std::unique_ptr<uv_async_t> thread_exit_async_;
|
|
bool scheduled_on_thread_stopped_ = false;
|
|
|
|
// This mutex only protects stopped_. If both locks are acquired, this needs
|
|
// to be the latter one.
|
|
mutable Mutex stopped_mutex_;
|
|
bool stopped_ = true;
|
|
|
|
bool thread_joined_ = true;
|
|
int exit_code_ = 0;
|
|
uint64_t thread_id_ = -1;
|
|
|
|
std::unique_ptr<MessagePortData> child_port_data_;
|
|
|
|
// The child port is always kept alive by the child Environment's persistent
|
|
// handle to it.
|
|
MessagePort* child_port_ = nullptr;
|
|
// This is always kept alive because the JS object associated with the Worker
|
|
// instance refers to it via its [kPort] property.
|
|
MessagePort* parent_port_ = nullptr;
|
|
};
|
|
|
|
} // namespace worker
|
|
} // namespace node
|
|
|
|
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
|
|
|
|
|
|
#endif // SRC_NODE_WORKER_H_
|