Skip to content

Instantly share code, notes, and snippets.

@isaacs
Created January 30, 2013 18:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isaacs/4675445 to your computer and use it in GitHub Desktop.
Save isaacs/4675445 to your computer and use it in GitHub Desktop.
diff --git a/lib/events.js b/lib/events.js
index 223015e..ccee37d 100644
--- a/lib/events.js
+++ b/lib/events.js
@@ -30,6 +30,8 @@ function EventEmitter() {
if (domain.active && !(this instanceof domain.Domain)) {
this.domain = domain.active;
}
+ } else {
+ this.domain = null;
}
this._events = this._events || null;
this._maxListeners = this._maxListeners || defaultMaxListeners;
diff --git a/lib/timers.js b/lib/timers.js
index 28c23c1..ac9ca85 100644
--- a/lib/timers.js
+++ b/lib/timers.js
@@ -207,6 +207,7 @@ exports.setTimeout = function(callback, after) {
}
if (process.domain) timer.domain = process.domain;
+ else timer.domain = null;
exports.active(timer);
@@ -230,6 +231,7 @@ exports.setInterval = function(callback, repeat) {
var timer = new Timer();
if (process.domain) timer.domain = process.domain;
+ else timer.domain = null;
repeat *= 1; // coalesce to number or NaN
@@ -342,6 +344,7 @@ exports.setImmediate = function(callback) {
}
if (process.domain) immediate.domain = process.domain;
+ else immediate.domain = null;
L.append(immediateQueue, immediate);
diff --git a/src/node.js b/src/node.js
index 9fc68e4..942c02d 100644
--- a/src/node.js
+++ b/src/node.js
@@ -214,6 +214,24 @@
return startup._lazyConstants;
};
+
+ // functions to avoid having to do checks that deopt our
+ // hot path functions.
+ function disposedDomain(domain) {
+ return domain && domain._disposed;
+ }
+
+ function enterDomain(domain) {
+ if (domain)
+ domain.enter();
+ }
+
+ function exitDomain(domain) {
+ if (domain)
+ domain.exit();
+ }
+
+
startup.processFatal = function() {
// call into the active domain, or emit uncaughtException,
// and exit if there are no listeners.
@@ -299,11 +317,10 @@
// Along with EventEmitter.emit, this is the hottest code in node.
// Everything that comes from C++ into JS passes through here.
process._makeCallback = function(obj, fn, args) {
- var domain = obj.domain;
- if (domain) {
- if (domain._disposed) return;
- domain.enter();
- }
+ if (disposedDomain(obj.domain))
+ return;
+
+ enterDomain(obj.domain);
// I know what you're thinking, why not just use fn.apply
// Because we hit this function a lot, and really want to make sure
@@ -338,7 +355,7 @@
break;
}
- if (domain) domain.exit();
+ exitDomain(obj.domain);
// process the nextTicks after each time we get called.
process._tickCallback();
@@ -417,10 +434,10 @@
while (nextTickIndex < nextTickLength) {
var tock = nextTickQueue[nextTickIndex++];
var callback = tock.callback;
- if (tock.domain) {
- if (tock.domain._disposed) continue;
- tock.domain.enter();
- }
+
+ if (disposedDomain(tock.domain)) continue;
+ enterDomain(tock.domain);
+
var threw = true;
try {
callback();
@@ -430,9 +447,8 @@
// so we can't clear the tickDepth at this point.
if (threw) tickDone(tickDepth);
}
- if (tock.domain) {
- tock.domain.exit();
- }
+
+ exitDomain(tock.domain);
}
nextTickQueue.splice(0, nextTickIndex);
nextTickIndex = 0;
@@ -452,12 +468,14 @@
if (tickDepth >= process.maxTickDepth)
maxTickWarn();
- var tock = { callback: callback };
- if (process.domain) tock.domain = process.domain;
- nextTickQueue.push(tock);
- if (nextTickQueue.length) {
+ var tock = {
+ callback: callback,
+ domain: process.domain || null
+ };
+ if (nextTickQueue.length === 0) {
process._needTickCallback();
}
+ nextTickQueue.push(tock);
};
};
diff --git a/src/req_wrap.h b/src/req_wrap.h
index ba56821..d2eff82 100644
--- a/src/req_wrap.h
+++ b/src/req_wrap.h
@@ -47,6 +47,8 @@ class ReqWrap {
if (!domain->IsUndefined()) {
// fprintf(stderr, "setting domain on ReqWrap\n");
object_->Set(domain_symbol, domain);
+ } else {
+ object_->Set(domain_symbol, v8::Null());
}
ngx_queue_insert_tail(&req_wrap_queue, &req_wrap_queue_);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment