Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
7a5e327
src: explicitly register built-in modules
yhwang Oct 22, 2017
6e98aa9
src: use unique pointer for tracing_agent
fhinkel Nov 12, 2017
2bffd70
async_hooks: add trace events to async_hooks
AndreasMadsen Jul 17, 2017
07e7f2e
async_hooks: add destroy event for gced AsyncResources
Sebmaster Nov 9, 2017
1a66f91
src: use NODE_BUILTIN_MODULE_CONTEXT_AWARE() macro
bnoordhuis Nov 16, 2017
b7e3109
src: rename async-wrap -> async_wrap
danbev Nov 14, 2017
8c83406
timers: cross JS/C++ border less frequently
addaleax Nov 15, 2017
d66d481
async_hooks: deprecate undocumented API
AndreasMadsen Nov 12, 2017
c904ce1
src: introduce internal C++ SetImmediate() mechanism
addaleax Nov 18, 2017
590cf4a
src: remove async_hooks destroy timer handle
addaleax Nov 18, 2017
d32d180
trace_events: add executionAsyncId to init events
AndreasMadsen Nov 24, 2017
44cbf56
async_wrap: add provider types for net server
AndreasMadsen Nov 20, 2017
1044e76
async_hooks: rename initTriggerId
AndreasMadsen Nov 22, 2017
a04d5ee
async_hooks: separate missing from default context
AndreasMadsen Nov 22, 2017
44f4c73
async_hooks: use scope for defaultTriggerAsyncId
AndreasMadsen Nov 22, 2017
7310f66
src: remove unused async hooks methods
addaleax Dec 19, 2017
4e656fd
async_hooks: use CHECK instead of throwing error
maclover7 Dec 23, 2017
067bd2a
async_hooks: use typed array stack as fast path
addaleax Dec 19, 2017
af928ef
trace_events: stop tracing agent in process.exit()
AndreasMadsen Jan 5, 2018
1efac0e
async_hooks: update defaultTriggerAsyncIdScope for perf
apapirovski Jan 5, 2018
3ba594f
async_hooks,http: set HTTPParser trigger to socket
AndreasMadsen Jan 5, 2018
8937440
async_hooks,test: only use IPv6 in http test
AndreasMadsen Jan 14, 2018
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
Prev Previous commit
Next Next commit
src: introduce internal C++ SetImmediate() mechanism
PR-URL: #17117
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
addaleax authored and AndreasMadsen committed Jan 16, 2018
commit c904ce1248c1fefb565dfc5cc04f507870264be8
7 changes: 7 additions & 0 deletions src/env-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,13 @@ Environment::scheduled_immediate_count() {
return scheduled_immediate_count_;
}

void Environment::SetImmediate(native_immediate_callback cb, void* data) {
native_immediate_callbacks_.push_back({ cb, data });
if (scheduled_immediate_count_[0] == 0)
ActivateImmediateCheck();
scheduled_immediate_count_[0] = scheduled_immediate_count_[0] + 1;
}

inline performance::performance_state* Environment::performance_state() {
return performance_state_;
}
Expand Down
52 changes: 52 additions & 0 deletions src/env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -223,4 +223,56 @@ void Environment::EnvPromiseHook(v8::PromiseHookType type,
}
}

void Environment::RunAndClearNativeImmediates() {
size_t count = native_immediate_callbacks_.size();
if (count > 0) {
std::vector<NativeImmediateCallback> list;
native_immediate_callbacks_.swap(list);
for (const auto& cb : list) {
cb.cb_(this, cb.data_);
}

#ifdef DEBUG
CHECK_GE(scheduled_immediate_count_[0], count);
#endif
scheduled_immediate_count_[0] = scheduled_immediate_count_[0] - count;
}
}

static bool MaybeStopImmediate(Environment* env) {
if (env->scheduled_immediate_count()[0] == 0) {
uv_check_stop(env->immediate_check_handle());
uv_idle_stop(env->immediate_idle_handle());
return true;
}
return false;
}


void Environment::CheckImmediate(uv_check_t* handle) {
Environment* env = Environment::from_immediate_check_handle(handle);
HandleScope scope(env->isolate());
Context::Scope context_scope(env->context());

if (MaybeStopImmediate(env))
return;

env->RunAndClearNativeImmediates();

MakeCallback(env->isolate(),
env->process_object(),
env->immediate_callback_string(),
0,
nullptr,
{0, 0}).ToLocalChecked();

MaybeStopImmediate(env);
}

void Environment::ActivateImmediateCheck() {
uv_check_start(&immediate_check_handle_, CheckImmediate);
// Idle handle is needed only to stop the event loop from blocking in poll.
uv_idle_start(&immediate_idle_handle_, [](uv_idle_t*){ });
}

} // namespace node
13 changes: 13 additions & 0 deletions src/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,11 @@ class Environment {
bool RemovePromiseHook(promise_hook_func fn, void* arg);
bool EmitNapiWarning();

typedef void (*native_immediate_callback)(Environment* env, void* data);
inline void SetImmediate(native_immediate_callback cb, void* data);
// This needs to be available for the JS-land setImmediate().
void ActivateImmediateCheck();

private:
inline void ThrowError(v8::Local<v8::Value> (*fun)(v8::Local<v8::String>),
const char* errmsg);
Expand Down Expand Up @@ -753,6 +758,14 @@ class Environment {
};
std::vector<PromiseHookCallback> promise_hooks_;

struct NativeImmediateCallback {
native_immediate_callback cb_;
void* data_;
};
std::vector<NativeImmediateCallback> native_immediate_callbacks_;
void RunAndClearNativeImmediates();
static void CheckImmediate(uv_check_t* handle);

static void EnvPromiseHook(v8::PromiseHookType type,
v8::Local<v8::Promise> promise,
v8::Local<v8::Value> parent);
Expand Down
33 changes: 1 addition & 32 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3265,40 +3265,9 @@ static void DebugEnd(const FunctionCallbackInfo<Value>& args);

namespace {

bool MaybeStopImmediate(Environment* env) {
if (env->scheduled_immediate_count()[0] == 0) {
uv_check_stop(env->immediate_check_handle());
uv_idle_stop(env->immediate_idle_handle());
return true;
}
return false;
}

void CheckImmediate(uv_check_t* handle) {
Environment* env = Environment::from_immediate_check_handle(handle);
HandleScope scope(env->isolate());
Context::Scope context_scope(env->context());

if (MaybeStopImmediate(env))
return;

MakeCallback(env->isolate(),
env->process_object(),
env->immediate_callback_string(),
0,
nullptr,
{0, 0}).ToLocalChecked();

MaybeStopImmediate(env);
}


void ActivateImmediateCheck(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
uv_check_start(env->immediate_check_handle(), CheckImmediate);
// Idle handle is needed only to stop the event loop from blocking in poll.
uv_idle_start(env->immediate_idle_handle(),
[](uv_idle_t*){ /* do nothing, just keep the loop running */ });
env->ActivateImmediateCheck();
}


Expand Down