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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
object scheduler {
import scala.concurrent.pilib._;
/**
* Random number generator.
*/
val random = new java.util.Random();
//***************** Scheduler ******************//
/**
* A cell of the scheduler whose attached agent is allowed to start.
*/
def A(a: Chan[unit], b: Chan[unit])(d: Chan[unit], c: Chan[unit]): unit = {
a.read;
C(a,b)(d,c)
}
/**
* A cell of the scheduler in an intermediate state (incorrect)
*/
// def B(a: Chan[unit], b: Chan[unit])(d: Chan[unit], c: Chan[unit]): unit = {
// b.read;
// D(a,b)(d,c)
// }
/**
* A cell of the scheduler in an intermediate state (correct).
*/
def B(a: Chan[unit], b: Chan[unit])(d: Chan[unit], c: Chan[unit]): unit =
choice (
b * (x => D(a,b)(d,c)),
d(()) * ({ b.read; A(a,b)(d,c)})
);
/**
* A cell of the scheduler in another intermediate state.
*/
def C(a: Chan[unit], b: Chan[unit])(d: Chan[unit], c: Chan[unit]): unit = {
c.read;
B(a,b)(d,c)
}
/**
* A cell of the scheduler whose attached agent is not yet allowed to start.
*/
def D(a: Chan[unit], b: Chan[unit])(d: Chan[unit], c: Chan[unit]): unit = {
d.write(());
A(a,b)(d,c)
}
//***************** Agents ******************//
def agent(i: Int)(a: Chan[unit], b: Chan[unit]): unit = {
Thread.sleep(random.nextInt(1000) + 1);
a.write(());
System.out.println("Starting agent " + i);
Thread.sleep(random.nextInt(1000) + 1);
// 10% chance that we sleep for a long while.
if(random.nextInt(10) == 0) {
System.out.println("Agent " + i + " sleeps");
Thread.sleep(20000);
}
b.write(());
System.out.println("Ending agent " + i);
agent(i)(a, b)
}
//***************** Entry function ******************//
/**
* Creates a scheduler for five agents (programs).
*/
def main(args: Array[String]): unit = {
val agentNb = 5;
val agents = for(val i <- List.range(0, agentNb)) yield agent(i);
scheduleAgents(agents);
}
//***************** Infrastructure *****************//
/**
* A cell is modelled as a function that takes as parameters
* input and output channels and which returns nothing.
*/
type Cell = (Chan[unit], Chan[unit]) => unit;
/**
* Creates a cell composed of two cells linked together.
*/
def join(cell1: Cell, cell2: Cell): Cell =
(l: Chan[unit], r: Chan[unit]) => {
val link = new Chan[unit];
spawn < cell1(l, link) | cell2(link, r) >
};
/**
* Links the output of a cell to its input.
*/
def close(cell: Cell): unit = {
val a = new Chan[unit];
cell(a, a)
}
/**
* Creates a cell consisting of a chain of cells.
*/
def chain(cells: List[Cell]): Cell =
cells reduceLeft join;
/**
* Creates a cell consisting of a chain of cells.
*/
def makeRing(cells: List[Cell]): unit =
close(chain(cells));
/**
* An agent is modelled as a function that takes as parameters channels to
* signal that it has started or finished.
*/
type Agent = (Chan[unit], Chan[unit]) => unit;
/**
* Takes a list of agents and schedules them.
*/
def scheduleAgents(agents: List[Agent]): unit = {
var firstAgent = true;
val cells = agents map (ag => {
val a = new Chan[unit];
val b = new Chan[unit];
spawn < ag(a, b) >;
if (firstAgent) {
firstAgent = false;
A(a, b)
}
else
D(a, b)
});
makeRing(cells)
}
}
|