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