-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathphilosophers.abs
76 lines (55 loc) · 1.43 KB
/
philosophers.abs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Written by Daniel Rødskog 2014
// http://www.mn.uio.no/ifi/english/research/groups/pma/completedmasters/2014/rodskog/
module Philosophers;
interface Philosopher { }
interface Fork {
Unit grab(Philosopher owner);
Unit release(Philosopher owner);
}
class Philosopher(Fork f1, Fork f2) implements Philosopher {
Unit run() {
while (True) {
this.tell("is thinking");
Fut<Unit> f;
f = f1!grab(this);
f.get;
// await f1!grab(this);
this.tell("grabbed left fork");
// await f2!grab(this);
f = f2!grab(this);
f.get;
this.tell("grabbed right fork");
this.tell("is eating");
f1!release(this);
f2!release(this);
this.tell("released both forks");
}
}
Unit tell(String s) {
Unit p = println(toString(this)+" "+s);
}
}
class Fork implements Fork {
Philosopher owner;
Unit grab(Philosopher p) {
await owner == null;
owner = p;
}
Unit release(Philosopher p) {
if (p == owner)
owner = null;
}
}
// MAIN //
{
Fork f1 = new Fork();
Fork f2 = new Fork();
Fork f3 = new Fork();
Fork f4 = new Fork();
Fork f5 = new Fork();
new Philosopher(f1, f2);
new Philosopher(f2, f3);
new Philosopher(f3, f4);
new Philosopher(f4, f5);
new Philosopher(f5, f1);
}