git @ Cat's Eye Technologies Matchbox / aee7948
Peterson's algorithm freezes the browser -- but it works! Chris Pressey 10 years ago
3 changed file(s) with 57 addition(s) and 9 deletion(s). Raw diff Collapse all Expand all
9898 (This doesn't entirely make sense if `WAIT` is the final instruction in
9999 a program, though.)
100100
101 Advanced Examples
102 -----------------
103
104 There are some classic synchronization algorithms in computer science.
105 We can try some of them here, and see if Matchbox can tell us if they
106 work.
107
108 [Peterson's algorithm][] is implemented in `eg/petersons-no-race.mbox`.
109 Inside the critical sections, the programs shown above, the ones which
110 have the race condition, are embedded. Matchbox takes a while to find
111 all the interleavings, but once it does, it confirms that there is no
112 race condition.
113
114 [Dekker's algorithm][] will not be possible to implement in Matchbox,
115 because it contains a nested `while` loop (see "Limitations", below.)
116
101117 Discussion
102118 ----------
103119
146162
147163 * animate and basically display it all more nicely
148164 * ability to break on first inconsistent result (for long programs)
149 * add sufficient instructions to implement:
150
151 * [Peterson's algorithm][]
152 * maybe [Szymanski's algorithm][]
153
154 [Dekker's algorithm][] will not be possible, because it contains a
155 nested `while` loop.
165 * add sufficient instructions to implement [Szymanski's algorithm][]?
156166
157167 [race conditions]: http://en.wikipedia.org/wiki/Race_condition
158168 [Dekker's algorithm]: http://en.wikipedia.org/wiki/Dekker%27s_algorithm
0 PROG 0
1 ; M0 is flag[0]
2 ; M1 is flag[1]
3 ; M2 is turn
4 ; M3 is the target
5
6 MOV 1, M0
7 MOV 1, M2
8 WAIT M1, 0
9 WAIT M2, 0
10
11 ; begin c.s.
12 MOV M3, R0
13 INC R0
14 MOV R0, M3
15 ; end c.s.
16
17 MOV 0, M0
18
19 PROG 1
20 ; M0 is flag[0]
21 ; M1 is flag[1]
22 ; M2 is turn
23 ; M3 is the target
24
25 MOV 1, M1
26 MOV 0, M2
27 WAIT M0, 0
28 WAIT M2, 0
29
30 ; begin c.s.
31 MOV M3, R0
32 INC R0
33 MOV R0, M3
34 ; end c.s.
35
36 MOV 0, M1
3737 output.innerHTML = matchbox.run(prog1ta.value);
3838 });
3939 yoob.makeLineBreak(prog1Ctr);
40 var prog1ta = yoob.makeTextArea(prog1Ctr, 20, 10);
40 var prog1ta = yoob.makeTextArea(prog1Ctr, 20, 20);
4141
4242 var prog2Ctr = makeContainer();
4343 var run2Btn = yoob.makeButton(prog2Ctr, "Run", function() {
4444 output.innerHTML = matchbox.run(prog2ta.value);
4545 });
4646 yoob.makeLineBreak(prog2Ctr);
47 var prog2ta = yoob.makeTextArea(prog2Ctr, 20, 10);
47 var prog2ta = yoob.makeTextArea(prog2Ctr, 20, 20);
4848
4949 var resultCtr = makeContainer();
5050 var findRacesBtn = yoob.makeButton(
6767 });
6868 p.add('basic-race.mbox');
6969 p.add('basic-no-race.mbox');
70 p.add('petersons-no-race.mbox');
7071 p.select('basic-race.mbox');
7172
7273 };