1 | #include <fstream> |
---|
2 | #include <kernel> |
---|
3 | #include <monitor> |
---|
4 | #include <stdlib> |
---|
5 | #include <thread> |
---|
6 | |
---|
7 | static const unsigned N = 100_000; |
---|
8 | |
---|
9 | enum state_t { WAITED, SIGNAL, BARGE }; |
---|
10 | |
---|
11 | monitor global_t {}; |
---|
12 | |
---|
13 | monitor global_data_t { |
---|
14 | state_t state; |
---|
15 | bool ran; |
---|
16 | }; |
---|
17 | |
---|
18 | void ?{} ( global_data_t * this ) { |
---|
19 | this->state = BARGE; |
---|
20 | } |
---|
21 | |
---|
22 | void ^?{} ( global_data_t * this ) {} |
---|
23 | |
---|
24 | global_data_t globalA, globalB; |
---|
25 | |
---|
26 | condition cond; |
---|
27 | |
---|
28 | volatile bool done; |
---|
29 | |
---|
30 | //------------------------------------------------------------------------------ |
---|
31 | void wait_op( global_data_t * mutex a, global_data_t * mutex b, unsigned i ) { |
---|
32 | wait( &cond ); |
---|
33 | a->ran = b->ran = true; |
---|
34 | |
---|
35 | yield( ((unsigned)rand48()) % 10 ); |
---|
36 | |
---|
37 | if(a->state != SIGNAL || b->state != SIGNAL) { |
---|
38 | sout | "ERROR Barging detected" | a->state | b->state | endl; |
---|
39 | abort(); |
---|
40 | } |
---|
41 | |
---|
42 | a->state = b->state = WAITED; |
---|
43 | |
---|
44 | yield( ((unsigned)rand48()) % 10 ); |
---|
45 | } |
---|
46 | |
---|
47 | thread Waiter {}; |
---|
48 | void main( Waiter* this ) { |
---|
49 | for( int i = 0; i < N; i++ ) { |
---|
50 | wait_op( &globalA, &globalB, i ); |
---|
51 | } |
---|
52 | } |
---|
53 | |
---|
54 | //------------------------------------------------------------------------------ |
---|
55 | void signal_op( global_data_t * mutex a, global_data_t * mutex b ) { |
---|
56 | yield( ((unsigned)rand48()) % 10 ); |
---|
57 | |
---|
58 | a->ran = b->ran = false; |
---|
59 | a->state = b->state = SIGNAL; |
---|
60 | |
---|
61 | signal_block( &cond ); |
---|
62 | |
---|
63 | yield( ((unsigned)rand48()) % 10 ); |
---|
64 | |
---|
65 | assert(a->ran == b->ran); |
---|
66 | if(a->ran) |
---|
67 | { |
---|
68 | if(a->state != WAITED || b->state != WAITED) { |
---|
69 | sout | "ERROR Barging detected" | a->state | b->state | endl; |
---|
70 | abort(); |
---|
71 | } |
---|
72 | } |
---|
73 | |
---|
74 | } |
---|
75 | |
---|
76 | thread Signaller {}; |
---|
77 | void main( Signaller* this ) { |
---|
78 | while( !done ) { |
---|
79 | signal_op( &globalA, &globalB ); |
---|
80 | } |
---|
81 | } |
---|
82 | |
---|
83 | //------------------------------------------------------------------------------ |
---|
84 | void barge_op( global_data_t * mutex a ) { |
---|
85 | a->state = BARGE; |
---|
86 | } |
---|
87 | |
---|
88 | thread Barger {}; |
---|
89 | void main( Barger* this ) { |
---|
90 | for( unsigned i = 0; !done; i++ ) { |
---|
91 | //Choose some monitor to barge into with some irregular pattern |
---|
92 | bool choose_a = (i % 13) > (i % 17); |
---|
93 | barge_op( choose_a ? &globalA : &globalB ); |
---|
94 | } |
---|
95 | } |
---|
96 | |
---|
97 | //------------------------------------------------------------------------------ |
---|
98 | |
---|
99 | int main(int argc, char* argv[]) { |
---|
100 | rand48seed(0); |
---|
101 | done = false; |
---|
102 | processor p; |
---|
103 | { |
---|
104 | Signaller s[4]; |
---|
105 | Barger b[13]; |
---|
106 | sout | "Starting waiters" | endl; |
---|
107 | { |
---|
108 | Waiter w[3]; |
---|
109 | } |
---|
110 | sout | "Waiters done" | endl; |
---|
111 | done = true; |
---|
112 | } |
---|
113 | } |
---|