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

Commit

Permalink
isolates have globals stored in struct globals
Browse files Browse the repository at this point in the history
  • Loading branch information
ry committed Dec 9, 2011
1 parent 2ba80a0 commit 1311017
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 5 deletions.
7 changes: 7 additions & 0 deletions src/node_isolate.cc
Expand Up @@ -46,6 +46,13 @@ Isolate::Isolate(uv_loop_t* loop) {

assert(isolate_->GetData() == NULL);
isolate_->SetData(this);

globals_init(&globals_);
}


struct globals* Isolate::Globals() {
return &globals_;
}


Expand Down
6 changes: 6 additions & 0 deletions src/node_isolate.h
Expand Up @@ -25,6 +25,7 @@
#include "queue.h"
#include "v8.h"
#include "uv.h"
#include "node_vars.h"

#ifdef NDEBUG
# define NODE_ISOLATE_CHECK(ptr) ((void) (ptr))
Expand Down Expand Up @@ -69,6 +70,8 @@ class Isolate {
/* Shutdown the isolate. Call this method at thread death. */
void Dispose();

struct globals* Globals();

private:
Isolate(uv_loop_t* loop);

Expand All @@ -81,6 +84,9 @@ class Isolate {
SLIST_HEAD(AtExitCallbacks, AtExitCallbackInfo) at_exit_callbacks_;
v8::Isolate* isolate_;
uv_loop_t* loop_;

// Global variables for this isolate.
struct globals globals_;
};

} // namespace node
Expand Down
17 changes: 12 additions & 5 deletions src/node_vars.cc
@@ -1,4 +1,5 @@
#include <node_vars.h>
#include <node_isolate.h>
#if HAVE_OPENSSL
# include <node_crypto.h>
#endif
Expand All @@ -9,11 +10,7 @@ namespace node {
// For now we just statically initialize the globals structure. Later there
// will be one struct globals for each isolate.

static struct globals g_struct;
static struct globals* g_ptr;


static void globals_init(struct globals* g) {
void globals_init(struct globals* g) {
memset(g, 0, sizeof(struct globals));
g->debug_port = 5858;

Expand All @@ -31,12 +28,22 @@ static void globals_init(struct globals* g) {
}


#if HAVE_ISOLATES
struct globals* globals_get() {
node::Isolate* isolate = node::Isolate::GetCurrent();
return isolate->Globals();
}
#else
static struct globals g_struct;
static struct globals* g_ptr;

struct globals* globals_get() {
if (!g_ptr) {
g_ptr = &g_struct;
globals_init(g_ptr);
}
return g_ptr;
}
#endif // HAVE_ISOLATES

} // namespace node
5 changes: 5 additions & 0 deletions src/node_vars.h
Expand Up @@ -182,6 +182,11 @@ struct globals {
::ares_channel ares_channel;
};

// Initialize globals struct.
void globals_init(struct globals*);

// Get the globals struct for the current Isolate. The returned pointer is
// already initialized.
struct globals* globals_get();

} // namespace node
Expand Down

0 comments on commit 1311017

Please sign in to comment.