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