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

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
ry committed Sep 18, 2011
1 parent 66d2fd2 commit a22cb64
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 23 deletions.
37 changes: 26 additions & 11 deletions lib/domain.js
Expand Up @@ -24,6 +24,7 @@ var EventEmitter = require('events').EventEmitter;
var util = require('util');

var domainIds = 0;
var allowBadHandleAccess = false;


var debug;
Expand Down Expand Up @@ -68,17 +69,28 @@ Domain.prototype.enter = function() {


Domain.prototype.kill = function() {
for (var i = 0; i < this.handles.length; i++) {
debug("kill handle", this.handles[i]);
if (this.handles[i].close) {
this.handles[i].close();
var handle;
console.error("this.handles.length=" + this.handles.length);
while (handle = this.handles.shift()) {
console.error("shift");
debug("kill handle", handle);
debugger;

if (handle._onTimeout || handle.ontimeout) {
// We may be calling domain.remove(handle) on a domain which is not our
// own. This is because the timers sometimes share a handle
// in JavaScript land.
allowBadHandleAccess = true;
clearTimeout(handle);
allowBadHandleAccess = false;

handle._onTimeout = null
handle.ontimeout = null
} else {
this.handles[i].oncomplete = null;
this.handles[i].ontimeout = null;
this.handles[i]._onTimeout = null;
handle.close();
handle.oncomplete = null;
}
}
this.handles = [];
};


Expand Down Expand Up @@ -142,21 +154,24 @@ exports.addDefaultDomain = function(handle) {
exports.remove = function(handle) {
if (process.features.domains) {
// TODO do this in O(1)
debug(currentDomain, "remove", handle);

if (handle.domain) {
if (currentDomain != handle.domain) {
if (!allowBadHandleAccess && currentDomain != handle.domain) {
console.error("(node) Attempting to access a handle outside of the " +
"current domain. Thiis is not allowed. handle =", handle);
throw new Error("bad handle access");
// We should not reach here.
process.exit(-10);
}

var i = handle.domain.handles.indexOf(handle);
handle.domain.handles.splice(i, 1);
handle.domain = null;
} else {
assert.equal(currentDomain, defaultDomain);
}

var i = currentDomain.handles.indexOf(handle);
currentDomain.handles.splice(i, 1);
}
};

Expand Down
15 changes: 10 additions & 5 deletions lib/timers_uv.js
Expand Up @@ -188,6 +188,14 @@ exports.active = function(item) {
*/


function TimerElement(after) {
this._idleTimeout = after;
this._idlePrev = this;
this._idleNext = this;
require('domain').add(this);
}


exports.setTimeout = function(callback, after) {
var timer, c, args;

Expand All @@ -206,11 +214,7 @@ exports.setTimeout = function(callback, after) {

timer.start(0, 0);
} else {
timer = { _idleTimeout: after };
timer._idlePrev = timer;
timer._idleNext = timer;

require('domain').add(timer);
timer = new TimerElement(after);

if (arguments.length <= 2) {
timer._onTimeout = callback;
Expand Down Expand Up @@ -243,6 +247,7 @@ exports.clearTimeout = function(timer) {
if (timer instanceof Timer) {
closeTimer(timer); // for after === 0
} else {
debugger;
exports.unenroll(timer);
}
}
Expand Down
1 change: 0 additions & 1 deletion src/node.js
Expand Up @@ -558,7 +558,6 @@
domain.kill();
}

debugger;
domain.exit();

NativeModule.require('domain').pollNewDomains();
Expand Down
10 changes: 4 additions & 6 deletions test/simple/test-domains2.js
Expand Up @@ -40,7 +40,7 @@ setTimeout(function() {
console.error("timer 3");
timerCallbacks++;
}, 1);
}, 3);
}, 1);

setTimeout(function() {
timerCallbacks++;
Expand All @@ -58,21 +58,19 @@ setTimeout(function() {

var t1 = setTimeout(function() {
assert.ok(false, "should not get here");
}, 4);
}, 1);
assert.ok(t1.domain);
assert.equal(t1.domain, d2);
assert.equal(t1.domain.id, require('domain').getCurrent().id);

var t2 = setTimeout(function() {
assert.ok(false, "should not get here");
}, 3);
}, 1);
assert.equal(t2.domain, d2);

debugger;

this.kill();
});
}, 2);
}, 1);


process.on('exit', function() {
Expand Down

0 comments on commit a22cb64

Please sign in to comment.