Skip to content

Commit

Permalink
async_wrap: new instances get uid
Browse files Browse the repository at this point in the history
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: #3461
Reviewed-By: Fedor Indutny <fedor@indutny.com>
  • Loading branch information
trevnorris authored and rvagg committed Nov 7, 2015
1 parent 5d34c81 commit eccbec9
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/async-wrap-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions src/async-wrap.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
Expand Down
5 changes: 5 additions & 0 deletions src/env-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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_;
Expand Down
3 changes: 3 additions & 0 deletions src/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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_;
Expand Down

0 comments on commit eccbec9

Please sign in to comment.