[73abe95] | 1 | #include <fstream.hfa> |
---|
| 2 | #include <kernel.hfa> |
---|
| 3 | #include <monitor.hfa> |
---|
| 4 | #include <thread.hfa> |
---|
[19c43b7] | 5 | |
---|
| 6 | #include <stdbool.h> |
---|
| 7 | |
---|
| 8 | monitor M { |
---|
| 9 | int index; |
---|
| 10 | int last_val; |
---|
| 11 | int calls[7]; |
---|
| 12 | }; |
---|
| 13 | |
---|
| 14 | volatile bool start = false; |
---|
| 15 | |
---|
| 16 | void ?{}( M & this ) { |
---|
| 17 | this.index = 0; |
---|
| 18 | this.last_val = 0; |
---|
| 19 | for( int i = 0; i < 7; i++ ) { |
---|
| 20 | this.calls[i] = 100; //10_000; |
---|
| 21 | } |
---|
| 22 | } |
---|
| 23 | |
---|
| 24 | void ^?{} ( M & mutex this ) {} |
---|
| 25 | |
---|
| 26 | int get_index( M & mutex this ) { |
---|
| 27 | this.index += 1; |
---|
| 28 | return this.index; |
---|
| 29 | } |
---|
| 30 | |
---|
| 31 | bool call1( M & mutex this ) { |
---|
| 32 | this.last_val = 1; |
---|
| 33 | this.calls[0] -= 1; |
---|
| 34 | return this.calls[0] > 0; |
---|
| 35 | } |
---|
| 36 | |
---|
| 37 | bool call2( M & mutex this ) { |
---|
| 38 | this.last_val = 2; |
---|
| 39 | this.calls[1] -= 1; |
---|
| 40 | return this.calls[1] > 0; |
---|
| 41 | } |
---|
| 42 | |
---|
| 43 | bool call3( M & mutex this ) { |
---|
| 44 | this.last_val = 3; |
---|
| 45 | this.calls[2] -= 1; |
---|
| 46 | return this.calls[2] > 0; |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | bool call4( M & mutex this ) { |
---|
| 50 | this.last_val = 4; |
---|
| 51 | this.calls[3] -= 1; |
---|
| 52 | return this.calls[3] > 0; |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | bool call5( M & mutex this ) { |
---|
| 56 | this.last_val = 5; |
---|
| 57 | this.calls[4] -= 1; |
---|
| 58 | return this.calls[4] > 0; |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | bool call6( M & mutex this ) { |
---|
| 62 | this.last_val = 6; |
---|
| 63 | this.calls[5] -= 1; |
---|
| 64 | return this.calls[5] > 0; |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | bool call7( M & mutex this ) { |
---|
| 68 | this.last_val = 7; |
---|
| 69 | this.calls[6] -= 1; |
---|
| 70 | return this.calls[6] > 0; |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | M m; |
---|
| 74 | thread caller{}; |
---|
| 75 | |
---|
| 76 | bool call( int index ) { |
---|
| 77 | switch( index ) { |
---|
| 78 | case 1: return call1( m ); |
---|
| 79 | case 2: return call2( m ); |
---|
| 80 | case 3: return call3( m ); |
---|
| 81 | case 4: return call4( m ); |
---|
| 82 | case 5: return call5( m ); |
---|
| 83 | case 6: return call6( m ); |
---|
| 84 | case 7: return call7( m ); |
---|
| 85 | default : |
---|
[200fcb3] | 86 | serr | "Incorrect index" | index; |
---|
[19c43b7] | 87 | abort(); |
---|
| 88 | } |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | void main( caller & this ) { |
---|
| 92 | int index = get_index( m ); |
---|
| 93 | while( !start ) yield(); |
---|
| 94 | while( call( index ) ); |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | void do_wait( M & mutex this ) { |
---|
| 98 | bool done = false; |
---|
| 99 | |
---|
| 100 | start = true; |
---|
| 101 | |
---|
| 102 | while( !done ) { |
---|
| 103 | waitfor( get_index, this ); |
---|
[200fcb3] | 104 | or waitfor( call1, this ) { sout | "Statement"; if( this.last_val != 1 ) { serr | "Incorrect index: expected" | 1 | "got" | this.last_val; } } |
---|
| 105 | or waitfor( call2, this ) { sout | "Statement"; if( this.last_val != 2 ) { serr | "Incorrect index: expected" | 2 | "got" | this.last_val; } } |
---|
| 106 | or waitfor( call3, this ) { sout | "Statement"; if( this.last_val != 3 ) { serr | "Incorrect index: expected" | 3 | "got" | this.last_val; } } |
---|
| 107 | or waitfor( call4, this ) { sout | "Statement"; if( this.last_val != 4 ) { serr | "Incorrect index: expected" | 4 | "got" | this.last_val; } } |
---|
| 108 | or waitfor( call5, this ) { sout | "Statement"; if( this.last_val != 5 ) { serr | "Incorrect index: expected" | 5 | "got" | this.last_val; } } |
---|
| 109 | or waitfor( call6, this ) { sout | "Statement"; if( this.last_val != 6 ) { serr | "Incorrect index: expected" | 6 | "got" | this.last_val; } } |
---|
| 110 | or waitfor( call7, this ) { sout | "Statement"; if( this.last_val != 7 ) { serr | "Incorrect index: expected" | 7 | "got" | this.last_val; } } |
---|
[19c43b7] | 111 | |
---|
| 112 | done = true; |
---|
| 113 | for( int i = 0; i < 7; i++ ) { |
---|
| 114 | if( this.calls[i] > 0 ) { |
---|
| 115 | done = false; |
---|
| 116 | break; |
---|
| 117 | } |
---|
| 118 | } |
---|
| 119 | } |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | thread waiter{}; |
---|
| 123 | |
---|
| 124 | void main( waiter & this ) { |
---|
| 125 | do_wait( m ); |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | int main() { |
---|
| 129 | processor p[2]; |
---|
[200fcb3] | 130 | sout | "Starting"; |
---|
[19c43b7] | 131 | { |
---|
| 132 | caller c[7]; |
---|
| 133 | waiter w; |
---|
| 134 | } |
---|
[200fcb3] | 135 | sout | "Stopping"; |
---|
| 136 | } |
---|