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