Skip to content

Commit cec1423

Browse files
committed
Checking in code
1 parent da3b97c commit cec1423

File tree

5 files changed

+40
-49
lines changed

5 files changed

+40
-49
lines changed

src/javascript/lib/core/processes/process.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
import Patterns from 'tailored';
12
import States from './states';
2-
import Core from '../../core';
33

44
class Process {
55
constructor(pid, func, args, mailbox, system) {
@@ -18,7 +18,7 @@ class Process {
1818
let retval = States.NORMAL;
1919

2020
try {
21-
await this.system.pause(this.pid);
21+
await this.system.set_current(this.pid);
2222
await this.func.apply(null, this.args);
2323
} catch (e) {
2424
console.error(e);
@@ -52,16 +52,16 @@ class Process {
5252

5353
for (let i = 0; i < messages.length; i++) {
5454
for (const clause of clauses) {
55-
const value = await Core.Patterns.match_or_default_async(
56-
clause[0].pattern,
55+
const value = await Patterns.match_or_default_async(
56+
clause.pattern,
5757
messages[i],
58-
clause[0].guard,
58+
clause.guard,
5959
States.NOMATCH,
6060
);
6161

6262
if (value !== States.NOMATCH) {
6363
this.mailbox.removeAt(i);
64-
return clause[1].apply(null, value);
64+
return clause.fn.apply(null, value);
6565
}
6666
}
6767
}

src/javascript/lib/core/processes/process_system.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,10 @@ class ProcessSystem {
188188

189189
if (pid) {
190190
this.mailboxes.get(pid).deliver(msg);
191-
191+
console.log('sending');
192+
console.log(pid);
192193
if (this.suspended.has(pid)) {
194+
console.log('Got suspended');
193195
const [args, resolver] = this.suspended.get(pid);
194196

195197
const result = await this.__receive(pid, args);
@@ -204,20 +206,23 @@ class ProcessSystem {
204206
}
205207

206208
async __receive(pid, args) {
209+
this.set_current(pid);
207210
const process = this.pids.get(pid);
208211
return process.receive(args);
209212
}
210213

211214
async receive(args, timeout = 0, timeoutFn = () => true) {
212215
let DateTimeout = null;
213216
const pid = this.current_process.pid;
214-
217+
console.log('Received');
218+
console.log(pid);
215219
if (timeout === 0 || timeout === Infinity) {
216220
const result = await this.__receive(pid, args);
221+
console.log(result);
217222
if (result !== States.NOMATCH) {
218223
return result;
219224
}
220-
return this.suspend(args);
225+
return this.suspend(pid, args);
221226
}
222227

223228
DateTimeout = Date.now() + timeout;
@@ -244,11 +249,13 @@ class ProcessSystem {
244249
});
245250
}
246251

247-
suspend(args) {
252+
suspend(pid, args) {
248253
this.current_process.status = States.SUSPENDED;
254+
console.log(pid);
249255

250256
return new Promise((resolver) => {
251-
this.suspended.set(this.current_process.pid, [args, resolver]);
257+
this.suspended.set(pid, [args, resolver]);
258+
console.log('suspended');
252259
});
253260
}
254261

src/javascript/lib/core/processes/scheduler.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import Task from './task';
2-
31
class ProcessQueue {
42
constructor(pid) {
53
this.pid = pid;
@@ -59,12 +57,13 @@ class Scheduler {
5957
} else {
6058
for (const [pid, queue] of this.queues) {
6159
let reductions = 0;
62-
63-
this.system.set_current(pid);
6460
while (queue && !queue.empty() && reductions < this.reductions_per_process) {
6561
const resolver = queue.next();
6662
this.isRunning = true;
63+
6764
resolver(true);
65+
this.system.set_current(pid);
66+
console.log(`Scheduler ${pid}`);
6867
this.isRunning = false;
6968
reductions++;
7069
}
@@ -79,6 +78,10 @@ class Scheduler {
7978
pause(pid, resolver) {
8079
this.addToQueue(pid, resolver);
8180
}
81+
82+
schedule(pid, fun, args, resolver) {
83+
this.addToQueue(pid, [fun, args, resolver]);
84+
}
8285
}
8386

8487
export default Scheduler;

src/javascript/lib/core/processes/task.js

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/javascript/tests/core/processes.spec.js

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import test from 'ava';
2+
import Patterns from 'tailored';
23
import ProcessSystem from '../../lib/core/processes/process_system';
34

45
let system = null;
@@ -7,45 +8,35 @@ test.beforeEach(() => {
78
system = new ProcessSystem();
89
});
910

10-
test('spawn process', async (t) => {
11-
const pid = system.spawn(async () => {
12-
await system.sleep(Symbol.for('Infinity'));
13-
});
14-
15-
t.is(system.list().length, 2);
16-
t.is(system.list()[1], pid);
17-
});
18-
19-
test('spawn linked process', async (t) => {
20-
const pid = system.spawn_link(async () => {
21-
await system.sleep(Symbol.for('Infinity'));
22-
});
23-
24-
t.is(system.list().length, 2);
25-
t.true(system.links.get(pid).has(system.list()[0]));
26-
t.true(system.links.get(system.list()[0]).has(pid));
27-
});
28-
2911
test('spawn linked process', async (t) => {
12+
// spawn one process
3013
const pid1 = system.spawn_link(async () => {
14+
const arg = Patterns.clause([Patterns.variable()], async x => console.log(x));
15+
3116
await system.pause();
32-
console.log('1');
17+
console.log(`first process ${system.pid()}`);
18+
await system.receive([arg]);
19+
console.log(`first process ${system.pid()}`);
3320
await Math.log2(2);
3421
await system.pause();
35-
console.log('2');
22+
console.log(`first process ${system.pid()}`);
3623
await Math.log2(4);
3724
});
3825

26+
// spawn another process
3927
const pid2 = system.spawn_link(async () => {
4028
await system.pause();
41-
console.log('3');
29+
console.log(`second process ${system.pid()}`);
4230
await Math.log2(4);
4331
await system.pause();
44-
console.log('4');
32+
console.log(`second process ${system.pid()}`);
33+
await system.send(pid1, 'This message was sent');
34+
console.log(`second process ${system.pid()}`);
4535
await Math.log2(4);
4636
});
4737

48-
await system.sleep(Symbol.for('Infinity'));
38+
console.log(`first process pid should be ${pid1}`);
39+
console.log(`first process pid should be ${pid2}`);
4940

5041
t.is(system.list().length, 3);
5142
t.is(system.list()[1], pid1);

0 commit comments

Comments
 (0)