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