Skip to content
This repository has been archived by the owner on Aug 31, 2018. It is now read-only.

worker: apply some preparations to Ayo’s internals #82

Merged
merged 6 commits into from
Sep 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions node.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,6 @@
'src/signal_wrap.cc',
'src/spawn_sync.cc',
'src/string_bytes.cc',
'src/string_search.cc',
'src/stream_base.cc',
'src/stream_wrap.cc',
'src/tcp_wrap.cc',
Expand Down Expand Up @@ -687,7 +686,6 @@
'<(OBJ_PATH)<(OBJ_SEPARATOR)node_url.<(OBJ_SUFFIX)',
'<(OBJ_PATH)<(OBJ_SEPARATOR)util.<(OBJ_SUFFIX)',
'<(OBJ_PATH)<(OBJ_SEPARATOR)string_bytes.<(OBJ_SUFFIX)',
'<(OBJ_PATH)<(OBJ_SEPARATOR)string_search.<(OBJ_SUFFIX)',
'<(OBJ_PATH)<(OBJ_SEPARATOR)stream_base.<(OBJ_SUFFIX)',
'<(OBJ_PATH)<(OBJ_SEPARATOR)node_constants.<(OBJ_SUFFIX)',
'<(OBJ_TRACING_PATH)<(OBJ_SEPARATOR)agent.<(OBJ_SUFFIX)',
Expand Down
18 changes: 14 additions & 4 deletions src/async-wrap-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,13 @@ inline v8::MaybeLocal<v8::Value> AsyncWrap::MakeCallback(
const v8::Local<v8::String> symbol,
int argc,
v8::Local<v8::Value>* argv) {
v8::Local<v8::Value> cb_v = object()->Get(symbol);
CHECK(cb_v->IsFunction());
v8::Local<v8::Value> cb_v;
if (!object()->Get(object()->CreationContext(), symbol).ToLocal(&cb_v))
return v8::MaybeLocal<v8::Value>();
if (!cb_v->IsFunction()) {
env()->ThrowError("callback must be a function");
return v8::MaybeLocal<v8::Value>();
}
return MakeCallback(cb_v.As<v8::Function>(), argc, argv);
}

Expand All @@ -60,8 +65,13 @@ inline v8::MaybeLocal<v8::Value> AsyncWrap::MakeCallback(
uint32_t index,
int argc,
v8::Local<v8::Value>* argv) {
v8::Local<v8::Value> cb_v = object()->Get(index);
CHECK(cb_v->IsFunction());
v8::Local<v8::Value> cb_v;
if (!object()->Get(object()->CreationContext(), index).ToLocal(&cb_v))
return v8::MaybeLocal<v8::Value>();
if (!cb_v->IsFunction()) {
env()->ThrowError("callback must be a function");
return v8::MaybeLocal<v8::Value>();
}
return MakeCallback(cb_v.As<v8::Function>(), argc, argv);
}

