1 | //--------------------------------------------------------- |
---|
2 | // Barging test |
---|
3 | // Ensures that no barging can occur between : |
---|
4 | // - the frontend of the signal_block and the signaled thread |
---|
5 | // - the signaled threadand the backend of the signal_block |
---|
6 | //--------------------------------------------------------- |
---|
7 | |
---|
8 | |
---|
9 | #include <fstream> |
---|
10 | #include <kernel> |
---|
11 | #include <monitor> |
---|
12 | #include <stdlib> |
---|
13 | #include <thread> |
---|
14 | |
---|
15 | #include <time.h> |
---|
16 | |
---|
17 | #ifdef LONG_TEST |
---|
18 | static const unsigned long N = 150_000ul; |
---|
19 | #else |
---|
20 | static const unsigned long N = 5_000ul; |
---|
21 | #endif |
---|
22 | |
---|
23 | #ifndef PREEMPTION_RATE |
---|
24 | #define PREEMPTION_RATE 10`ms |
---|
25 | #endif |
---|
26 | |
---|
27 | Duration default_preemption() { |
---|
28 | return PREEMPTION_RATE; |
---|
29 | } |
---|
30 | |
---|
31 | enum state_t { WAITED, SIGNAL, BARGE }; |
---|
32 | |
---|
33 | monitor global_data_t { |
---|
34 | thread_desc * last_thread; |
---|
35 | thread_desc * last_signaller; |
---|
36 | }; |
---|
37 | |
---|
38 | void ?{} ( global_data_t & this ) { |
---|
39 | this.last_thread = NULL; |
---|
40 | this.last_signaller = NULL; |
---|
41 | } |
---|
42 | |
---|
43 | void ^?{} ( global_data_t & this ) {} |
---|
44 | |
---|
45 | global_data_t globalA, globalB; |
---|
46 | |
---|
47 | condition cond; |
---|
48 | |
---|
49 | volatile bool done; |
---|
50 | |
---|
51 | //------------------------------------------------------------------------------ |
---|
52 | void wait_op( global_data_t & mutex a, global_data_t & mutex b, unsigned i ) { |
---|
53 | wait( cond, (uintptr_t)active_thread() ); |
---|
54 | |
---|
55 | yield( random( 10 ) ); |
---|
56 | |
---|
57 | if(a.last_thread != a.last_signaller || b.last_thread != b.last_signaller ) { |
---|
58 | sout | "ERROR Barging detected, expected" | a.last_signaller | b.last_signaller | "got" | a.last_thread | b.last_thread | endl; |
---|
59 | abort(); |
---|
60 | } |
---|
61 | |
---|
62 | a.last_thread = b.last_thread = active_thread(); |
---|
63 | |
---|
64 | yield( random( 10 ) ); |
---|
65 | } |
---|
66 | |
---|
67 | thread Waiter {}; |
---|
68 | void main( Waiter & this ) { |
---|
69 | for( int i = 0; i < N; i++ ) { |
---|
70 | wait_op( globalA, globalB, i ); |
---|
71 | } |
---|
72 | } |
---|
73 | |
---|
74 | //------------------------------------------------------------------------------ |
---|
75 | void signal_op( global_data_t & mutex a, global_data_t & mutex b ) { |
---|
76 | yield( random( 10 ) ); |
---|
77 | |
---|
78 | [a.last_thread, b.last_thread, a.last_signaller, b.last_signaller] = active_thread(); |
---|
79 | |
---|
80 | if( !is_empty( cond ) ) { |
---|
81 | |
---|
82 | thread_desc * next = front( cond ); |
---|
83 | |
---|
84 | if( ! signal_block( cond ) ) { |
---|
85 | sout | "ERROR expected to be able to signal" | endl; |
---|
86 | abort(); |
---|
87 | } |
---|
88 | |
---|
89 | yield( random( 10 ) ); |
---|
90 | |
---|
91 | if(a.last_thread != next || b.last_thread != next) { |
---|
92 | sout | "ERROR Barging detected, expected" | next | "got" | a.last_thread | b.last_thread | endl; |
---|
93 | abort(); |
---|
94 | } |
---|
95 | } |
---|
96 | |
---|
97 | } |
---|
98 | |
---|
99 | thread Signaller {}; |
---|
100 | void main( Signaller & this ) { |
---|
101 | while( !done ) { |
---|
102 | signal_op( globalA, globalB ); |
---|
103 | } |
---|
104 | } |
---|
105 | |
---|
106 | //------------------------------------------------------------------------------ |
---|
107 | void barge_op( global_data_t & mutex a ) { |
---|
108 | a.last_thread = active_thread(); |
---|
109 | } |
---|
110 | |
---|
111 | thread Barger {}; |
---|
112 | void main( Barger & this ) { |
---|
113 | for( unsigned i = 0; !done; i++ ) { |
---|
114 | //Choose some monitor to barge into with some irregular pattern |
---|
115 | bool choose_a = (i % 13) > (i % 17); |
---|
116 | if ( choose_a ) barge_op( globalA ); |
---|
117 | else barge_op( globalB ); |
---|
118 | } |
---|
119 | } |
---|
120 | |
---|
121 | //------------------------------------------------------------------------------ |
---|
122 | |
---|
123 | int main(int argc, char* argv[]) { |
---|
124 | srandom( time( NULL ) ); |
---|
125 | done = false; |
---|
126 | processor p; |
---|
127 | { |
---|
128 | Signaller s[4]; |
---|
129 | Barger b[13]; |
---|
130 | sout | "Starting waiters" | endl; |
---|
131 | { |
---|
132 | Waiter w[3]; |
---|
133 | } |
---|
134 | sout | "Waiters done" | endl; |
---|
135 | done = true; |
---|
136 | } |
---|
137 | } |
---|