Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
Add node::Loop() and don't inc node_isolate.h in *.cc
node::Loop() replaces the NODE_LOOP macro. This avoids hitting
v8::Isolate::GetCurrent() for each loop lookup when HAVE_ISOLATE==0
  • Loading branch information
ry committed Dec 22, 2011
1 parent e2c90af commit 2ac02f4
Show file tree
Hide file tree
Showing 15 changed files with 93 additions and 82 deletions.
10 changes: 5 additions & 5 deletions src/cares_wrap.cc
Expand Up @@ -22,7 +22,7 @@
#include <assert.h>
#include <node.h>
#include <req_wrap.h>
#include <node_isolate.h>
#include <node_vars.h>
#include <uv.h>

#include <string.h>
Expand Down Expand Up @@ -608,7 +608,7 @@ void AfterGetAddrInfo(uv_getaddrinfo_t* req, int status, struct addrinfo* res) {

if (status) {
// Error
SetErrno(uv_last_error(NODE_LOOP()));
SetErrno(uv_last_error(Loop()));
argv[0] = Local<Value>::New(Null());
} else {
// Success
Expand Down Expand Up @@ -711,7 +711,7 @@ static Handle<Value> GetAddrInfo(const Arguments& args) {
hints.ai_family = fam;
hints.ai_socktype = SOCK_STREAM;

int r = uv_getaddrinfo(NODE_LOOP(),
int r = uv_getaddrinfo(Loop(),
&req_wrap->req_,
AfterGetAddrInfo,
*hostname,
Expand All @@ -720,7 +720,7 @@ static Handle<Value> GetAddrInfo(const Arguments& args) {
req_wrap->Dispatched();

if (r) {
SetErrno(uv_last_error(NODE_LOOP()));
SetErrno(uv_last_error(Loop()));
delete req_wrap;
return scope.Close(v8::Null());
} else {
Expand All @@ -737,7 +737,7 @@ static void Initialize(Handle<Object> target) {
assert(r == ARES_SUCCESS);

struct ares_options options;
uv_ares_init_options(NODE_LOOP(), &ares_channel, &options, 0);
uv_ares_init_options(Loop(), &ares_channel, &options, 0);
assert(r == 0);

NODE_SET_METHOD(target, "queryA", Query<QueryAWrap>);
Expand Down
10 changes: 5 additions & 5 deletions src/fs_event_wrap.cc
Expand Up @@ -21,7 +21,7 @@

#include <node.h>
#include <handle_wrap.h>
#include <node_isolate.h>
#include <node_vars.h>

#include <stdlib.h>

Expand Down Expand Up @@ -110,15 +110,15 @@ Handle<Value> FSEventWrap::Start(const Arguments& args) {

String::Utf8Value path(args[0]->ToString());

int r = uv_fs_event_init(NODE_LOOP(), &wrap->handle_, *path, OnEvent, 0);
int r = uv_fs_event_init(Loop(), &wrap->handle_, *path, OnEvent, 0);
if (r == 0) {
// Check for persistent argument
if (!args[1]->IsTrue()) {
uv_unref(NODE_LOOP());
uv_unref(Loop());
}
wrap->initialized_ = true;
} else {
SetErrno(uv_last_error(NODE_LOOP()));
SetErrno(uv_last_error(Loop()));
}

return scope.Close(Integer::New(r));
Expand Down Expand Up @@ -146,7 +146,7 @@ void FSEventWrap::OnEvent(uv_fs_event_t* handle, const char* filename,
// assumption that a rename implicitly means an attribute change. Not too
// unreasonable, right? Still, we should revisit this before v1.0.
if (status) {
SetErrno(uv_last_error(NODE_LOOP()));
SetErrno(uv_last_error(Loop()));
eventStr = String::Empty();
}
else if (events & UV_RENAME) {
Expand Down
4 changes: 2 additions & 2 deletions src/handle_wrap.cc
Expand Up @@ -21,7 +21,7 @@

#include <node.h>
#include <handle_wrap.h>
#include <node_isolate.h>
#include <node_vars.h>

namespace node {

Expand Down Expand Up @@ -71,7 +71,7 @@ Handle<Value> HandleWrap::Unref(const Arguments& args) {
}

wrap->unref = true;
uv_unref(NODE_LOOP());
uv_unref(Loop());

return v8::Undefined();
}
Expand Down
19 changes: 14 additions & 5 deletions src/node.cc
Expand Up @@ -147,6 +147,15 @@ void StartThread(Isolate* isolate,
char** argv);


uv_loop_t* Loop() {
#if defined(HAVE_ISOLATES) && HAVE_ISOLATES
return Isolate::GetCurrent()->GetLoop();
#else
return uv_default_loop();
#endif
}


static void StartGCTimer () {
if (!uv_is_active((uv_handle_t*) &gc_timer)) {
uv_timer_start(&gc_timer, node::CheckStatus, 5000, 5000);
Expand All @@ -173,7 +182,7 @@ static void Idle(uv_idle_t* watcher, int status) {
static void Check(uv_check_t* watcher, int status) {
assert(watcher == &gc_check);

tick_times[tick_time_head] = uv_now(NODE_LOOP());
tick_times[tick_time_head] = uv_now(Loop());
tick_time_head = (tick_time_head + 1) % RPM_SAMPLES;

StartGCTimer();
Expand Down Expand Up @@ -203,7 +212,7 @@ static void Tick(void) {
need_tick_cb = false;
if (uv_is_active((uv_handle_t*) &tick_spinner)) {
uv_idle_stop(&tick_spinner);
uv_unref(NODE_LOOP());
uv_unref(Loop());
}

HandleScope scope;
Expand Down Expand Up @@ -245,7 +254,7 @@ static Handle<Value> NeedTickCallback(const Arguments& args) {
// tick_spinner to keep the event loop alive long enough to handle it.
if (!uv_is_active((uv_handle_t*) &tick_spinner)) {
uv_idle_start(&tick_spinner, Spin);
uv_ref(NODE_LOOP());
uv_ref(Loop());
}
return Undefined();
}
Expand Down Expand Up @@ -1497,7 +1506,7 @@ static void CheckStatus(uv_timer_t* watcher, int status) {
}
}

double d = uv_now(NODE_LOOP()) - TICK_TIME(3);
double d = uv_now(Loop()) - TICK_TIME(3);

//printfb("timer d = %f\n", d);

Expand Down Expand Up @@ -1526,7 +1535,7 @@ static Handle<Value> Uptime(const Arguments& args) {
v8::Handle<v8::Value> UVCounters(const v8::Arguments& args) {
HandleScope scope;

uv_counters_t* c = &NODE_LOOP()->counters;
uv_counters_t* c = &Loop()->counters;

Local<Object> obj = Object::New();

Expand Down
6 changes: 4 additions & 2 deletions src/node.h
Expand Up @@ -75,8 +75,6 @@
#define NODE_STRINGIFY_HELPER(n) #n
#endif

#define NODE_LOOP() (node::Isolate::GetCurrent()->GetLoop())

namespace node {

int Start(int argc, char *argv[]);
Expand All @@ -86,6 +84,10 @@ v8::Handle<v8::Object> SetupProcessObject(int argc, char *argv[]);
void Load(v8::Handle<v8::Object> process);
void EmitExit(v8::Handle<v8::Object> process);

// Returns the loop for the current isolate. If compiled with
// --without-isolates then this will always return uv_default_loop();
uv_loop_t* Loop();

#define NODE_PSYMBOL(s) \
v8::Persistent<v8::String>::New(v8::String::NewSymbol(s))

Expand Down
6 changes: 3 additions & 3 deletions src/node_crypto.cc
Expand Up @@ -24,7 +24,7 @@

#include <node.h>
#include <node_buffer.h>
#include <node_isolate.h>
#include <node_vars.h>
#include <node_root_certs.h>

#include <string.h>
Expand Down Expand Up @@ -4118,7 +4118,7 @@ PBKDF2(const Arguments& args) {

req = new uv_work_t();
req->data = request;
uv_queue_work(NODE_LOOP(), req, EIO_PBKDF2, EIO_PBKDF2After);
uv_queue_work(Loop(), req, EIO_PBKDF2, EIO_PBKDF2After);

return Undefined();

Expand Down Expand Up @@ -4241,7 +4241,7 @@ Handle<Value> RandomBytes(const Arguments& args) {
Local<Function> callback_v = Local<Function>(Function::Cast(*args[1]));
req->callback_ = Persistent<Function>::New(callback_v);

uv_queue_work(NODE_LOOP(),
uv_queue_work(Loop(),
&req->work_req_,
RandomBytesWork<generator>,
RandomBytesAfter<generator>);
Expand Down
8 changes: 4 additions & 4 deletions src/node_file.cc
Expand Up @@ -22,7 +22,7 @@
#include "node.h"
#include "node_file.h"
#include "node_buffer.h"
#include <node_isolate.h>
#include <node_vars.h>
#ifdef __POSIX__
# include "node_stat_watcher.h"
#endif
Expand Down Expand Up @@ -226,7 +226,7 @@ struct fs_req_wrap {

#define ASYNC_CALL(func, callback, ...) \
FSReqWrap* req_wrap = new FSReqWrap(); \
int r = uv_fs_##func(NODE_LOOP(), &req_wrap->req_, \
int r = uv_fs_##func(Loop(), &req_wrap->req_, \
__VA_ARGS__, After); \
assert(r == 0); \
req_wrap->object_->Set(oncomplete_sym, callback); \
Expand All @@ -235,9 +235,9 @@ struct fs_req_wrap {

#define SYNC_CALL(func, path, ...) \
fs_req_wrap req_wrap; \
int result = uv_fs_##func(NODE_LOOP(), &req_wrap.req, __VA_ARGS__, NULL); \
int result = uv_fs_##func(Loop(), &req_wrap.req, __VA_ARGS__, NULL); \
if (result < 0) { \
int code = uv_last_error(NODE_LOOP()).code; \
int code = uv_last_error(Loop()).code; \
return ThrowException(UVException(code, #func, "", path)); \
}

Expand Down
4 changes: 2 additions & 2 deletions src/node_zlib.cc
Expand Up @@ -29,7 +29,7 @@

#include <node.h>
#include <node_buffer.h>
#include <node_isolate.h>
#include <node_vars.h>
#include <req_wrap.h>

#include <node_vars.h>
Expand Down Expand Up @@ -134,7 +134,7 @@ template <node_zlib_mode mode> class ZCtx : public ObjectWrap {
uv_work_t* work_req = new uv_work_t();
work_req->data = req_wrap;

uv_queue_work(NODE_LOOP(),
uv_queue_work(Loop(),
work_req,
ZCtx<mode>::Process,
ZCtx<mode>::After);
Expand Down
10 changes: 5 additions & 5 deletions src/pipe_wrap.cc
Expand Up @@ -21,7 +21,7 @@

#include <node.h>
#include <node_buffer.h>
#include <node_isolate.h>
#include <node_vars.h>
#include <req_wrap.h>
#include <handle_wrap.h>
#include <stream_wrap.h>
Expand Down Expand Up @@ -124,7 +124,7 @@ Handle<Value> PipeWrap::New(const Arguments& args) {

PipeWrap::PipeWrap(Handle<Object> object, bool ipc)
: StreamWrap(object, (uv_stream_t*) &handle_) {
int r = uv_pipe_init(NODE_LOOP(), &handle_, ipc);
int r = uv_pipe_init(Loop(), &handle_, ipc);
assert(r == 0); // How do we proxy this error up to javascript?
// Suggestion: uv_pipe_init() returns void.
handle_.data = reinterpret_cast<void*>(this);
Expand All @@ -142,7 +142,7 @@ Handle<Value> PipeWrap::Bind(const Arguments& args) {
int r = uv_pipe_bind(&wrap->handle_, *name);

// Error starting the pipe.
if (r) SetErrno(uv_last_error(NODE_LOOP()));
if (r) SetErrno(uv_last_error(Loop()));

return scope.Close(Integer::New(r));
}
Expand Down Expand Up @@ -173,7 +173,7 @@ Handle<Value> PipeWrap::Listen(const Arguments& args) {
int r = uv_listen((uv_stream_t*)&wrap->handle_, backlog, OnConnection);

// Error starting the pipe.
if (r) SetErrno(uv_last_error(NODE_LOOP()));
if (r) SetErrno(uv_last_error(Loop()));

return scope.Close(Integer::New(r));
}
Expand Down Expand Up @@ -226,7 +226,7 @@ void PipeWrap::AfterConnect(uv_connect_t* req, int status) {
assert(wrap->object_.IsEmpty() == false);

if (status) {
SetErrno(uv_last_error(NODE_LOOP()));
SetErrno(uv_last_error(Loop()));
}

Local<Value> argv[3] = {
Expand Down
8 changes: 4 additions & 4 deletions src/process_wrap.cc
Expand Up @@ -21,7 +21,7 @@

#include <node.h>
#include <handle_wrap.h>
#include <node_isolate.h>
#include <node_vars.h>
#include <pipe_wrap.h>
#include <string.h>
#include <stdlib.h>
Expand Down Expand Up @@ -176,7 +176,7 @@ class ProcessWrap : public HandleWrap {
Get(String::NewSymbol("windowsVerbatimArguments"))->IsTrue();
#endif

int r = uv_spawn(NODE_LOOP(), &wrap->process_, options);
int r = uv_spawn(Loop(), &wrap->process_, options);

wrap->SetHandle((uv_handle_t*)&wrap->process_);
assert(wrap->process_.data == wrap);
Expand All @@ -196,7 +196,7 @@ class ProcessWrap : public HandleWrap {
delete [] options.env;
}

if (r) SetErrno(uv_last_error(NODE_LOOP()));
if (r) SetErrno(uv_last_error(Loop()));

return scope.Close(Integer::New(r));
}
Expand All @@ -210,7 +210,7 @@ class ProcessWrap : public HandleWrap {

int r = uv_process_kill(&wrap->process_, signal);

if (r) SetErrno(uv_last_error(NODE_LOOP()));
if (r) SetErrno(uv_last_error(Loop()));

return scope.Close(Integer::New(r));
}
Expand Down

0 comments on commit 2ac02f4

Please sign in to comment.