Expand Down
4 changes: 4 additions & 0 deletions src/async-wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ static void DestroyIdsCb(uv_timer_t* handle) {
do {
std::vector<double> destroy_ids_list;
destroy_ids_list.swap(*env->destroy_ids_list());
if (!env->can_call_into_js()) return;
for (auto current_id : destroy_ids_list) {
// Want each callback to be cleaned up after itself, instead of cleaning
// them all up after the while() loop completes.
Expand All @@ -174,6 +175,9 @@ static void PushBackDestroyId(Environment* env, double id) {
if (env->async_hooks()->fields()[AsyncHooks::kDestroy] == 0)
return;

if (!env->can_call_into_js())
return;

if (env->destroy_ids_list()->empty())
uv_timer_start(env->destroy_ids_timer_handle(), DestroyIdsCb, 0, 0);

Expand Down
5 changes: 5 additions & 0 deletions src/cares_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ using v8::Value;

namespace {

Mutex ares_library_mutex;

inline uint16_t cares_get_16bit(const unsigned char* p) {
return static_cast<uint32_t>(p[0] << 8U) | (static_cast<uint32_t>(p[1]));
}
Expand Down Expand Up @@ -496,6 +498,7 @@ void ChannelWrap::Setup() {

int r;
if (!library_inited_) {
Mutex::ScopedLock lock(ares_library_mutex);
// Multiple calls to ares_library_init() increase a reference counter,
// so this is a no-op except for the first call to it.
r = ares_library_init(ARES_LIB_INIT_ALL);
Expand All @@ -509,6 +512,7 @@ void ChannelWrap::Setup() {
ARES_OPT_FLAGS | ARES_OPT_SOCK_STATE_CB);

if (r != ARES_SUCCESS) {
Mutex::ScopedLock lock(ares_library_mutex);
ares_library_cleanup();
return env()->ThrowError(ToErrorCodeString(r));
}
Expand All @@ -526,6 +530,7 @@ void ChannelWrap::Setup() {

ChannelWrap::~ChannelWrap() {
if (library_inited_) {
Mutex::ScopedLock lock(ares_library_mutex);
// This decreases the reference counter increased by ares_library_init().
ares_library_cleanup();
}
Expand Down
29 changes: 29 additions & 0 deletions src/env-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@ inline Environment::~Environment() {
delete[] heap_space_statistics_buffer_;
delete[] http_parser_buffer_;
delete http2_state_;
delete[] fs_stats_field_array_;
free(performance_state_);
}

Expand Down Expand Up @@ -503,6 +504,14 @@ inline void Environment::set_fs_stats_field_array(double* fields) {
fs_stats_field_array_ = fields;
}

inline bool Environment::can_call_into_js() const {
return can_call_into_js_;
}

inline void Environment::set_can_call_into_js(bool can_call_into_js) {
can_call_into_js_ = can_call_into_js;
}

inline performance::performance_state* Environment::performance_state() {
return performance_state_;
}
Expand Down Expand Up @@ -596,6 +605,26 @@ inline void Environment::SetTemplateMethod(v8::Local<v8::FunctionTemplate> that,
t->SetClassName(name_string); // NODE_SET_METHOD() compatibility.
}

void Environment::AddCleanupHook(void (*fn)(void*), void* arg) {
cleanup_hooks_[arg].push_back(
CleanupHookCallback { fn, arg, cleanup_hook_counter_++ });
}

void Environment::RemoveCleanupHook(void (*fn)(void*), void* arg) {
auto map_it = cleanup_hooks_.find(arg);
if (map_it == cleanup_hooks_.end())
return;

for (auto it = map_it->second.begin(); it != map_it->second.end(); ++it) {
if (it->fun_ == fn && it->arg_ == arg) {
map_it->second.erase(it);
if (map_it->second.empty())
cleanup_hooks_.erase(arg);
return;
}
}
}

#define VP(PropertyName, StringValue) V(v8::Private, PropertyName)
#define VS(PropertyName, StringValue) V(v8::String, PropertyName)
#define V(TypeName, PropertyName) \
Expand Down
20 changes: 20 additions & 0 deletions src/env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,26 @@ void Environment::PrintSyncTrace() const {
fflush(stderr);
}

void Environment::RunCleanup() {
while (!cleanup_hooks_.empty()) {
std::vector<CleanupHookCallback> callbacks;
// Concatenate all vectors in cleanup_hooks_
for (const auto& pair : cleanup_hooks_)
callbacks.insert(callbacks.end(), pair.second.begin(), pair.second.end());
cleanup_hooks_.clear();
std::sort(callbacks.begin(), callbacks.end(),
[](const CleanupHookCallback& a, const CleanupHookCallback& b) {
// Sort in descending order so that the last-inserted callbacks get run
// first.
return a.insertion_order_counter_ > b.insertion_order_counter_;
});

for (const CleanupHookCallback& cb : callbacks) {
cb.fun_(cb.arg_);
}
}
}

void Environment::RunAtExitCallbacks() {
for (AtExitCallback at_exit : at_exit_functions_) {
at_exit.cb_(at_exit.arg_);
Expand Down
20 changes: 19 additions & 1 deletion src/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,9 @@ class Environment {
inline performance::performance_state* performance_state();
inline std::map<std::string, uint64_t>* performance_marks();

inline bool can_call_into_js() const;
inline void set_can_call_into_js(bool can_call_into_js);

inline void ThrowError(const char* errmsg);
inline void ThrowTypeError(const char* errmsg);
inline void ThrowRangeError(const char* errmsg);
Expand Down Expand Up @@ -678,6 +681,10 @@ class Environment {
bool RemovePromiseHook(promise_hook_func fn, void* arg);
bool EmitNapiWarning();

inline void AddCleanupHook(void (*fn)(void*), void* arg);
inline void RemoveCleanupHook(void (*fn)(void*), void* arg);
void RunCleanup();

private:
inline void ThrowError(v8::Local<v8::Value> (*fun)(v8::Local<v8::String>),
const char* errmsg);
Expand All @@ -702,6 +709,8 @@ class Environment {
size_t makecallback_cntr_;
std::vector<double> destroy_ids_list_;

bool can_call_into_js_ = true;

performance::performance_state* performance_state_ = nullptr;
std::map<std::string, uint64_t> performance_marks_;

Expand All @@ -721,7 +730,7 @@ class Environment {
char* http_parser_buffer_;
http2::http2_state* http2_state_ = nullptr;

double* fs_stats_field_array_;
double* fs_stats_field_array_ = nullptr;

struct AtExitCallback {
void (*cb_)(void* arg);
Expand All @@ -736,6 +745,15 @@ class Environment {
};
std::vector<PromiseHookCallback> promise_hooks_;

struct CleanupHookCallback {
void (*fun_)(void*);
void* arg_;
int64_t insertion_order_counter_;
};

std::unordered_map<void*, std::vector<CleanupHookCallback>> cleanup_hooks_;
int64_t cleanup_hook_counter_ = 0;

static void EnvPromiseHook(v8::PromiseHookType type,
v8::Local<v8::Promise> promise,
v8::Local<v8::Value> parent);
Expand Down
40 changes: 40 additions & 0 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,9 @@ using v8::Value;

using AsyncHooks = node::Environment::AsyncHooks;

static Mutex process_mutex;
static Mutex environ_mutex;

static bool print_eval = false;
static bool force_repl = false;
static bool syntax_check_only = false;
Expand Down Expand Up @@ -1339,6 +1342,22 @@ void AddPromiseHook(v8::Isolate* isolate, promise_hook_func fn, void* arg) {
env->AddPromiseHook(fn, arg);
}

void AddEnvironmentCleanupHook(v8::Isolate* isolate,
void (*fun)(void* arg),
void* arg) {
Environment* env = Environment::GetCurrent(isolate);
env->AddCleanupHook(fun, arg);
}


void RemoveEnvironmentCleanupHook(v8::Isolate* isolate,
void (*fun)(void* arg),
void* arg) {
Environment* env = Environment::GetCurrent(isolate);
env->RemoveCleanupHook(fun, arg);
}


CallbackScope::CallbackScope(Isolate* isolate,
Local<Object> object,
async_context asyncContext)
Expand Down Expand Up @@ -1367,6 +1386,11 @@ InternalCallbackScope::InternalCallbackScope(Environment* env,
CHECK(!object.IsEmpty());
}

if (!env->can_call_into_js()) {
failed_ = true;
return;
}

HandleScope handle_scope(env->isolate());
// If you hit this assertion, you forgot to enter the v8::Context first.
CHECK_EQ(env->context(), env->isolate()->GetCurrentContext());
Expand Down Expand Up @@ -1414,6 +1438,7 @@ void InternalCallbackScope::Close() {

Environment::TickInfo* tick_info = env_->tick_info();

if (!env_->can_call_into_js()) return;
if (tick_info->length() == 0) {
env_->isolate()->RunMicrotasks();
}
Expand All @@ -1433,6 +1458,8 @@ void InternalCallbackScope::Close() {
CHECK_EQ(env_->current_async_id(), 0);
CHECK_EQ(env_->trigger_id(), 0);

if (!env_->can_call_into_js()) return;

if (env_->tick_callback_function()->Call(process, 0, nullptr).IsEmpty()) {
failed_ = true;
}
Expand Down Expand Up @@ -1792,6 +1819,7 @@ void AppendExceptionLine(Environment* env,
if (!can_set_arrow || (mode == FATAL_ERROR && !err_obj->IsNativeError())) {
if (env->printed_error())
return;
Mutex::ScopedLock lock(process_mutex);
env->set_printed_error(true);

uv_tty_reset_mode();
Expand Down Expand Up @@ -2955,6 +2983,7 @@ static void LinkedBinding(const FunctionCallbackInfo<Value>& args) {

static void ProcessTitleGetter(Local<Name> property,
const PropertyCallbackInfo<Value>& info) {
Mutex::ScopedLock lock(process_mutex);
char buffer[512];
uv_get_process_title(buffer, sizeof(buffer));
info.GetReturnValue().Set(String::NewFromUtf8(info.GetIsolate(), buffer));
Expand All @@ -2964,6 +2993,7 @@ static void ProcessTitleGetter(Local<Name> property,
static void ProcessTitleSetter(Local<Name> property,
Local<Value> value,
const PropertyCallbackInfo<void>& info) {
Mutex::ScopedLock lock(process_mutex);
node::Utf8Value title(info.GetIsolate(), value);
// TODO(piscisaureus): protect with a lock
uv_set_process_title(*title);
Expand All @@ -2972,6 +3002,7 @@ static void ProcessTitleSetter(Local<Name> property,

static void EnvGetter(Local<Name> property,
const PropertyCallbackInfo<Value>& info) {
Mutex::ScopedLock lock(environ_mutex);
Isolate* isolate = info.GetIsolate();
if (property->IsSymbol()) {
return info.GetReturnValue().SetUndefined();
Expand Down Expand Up @@ -3004,6 +3035,7 @@ static void EnvGetter(Local<Name> property,
static void EnvSetter(Local<Name> property,
Local<Value> value,
const PropertyCallbackInfo<Value>& info) {
Mutex::ScopedLock lock(environ_mutex);
#ifdef __POSIX__
node::Utf8Value key(info.GetIsolate(), property);
node::Utf8Value val(info.GetIsolate(), value);
Expand All @@ -3024,6 +3056,7 @@ static void EnvSetter(Local<Name> property,

static void EnvQuery(Local<Name> property,
const PropertyCallbackInfo<Integer>& info) {
Mutex::ScopedLock lock(environ_mutex);
int32_t rc = -1; // Not found unless proven otherwise.
if (property->IsString()) {
#ifdef __POSIX__
Expand Down Expand Up @@ -3052,6 +3085,7 @@ static void EnvQuery(Local<Name> property,

static void EnvDeleter(Local<Name> property,
const PropertyCallbackInfo<Boolean>& info) {
Mutex::ScopedLock lock(environ_mutex);
if (property->IsString()) {
#ifdef __POSIX__
node::Utf8Value key(info.GetIsolate(), property);
Expand All @@ -3070,6 +3104,7 @@ static void EnvDeleter(Local<Name> property,


static void EnvEnumerator(const PropertyCallbackInfo<Array>& info) {
Mutex::ScopedLock lock(environ_mutex);
Environment* env = Environment::GetCurrent(info);
Isolate* isolate = env->isolate();
Local<Context> ctx = env->context();
Expand Down Expand Up @@ -3193,6 +3228,7 @@ static Local<Object> GetFeatures(Environment* env) {

static void DebugPortGetter(Local<Name> property,
const PropertyCallbackInfo<Value>& info) {
Mutex::ScopedLock lock(process_mutex);
int port = debug_options.port();
#if HAVE_INSPECTOR
if (port == 0) {
Expand All @@ -3208,6 +3244,7 @@ static void DebugPortGetter(Local<Name> property,
static void DebugPortSetter(Local<Name> property,
Local<Value> value,
const PropertyCallbackInfo<void>& info) {
Mutex::ScopedLock lock(process_mutex);
debug_options.set_port(value->Int32Value());
}

Expand Down Expand Up @@ -4726,6 +4763,9 @@ inline int Start(Isolate* isolate, IsolateData* isolate_data,
env.set_trace_sync_io(false);

const int exit_code = EmitExit(&env);

env.set_can_call_into_js(false);
env.RunCleanup();
RunAtExit(&env);
uv_key_delete(&thread_local_env);

Expand Down
Loading