Skip to content

Commit 20db88e

Browse files
committed
Add PID class
1 parent 7077ce3 commit 20db88e

File tree

3 files changed

+30
-12
lines changed

3 files changed

+30
-12
lines changed

src/processes/pid.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"use strict";
2+
/* @flow */
3+
4+
let process_counter = -1;
5+
6+
class PID {
7+
constructor(){
8+
process_counter = process_counter + 1;
9+
this.id = process_counter;
10+
}
11+
12+
toString(){
13+
return "PID#<0." + id + ".0>";
14+
}
15+
}
16+
17+
18+
export default PID;

src/processes/process_system.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ import Mailbox from "./mailbox";
55
import Process from "./process";
66
import States from "./states";
77
import Scheduler from "./scheduler";
8+
import PID from "./pid";
89

910
class ProcessSystem {
1011

1112
constructor(){
12-
this.process_counter = -1;
1313
this.pids = new Map();
1414
this.mailboxes = new Map();
1515
this.names = new Map();
@@ -83,8 +83,7 @@ class ProcessSystem {
8383
}
8484

8585
add_proc(fun, args, linked){
86-
this.process_counter = this.process_counter + 1;
87-
let newpid = this.process_counter;
86+
let newpid = new PID();
8887
let mailbox = new Mailbox();
8988
let newproc = new Process(newpid, fun, args, mailbox, this);
9089

@@ -141,7 +140,7 @@ class ProcessSystem {
141140
}
142141

143142
pidof(id){
144-
if (typeof(id) === "number") {
143+
if (id instanceof PID) {
145144
return this.pids.has(id) ? id : null;
146145
} else if (id instanceof Process) {
147146
return id.pid;

src/processes/scheduler.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"use strict";
22

3+
// A reduction is equal to a function call
34
const MAX_REDUCTIONS_PER_PROCESS = 8;
45

56
class ProcessQueue {
@@ -25,22 +26,22 @@ class Scheduler {
2526
constructor(throttle = 0){
2627
this.isRunning = false;
2728
this.invokeLater = function (callback) { setTimeout(callback, throttle); }
28-
this.queues = {};
29+
this.queues = new Map();
2930
this.run();
3031
}
3132

3233
addToQueue(pid, task){
33-
if(!this.queues[pid]){
34-
this.queues[pid] = new ProcessQueue(pid);
34+
if(!this.queues.has(pid)){
35+
this.queues.set(pid, new ProcessQueue(pid));
3536
}
3637

37-
this.queues[pid].add(task);
38+
this.queues.get(pid).add(task);
3839
}
3940

4041
removePid(pid){
4142
this.isRunning = true;
4243

43-
delete this.queues[pid];
44+
this.queues.delete(pid);
4445

4546
this.isRunning = false;
4647
}
@@ -49,10 +50,10 @@ class Scheduler {
4950
if (this.isRunning) {
5051
this.invokeLater(() => { this.run(); });
5152
} else {
52-
for(let pid of Object.keys(this.queues)){
53+
for(let [pid, queue] of this.queues){
5354
let reductions = 0;
54-
while(this.queues[pid] && !this.queues[pid].empty() && reductions < MAX_REDUCTIONS_PER_PROCESS){
55-
let task = this.queues[pid].next();
55+
while(queue && !queue.empty() && reductions < MAX_REDUCTIONS_PER_PROCESS){
56+
let task = queue.next();
5657
this.isRunning = true;
5758

5859
let result;

0 commit comments

Comments
 (0)