Skip to content

Commit

Permalink
squash: move code into Environment to avoid dupl
Browse files Browse the repository at this point in the history
  • Loading branch information
danbev committed Feb 13, 2018
1 parent e047cb0 commit 6dee9c8
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 13 deletions.
9 changes: 9 additions & 0 deletions src/env-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,10 @@ inline Environment* Environment::GetCurrent(
info.Data().template As<v8::External>()->Value());
}

inline Environment* Environment::GetThreadLocalEnv() {
return static_cast<Environment*>(uv_key_get(&thread_local_env));
}

inline Environment::Environment(IsolateData* isolate_data,
v8::Local<v8::Context> context)
: isolate_(context->GetIsolate()),
Expand Down Expand Up @@ -369,6 +373,11 @@ inline Environment::~Environment() {
ENVIRONMENT_STRONG_PERSISTENT_PROPERTIES(V)
#undef V

auto tl_env = static_cast<Environment*>(uv_key_get(&thread_local_env));
if (tl_env == this) {
uv_key_delete(&Environment::thread_local_env);
}

delete[] heap_statistics_buffer_;
delete[] heap_space_statistics_buffer_;
delete[] http_parser_buffer_;
Expand Down
5 changes: 5 additions & 0 deletions src/env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ void Environment::Start(int argc,

SetupProcessObject(this, argc, argv, exec_argc, exec_argv);
LoadAsyncWrapperInfo(this);

CHECK_EQ(0, uv_key_create(&thread_local_env));
uv_key_set(&thread_local_env, this);
}

void Environment::CleanupHandles() {
Expand Down Expand Up @@ -471,4 +474,6 @@ void Environment::AsyncHooks::grow_async_ids_stack() {
async_ids_stack_.GetJSArray()).FromJust();
}

uv_key_t Environment::thread_local_env = {};

} // namespace node
5 changes: 5 additions & 0 deletions src/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,7 @@ struct ContextInfo {
bool is_default = false;
};


class Environment {
public:
class AsyncHooks {
Expand Down Expand Up @@ -544,6 +545,8 @@ class Environment {
static inline Environment* GetCurrent(
const v8::PropertyCallbackInfo<T>& info);

static inline Environment* GetThreadLocalEnv();

inline Environment(IsolateData* isolate_data, v8::Local<v8::Context> context);
inline ~Environment();

Expand Down Expand Up @@ -832,6 +835,8 @@ class Environment {
v8::Local<v8::Promise> promise,
v8::Local<v8::Value> parent);

static uv_key_t thread_local_env;

#define V(PropertyName, TypeName) \
v8::Persistent<TypeName> PropertyName ## _;
ENVIRONMENT_STRONG_PERSISTENT_PROPERTIES(V)
Expand Down
14 changes: 1 addition & 13 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4207,11 +4207,8 @@ uv_loop_t* GetCurrentEventLoop(v8::Isolate* isolate) {
}


static uv_key_t thread_local_env;


void AtExit(void (*cb)(void* arg), void* arg) {
auto env = static_cast<Environment*>(uv_key_get(&thread_local_env));
auto env = Environment::GetThreadLocalEnv();
AtExit(env, cb, arg);
}

Expand Down Expand Up @@ -4296,18 +4293,12 @@ Environment* CreateEnvironment(IsolateData* isolate_data,
HandleScope handle_scope(isolate);
Context::Scope context_scope(context);
auto env = new Environment(isolate_data, context);
CHECK_EQ(0, uv_key_create(&thread_local_env));
uv_key_set(&thread_local_env, env);
env->Start(argc, argv, exec_argc, exec_argv, v8_is_profiling);
return env;
}


void FreeEnvironment(Environment* env) {
auto tl_env = static_cast<Environment*>(uv_key_get(&thread_local_env));
if (tl_env == env) {
uv_key_delete(&thread_local_env);
}
delete env;
}

Expand Down Expand Up @@ -4348,8 +4339,6 @@ inline int Start(Isolate* isolate, IsolateData* isolate_data,
Local<Context> context = NewContext(isolate);
Context::Scope context_scope(context);
Environment env(isolate_data, context);
CHECK_EQ(0, uv_key_create(&thread_local_env));
uv_key_set(&thread_local_env, &env);
env.Start(argc, argv, exec_argc, exec_argv, v8_is_profiling);

const char* path = argc > 1 ? argv[1] : nullptr;
Expand Down Expand Up @@ -4399,7 +4388,6 @@ inline int Start(Isolate* isolate, IsolateData* isolate_data,

const int exit_code = EmitExit(&env);
RunAtExit(&env);
uv_key_delete(&thread_local_env);

v8_platform.DrainVMTasks(isolate);
v8_platform.CancelVMTasks(isolate);
Expand Down

0 comments on commit 6dee9c8

Please sign in to comment.