1 | #include <fstream> |
---|
2 | #include <kernel> |
---|
3 | #include <monitor> |
---|
4 | #include <thread> |
---|
5 | |
---|
6 | #include <time.h> |
---|
7 | |
---|
8 | #ifdef LONG_TEST |
---|
9 | static const unsigned long N = 300_000ul; |
---|
10 | #else |
---|
11 | static const unsigned long N = 10_000ul; |
---|
12 | #endif |
---|
13 | |
---|
14 | #ifndef PREEMPTION_RATE |
---|
15 | #define PREEMPTION_RATE 10_000ul |
---|
16 | #endif |
---|
17 | |
---|
18 | unsigned int default_preemption() { |
---|
19 | return PREEMPTION_RATE; |
---|
20 | } |
---|
21 | |
---|
22 | enum state_t { WAIT, SIGNAL, BARGE }; |
---|
23 | |
---|
24 | monitor global_t {}; |
---|
25 | global_t mut; |
---|
26 | |
---|
27 | monitor global_data_t; |
---|
28 | void ?{}( global_data_t & this ); |
---|
29 | void ^?{} ( global_data_t & this ); |
---|
30 | |
---|
31 | monitor global_data_t { |
---|
32 | int counter; |
---|
33 | state_t state; |
---|
34 | } data; |
---|
35 | |
---|
36 | condition cond; |
---|
37 | |
---|
38 | volatile bool all_done; |
---|
39 | |
---|
40 | void ?{}( global_data_t & this ) { |
---|
41 | this.counter == 0; |
---|
42 | this.state = BARGE; |
---|
43 | } |
---|
44 | |
---|
45 | void ^?{} ( global_data_t & this ) {} |
---|
46 | |
---|
47 | //------------------------------------------------------------------------------ |
---|
48 | // Barging logic |
---|
49 | void barge( global_data_t & mutex d ) { |
---|
50 | d.state = BARGE; |
---|
51 | } |
---|
52 | |
---|
53 | thread Barger {}; |
---|
54 | |
---|
55 | void main( Barger & this ) { |
---|
56 | while( !all_done ) { |
---|
57 | barge( data ); |
---|
58 | yield(); |
---|
59 | } |
---|
60 | } |
---|
61 | |
---|
62 | //------------------------------------------------------------------------------ |
---|
63 | // Waiting logic |
---|
64 | bool wait( global_t & mutex m, global_data_t & mutex d ) { |
---|
65 | wait( cond ); |
---|
66 | if( d.state != SIGNAL ) { |
---|
67 | sout | "ERROR barging!" | endl; |
---|
68 | } |
---|
69 | |
---|
70 | d.counter++; |
---|
71 | |
---|
72 | if( (d.counter % 1000) == 0 ) sout | d.counter | endl; |
---|
73 | |
---|
74 | return d.counter < N; |
---|
75 | } |
---|
76 | |
---|
77 | thread Waiter {}; |
---|
78 | |
---|
79 | void main( Waiter & this ) { |
---|
80 | while( wait( mut, data ) ) { yield(); } |
---|
81 | } |
---|
82 | |
---|
83 | |
---|
84 | //------------------------------------------------------------------------------ |
---|
85 | // Signalling logic |
---|
86 | void signal( condition & cond, global_t & mutex a, global_data_t & mutex b ) { |
---|
87 | b.state = SIGNAL; |
---|
88 | signal( cond ); |
---|
89 | } |
---|
90 | |
---|
91 | void logic( global_t & mutex a ) { |
---|
92 | signal( cond, a, data ); |
---|
93 | |
---|
94 | yield( random( 10 ) ); |
---|
95 | |
---|
96 | //This is technically a mutual exclusion violation but the mutex monitor protects us |
---|
97 | bool running = data.counter < N && data.counter > 0; |
---|
98 | if( data.state != SIGNAL && running ) { |
---|
99 | sout | "ERROR Eager signal" | data.state | endl; |
---|
100 | } |
---|
101 | } |
---|
102 | |
---|
103 | thread Signaller {}; |
---|
104 | |
---|
105 | void main( Signaller & this ) { |
---|
106 | while( !all_done ) { |
---|
107 | logic( mut ); |
---|
108 | yield(); |
---|
109 | } |
---|
110 | } |
---|
111 | |
---|
112 | //------------------------------------------------------------------------------ |
---|
113 | // Main loop |
---|
114 | int main(int argc, char* argv[]) { |
---|
115 | srandom( time( NULL ) ); |
---|
116 | all_done = false; |
---|
117 | processor p; |
---|
118 | { |
---|
119 | Signaller s; |
---|
120 | Barger b[17]; |
---|
121 | { |
---|
122 | Waiter w[4]; |
---|
123 | } |
---|
124 | sout | "All waiter done" | endl; |
---|
125 | all_done = true; |
---|
126 | } |
---|
127 | } |
---|