1 | #include <fstream>
|
---|
2 | #include <kernel>
|
---|
3 | #include <monitor>
|
---|
4 | #include <thread>
|
---|
5 |
|
---|
6 | monitor global_t {};
|
---|
7 |
|
---|
8 | global_t globalA;
|
---|
9 | global_t globalB;
|
---|
10 | global_t globalC;
|
---|
11 |
|
---|
12 | condition condAB, condAC, condBC, condABC;
|
---|
13 |
|
---|
14 | thread Signaler {};
|
---|
15 | thread WaiterAB {};
|
---|
16 | thread WaiterAC {};
|
---|
17 | thread WaiterBC {};
|
---|
18 | thread WaiterABC{};
|
---|
19 |
|
---|
20 | int state;
|
---|
21 |
|
---|
22 | /*
|
---|
23 | multi phase
|
---|
24 | */
|
---|
25 |
|
---|
26 | //----------------------------------------------------------------------------------------------------
|
---|
27 | // Tools
|
---|
28 | void signal( condition * cond, global_t * mutex a, global_t * mutex b ) {
|
---|
29 | signal( cond );
|
---|
30 | }
|
---|
31 |
|
---|
32 | void signal( condition * cond, global_t * mutex a, global_t * mutex b, global_t * mutex c ) {
|
---|
33 | signal( cond );
|
---|
34 | }
|
---|
35 |
|
---|
36 | void wait( condition * cond, global_t * mutex a, global_t * mutex b ) {
|
---|
37 | state++;
|
---|
38 | sout | "Waiting" | state | endl;
|
---|
39 | wait( cond );
|
---|
40 | sout | "Waking" | state | endl;
|
---|
41 | state--;
|
---|
42 | }
|
---|
43 |
|
---|
44 | void wait( condition * cond, global_t * mutex a, global_t * mutex b, global_t * mutex c ) {
|
---|
45 | state++;
|
---|
46 | sout | "Waiting" | state | endl;
|
---|
47 | wait( cond );
|
---|
48 | sout | "Waking" | state | endl;
|
---|
49 | state--;
|
---|
50 | }
|
---|
51 |
|
---|
52 | //----------------------------------------------------------------------------------------------------
|
---|
53 | // Signaler
|
---|
54 | // signals respectively AB, AC, BC, ABC
|
---|
55 | void signalerABC( global_t * mutex a, global_t * mutex b, global_t * mutex c ) {
|
---|
56 | sout | "Signaling ABC" | endl;
|
---|
57 | signal( &condABC, a, b, c );
|
---|
58 | sout | "Signaling AB" | endl;
|
---|
59 | signal( &condAB , a, b );
|
---|
60 | sout | "Signaling BC" | endl;
|
---|
61 | signal( &condBC , b, c );
|
---|
62 | sout | "Signaling AC" | endl;
|
---|
63 | signal( &condAC , a, c );
|
---|
64 | }
|
---|
65 |
|
---|
66 | void signalerAB( global_t * mutex a, global_t * mutex b, global_t * c ) {
|
---|
67 | signalerABC(a, b, c);
|
---|
68 | }
|
---|
69 |
|
---|
70 | void signalerA( global_t * mutex a, global_t * b, global_t * c ) {
|
---|
71 | signalerAB (a, b, c);
|
---|
72 | }
|
---|
73 |
|
---|
74 | void main( Signaler* this ) {
|
---|
75 | while( state != 4 ) { yield(); }
|
---|
76 | signalerA( &globalA, &globalB, &globalC );
|
---|
77 | }
|
---|
78 |
|
---|
79 | //----------------------------------------------------------------------------------------------------
|
---|
80 | // Waiter ABC
|
---|
81 | void main( WaiterABC* this ) {
|
---|
82 | while( state != 0 ) { yield(); }
|
---|
83 | wait( &condABC, &globalA, &globalB, &globalC );
|
---|
84 | }
|
---|
85 |
|
---|
86 | //----------------------------------------------------------------------------------------------------
|
---|
87 | // Waiter AB
|
---|
88 | void main( WaiterAB* this ) {
|
---|
89 | while( state != 1 ) { yield(); }
|
---|
90 | wait( &condAB , &globalA, &globalB );
|
---|
91 | }
|
---|
92 |
|
---|
93 | //----------------------------------------------------------------------------------------------------
|
---|
94 | // Waiter AC
|
---|
95 | void main( WaiterAC* this ) {
|
---|
96 | while( state != 2 ) { yield(); }
|
---|
97 | wait( &condAC , &globalA, &globalC );
|
---|
98 | }
|
---|
99 |
|
---|
100 | //----------------------------------------------------------------------------------------------------
|
---|
101 | // Waiter BC
|
---|
102 | void main( WaiterBC* this ) {
|
---|
103 | while( state != 3 ) { yield(); }
|
---|
104 | wait( &condBC , &globalB, &globalC );
|
---|
105 | }
|
---|
106 |
|
---|
107 | //----------------------------------------------------------------------------------------------------
|
---|
108 | // Main
|
---|
109 | int main(int argc, char* argv[]) {
|
---|
110 | state = 0;
|
---|
111 | processor p;
|
---|
112 | {
|
---|
113 | WaiterABC a;
|
---|
114 | WaiterAB b;
|
---|
115 | WaiterBC c;
|
---|
116 | WaiterAC d;
|
---|
117 | Signaler e;
|
---|
118 | }
|
---|
119 | }
|
---|