| 1 | #include <fstream>
 | 
|---|
| 2 | #include <kernel>
 | 
|---|
| 3 | #include <monitor>
 | 
|---|
| 4 | #include <stdlib>
 | 
|---|
| 5 | #include <thread>
 | 
|---|
| 6 | 
 | 
|---|
| 7 | #include <time.h>
 | 
|---|
| 8 | 
 | 
|---|
| 9 | static const unsigned long N = 5_000ul;
 | 
|---|
| 10 | 
 | 
|---|
| 11 | #ifndef PREEMPTION_RATE
 | 
|---|
| 12 | #define PREEMPTION_RATE 10_000ul
 | 
|---|
| 13 | #endif
 | 
|---|
| 14 | 
 | 
|---|
| 15 | unsigned int default_preemption() {
 | 
|---|
| 16 |         return PREEMPTION_RATE;
 | 
|---|
| 17 | }
 | 
|---|
| 18 | 
 | 
|---|
| 19 | enum state_t { WAITED, SIGNAL, BARGE };
 | 
|---|
| 20 | 
 | 
|---|
| 21 | monitor global_data_t {
 | 
|---|
| 22 |         thread_desc * last_thread;
 | 
|---|
| 23 |         thread_desc * last_signaller;
 | 
|---|
| 24 | };
 | 
|---|
| 25 | 
 | 
|---|
| 26 | void ?{} ( global_data_t & this ) {
 | 
|---|
| 27 |         this.last_thread = NULL;
 | 
|---|
| 28 |         this.last_signaller = NULL;
 | 
|---|
| 29 | }
 | 
|---|
| 30 | 
 | 
|---|
| 31 | void ^?{} ( global_data_t & this ) {}
 | 
|---|
| 32 | 
 | 
|---|
| 33 | global_data_t globalA, globalB;
 | 
|---|
| 34 | 
 | 
|---|
| 35 | condition cond;
 | 
|---|
| 36 | 
 | 
|---|
| 37 | volatile bool done;
 | 
|---|
| 38 | 
 | 
|---|
| 39 | //------------------------------------------------------------------------------
 | 
|---|
| 40 | void wait_op( global_data_t & mutex a, global_data_t & mutex b, unsigned i ) {
 | 
|---|
| 41 |         wait( &cond, (uintptr_t)this_thread );
 | 
|---|
| 42 | 
 | 
|---|
| 43 |         yield( ((unsigned)rand48()) % 10 );
 | 
|---|
| 44 | 
 | 
|---|
| 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;
 | 
|---|
| 47 |                 abort();
 | 
|---|
| 48 |         }
 | 
|---|
| 49 | 
 | 
|---|
| 50 |         a.last_thread = b.last_thread = this_thread;
 | 
|---|
| 51 | 
 | 
|---|
| 52 |         yield( ((unsigned)rand48()) % 10 );
 | 
|---|
| 53 | }
 | 
|---|
| 54 | 
 | 
|---|
| 55 | thread Waiter {};
 | 
|---|
| 56 | void main( Waiter & this ) {
 | 
|---|
| 57 |         for( int i = 0; i < N; i++ ) {
 | 
|---|
| 58 |                 wait_op( globalA, globalB, i );
 | 
|---|
| 59 |         }
 | 
|---|
| 60 | }
 | 
|---|
| 61 | 
 | 
|---|
| 62 | //------------------------------------------------------------------------------
 | 
|---|
| 63 | void signal_op( global_data_t & mutex a, global_data_t & mutex b ) {
 | 
|---|
| 64 |         yield( ((unsigned)rand48()) % 10 );
 | 
|---|
| 65 | 
 | 
|---|
| 66 |         [a.last_thread, b.last_thread, a.last_signaller, b.last_signaller] = this_thread;
 | 
|---|
| 67 | 
 | 
|---|
| 68 |         if( !is_empty( &cond ) ) {
 | 
|---|
| 69 | 
 | 
|---|
| 70 |                 thread_desc * next = front( &cond );
 | 
|---|
| 71 | 
 | 
|---|
| 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 | 
 | 
|---|
| 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;
 | 
|---|
| 81 |                         abort();
 | 
|---|
| 82 |                 }
 | 
|---|
| 83 |         }
 | 
|---|
| 84 | 
 | 
|---|
| 85 | }
 | 
|---|
| 86 | 
 | 
|---|
| 87 | thread Signaller {};
 | 
|---|
| 88 | void main( Signaller & this ) {
 | 
|---|
| 89 |         while( !done ) {
 | 
|---|
| 90 |                 signal_op( globalA, globalB );
 | 
|---|
| 91 |         }
 | 
|---|
| 92 | }
 | 
|---|
| 93 | 
 | 
|---|
| 94 | //------------------------------------------------------------------------------
 | 
|---|
| 95 | void barge_op( global_data_t & mutex a ) {
 | 
|---|
| 96 |         a.last_thread = this_thread;
 | 
|---|
| 97 | }
 | 
|---|
| 98 | 
 | 
|---|
| 99 | thread Barger {};
 | 
|---|
| 100 | void main( Barger & this ) {
 | 
|---|
| 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);
 | 
|---|
| 104 |                 if ( choose_a ) barge_op( globalA );
 | 
|---|
| 105 |                 else barge_op( globalB );
 | 
|---|
| 106 |         }
 | 
|---|
| 107 | }
 | 
|---|
| 108 | 
 | 
|---|
| 109 | //------------------------------------------------------------------------------
 | 
|---|
| 110 | 
 | 
|---|
| 111 | int main(int argc, char* argv[]) {
 | 
|---|
| 112 |         rand48seed( time( NULL ) );
 | 
|---|
| 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 |         }
 | 
|---|
| 125 | }
 | 
|---|