Skip to content
This repository has been archived by the owner on Sep 23, 2021. It is now read-only.

Commit

Permalink
Make Process.Status an enum
Browse files Browse the repository at this point in the history
  • Loading branch information
ComFreek committed Feb 12, 2015
1 parent 4533943 commit e923503
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 28 deletions.
11 changes: 6 additions & 5 deletions core/InputPort.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

var IP = require('./IP')
, Fiber = require('fibers')
, ProcessStatus = require('./Process').Status
, Utils = require('./utils');

var InputPort = module.exports = function(queue){
Expand Down Expand Up @@ -48,19 +49,19 @@ InputPort.prototype.receive = function(){
console.log(proc.name + ' recv EOS from ' + this.name );
return null;
}
proc.status = 'R';
proc.status = ProcessStatus.WAITING_TO_RECEIVE;
proc.yielded = true;
Fiber.yield();
proc.status = 'A';
proc.status = ProcessStatus.ACTIVE;
proc.yielded = false;
}
else
break;
}
//if (conn.usedslots == conn.array.length)
for (var i = 0; i < conn.up.length; i ++) {
if (conn.up[i].status == 'S'){
conn.up[i].status = 'K';
if (conn.up[i].status == ProcessStatus.WAITING_TO_SEND) {
conn.up[i].status = ProcessStatus.READY_TO_EXECUTE;
this.queue.push(conn.up[i]);
}
}
Expand All @@ -84,7 +85,7 @@ InputPort.prototype.close = function(){
conn.closed = true;
console.log(proc.name + ': ' + conn.usedslots + ' IPs dropped because of close on ' + conn.name);
for (var i = 0; i < conn.up.length; i ++) {
if (conn.up[i].status == 'S')
if (conn.up[i].status == ProcessStatus.WAITING_TO_SEND)
this.queue.push(conn.up[i]);
}
};
13 changes: 8 additions & 5 deletions core/OutputPort.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';
var Fiber = require('fibers');
var Fiber = require('fibers')
, ProcessStatus = require('./Process').Status;

var OutputPort = module.exports = function(queue){
this.name = null;
Expand Down Expand Up @@ -36,15 +37,17 @@ OutputPort.prototype.send = function(ip){
return -1;
}
while (true) {
if (conn.down.status == 'R' || conn.down.status == 'N' || conn.down.status == 'D') {
conn.down.status = 'K';
if (conn.down.status == ProcessStatus.WAITING_TO_RECEIVE ||
conn.down.status == ProcessStatus.NOT_INITIALIZED ||
conn.down.status == ProcessStatus.DORMANT) {
conn.down.status = ProcessStatus.READY_TO_EXECUTE;
this.queue.push(conn.down);
}
if (conn.usedslots == conn.array.length) {
proc.status = 'S';
proc.status = ProcessStatus.WAITING_TO_SEND;
proc.yielded = true;
Fiber.yield();
proc.status = 'A';
proc.status = ProcessStatus.ACTIVE;
proc.yielded = false;
}
else {
Expand Down
20 changes: 11 additions & 9 deletions core/Process.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,19 @@ var Process = module.exports = function(name, func, list) {
this.inports = [];
this.outports = [];
list[list.length] = this;
//this.closed = false;
this.status =
'N'; // not initiated
// 'A' active (includes waiting on callback ...)
// 'R' waiting to receive
// 'S' waiting to send
// 'K' ready to execute
// 'D' dormant
// 'C' closed
this.status = Process.Status.NOT_INITIALIZED;
this.ownedIPs = 0;
this.cbpending = false;
this.yielded = false;
this.data = null;
};

Process.Status = {
NOT_INITIALIZED: 1,
ACTIVE: 2, // (includes waiting on callback ...)
WAITING_TO_RECEIVE: 3,
WAITIN_TO_SEND: 4,
READY_TO_EXECUTE: 5,
DORMANT: 6,
CLOSED: 8
};
19 changes: 10 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,14 @@ function close(proc) {
if (tracing) {
console.log(proc.name + ' closing');
}
proc.status = 'C';
proc.status = Process.Status.CLOSED;
//console.log('cl' + count);
count--;
for (var i = 0; i < proc.outports.length; i++) {
var conn = proc.outports[i][1].conn;
if (conn.down.status == 'R' || conn.down.status == 'N' /*|| conn.down.status == 'D' */ ) {
conn.down.status = 'K';
if (conn.down.status == Process.Status.WAITING_TO_RECEIVE ||
conn.down.status == Process.Status.NOT_INITIALIZED) {
conn.down.status = Process.Status.READY_TO_EXECUTE;
queue.push(conn.down);
}
conn.upstreamProcsUnclosed--;
Expand All @@ -53,7 +54,7 @@ function close(proc) {
continue;
}
for (var j = 0; j < conn.up.length; j++) {
if (conn.up[j].status == 'S') {
if (conn.up[j].status == Process.Status.CLOSED) {
queue.push(conn.up[j]);
}
}
Expand Down Expand Up @@ -179,11 +180,11 @@ function run2(trace) {
if (x.fiber == null) {
x.fiber = new Fiber(x.func);
x.fiber.fbpProc = x;
x.status = 'A';
x.status = Process.Status.ACTIVE;
}

if (x.status != 'C') {
if (x.status == 'D' && upconnsclosed(x)) {
if (x.status != Process.Status.CLOSED) {
if (x.status == Process.Status.DORMANT && upconnsclosed(x)) {
close(x);
}
else {
Expand Down Expand Up @@ -222,7 +223,7 @@ function run2(trace) {
}
if (!x.yielded && !x.cbpending) {
if (!upconnsclosed(x)) {
x.status = 'D';
x.status = Process.Status.DORMANT;
queue.push(x);
for (var j = 0; j < x.inports.length; j++) {
var k = x.inports[j];
Expand All @@ -245,7 +246,7 @@ function run2(trace) {
}
var deadlock = true;
for (var i = 0; i < list.length; i++) {
if (list[i].cbpending || list[i].status == 'A') {
if (list[i].cbpending || list[i].status == Process.Status.ACTIVE) {
deadlock = false;
break;
}
Expand Down

0 comments on commit e923503

Please sign in to comment.