async_wrap: new instances get uid

New instances of AsyncWrap are automatically assigned a unique id. The
value will be used in future commits to communicate additional
information via the async hooks.

While the largest value we can reliably communicate to JS is 2^53, even
if a new AsyncWrap is created every 100ns the uid won't reach its end
for 28.5 years.

PR-URL: https://github.com/nodejs/node/pull/3461
Reviewed-By: Fedor Indutny <fedor@indutny.com>
This commit is contained in:
Trevor Norris 2015-10-19 17:14:05 -06:00
parent a4e9487aae
commit 80a66ba6ae
4 changed files with 18 additions and 1 deletions

View File

@ -17,7 +17,8 @@ inline AsyncWrap::AsyncWrap(Environment* env,
v8::Local<v8::Object> object,
ProviderType provider,
AsyncWrap* parent)
: BaseObject(env, object), bits_(static_cast<uint32_t>(provider) << 1) {
: BaseObject(env, object), bits_(static_cast<uint32_t>(provider) << 1),
uid_(env->get_async_wrap_uid()) {
CHECK_NE(provider, PROVIDER_NONE);
CHECK_GE(object->InternalFieldCount(), 1);
@ -66,6 +67,11 @@ inline AsyncWrap::ProviderType AsyncWrap::provider_type() const {
}
inline int64_t AsyncWrap::get_uid() const {
return uid_;
}
inline v8::Local<v8::Value> AsyncWrap::MakeCallback(
const v8::Local<v8::String> symbol,
int argc,

View File

@ -55,6 +55,8 @@ class AsyncWrap : public BaseObject {
inline ProviderType provider_type() const;
inline int64_t get_uid() const;
// Only call these within a valid HandleScope.
v8::Local<v8::Value> MakeCallback(const v8::Local<v8::Function> cb,
int argc,
@ -76,6 +78,7 @@ class AsyncWrap : public BaseObject {
// expected the context object will receive a _asyncQueue object property
// that will be used to call pre/post in MakeCallback.
uint32_t bits_;
const int64_t uid_;
};
void LoadAsyncWrapperInfo(Environment* env);

View File

@ -210,6 +210,7 @@ inline Environment::Environment(v8::Local<v8::Context> context,
using_domains_(false),
printed_error_(false),
trace_sync_io_(false),
async_wrap_uid_(0),
debugger_agent_(this),
http_parser_buffer_(nullptr),
context_(context->GetIsolate(), context) {
@ -359,6 +360,10 @@ inline void Environment::set_trace_sync_io(bool value) {
trace_sync_io_ = value;
}
inline int64_t Environment::get_async_wrap_uid() {
return ++async_wrap_uid_;
}
inline uint32_t* Environment::heap_statistics_buffer() const {
CHECK_NE(heap_statistics_buffer_, nullptr);
return heap_statistics_buffer_;

View File

@ -446,6 +446,8 @@ class Environment {
void PrintSyncTrace() const;
inline void set_trace_sync_io(bool value);
inline int64_t get_async_wrap_uid();
bool KickNextTick();
inline uint32_t* heap_statistics_buffer() const;
@ -541,6 +543,7 @@ class Environment {
bool using_domains_;
bool printed_error_;
bool trace_sync_io_;
int64_t async_wrap_uid_;
debugger::Agent debugger_agent_;
HandleWrapQueue handle_wrap_queue_;