| [73abe95] | 1 | #include <fstream.hfa> | 
|---|
| [096a2ff] | 2 | #include <kernel.hfa> | 
|---|
| [73abe95] | 3 | #include <monitor.hfa> | 
|---|
|  | 4 | #include <thread.hfa> | 
|---|
|  | 5 | #include <time.hfa> | 
|---|
| [303406a] | 6 |  | 
|---|
| [dc8511c] | 7 | #include "long_tests.hfa" | 
|---|
| [7bdcac1] | 8 |  | 
|---|
| [cd99ef1] | 9 | #ifndef PREEMPTION_RATE | 
|---|
| [8ad6533] | 10 | #define PREEMPTION_RATE 10`ms | 
|---|
| [e1c1829] | 11 | #endif | 
|---|
| [5ea06d6] | 12 |  | 
|---|
| [8ad6533] | 13 | Duration default_preemption() { | 
|---|
| [cd99ef1] | 14 | return PREEMPTION_RATE; | 
|---|
|  | 15 | } | 
|---|
|  | 16 |  | 
|---|
| [7bdcac1] | 17 | #ifdef TEST_LONG | 
|---|
| [b9da9585] | 18 | static const unsigned long N = 300_000ul; | 
|---|
|  | 19 | #else | 
|---|
|  | 20 | static const unsigned long N = 10_000ul; | 
|---|
|  | 21 | #endif | 
|---|
|  | 22 |  | 
|---|
| [102a58b] | 23 | enum state_t { WAIT, SIGNAL, BARGE }; | 
|---|
|  | 24 |  | 
|---|
|  | 25 | monitor global_t {}; | 
|---|
|  | 26 | global_t mut; | 
|---|
|  | 27 |  | 
|---|
|  | 28 | monitor global_data_t; | 
|---|
| [2afec66] | 29 | void ?{}( global_data_t & this ); | 
|---|
| [8638cef] | 30 | void ^?{} ( global_data_t & mutex this ); | 
|---|
| [102a58b] | 31 |  | 
|---|
|  | 32 | monitor global_data_t { | 
|---|
|  | 33 | int counter; | 
|---|
|  | 34 | state_t state; | 
|---|
|  | 35 | } data; | 
|---|
| [5ea06d6] | 36 |  | 
|---|
|  | 37 | condition cond; | 
|---|
|  | 38 |  | 
|---|
| [102a58b] | 39 | volatile bool all_done; | 
|---|
| [5ea06d6] | 40 |  | 
|---|
| [2afec66] | 41 | void ?{}( global_data_t & this ) { | 
|---|
|  | 42 | this.counter == 0; | 
|---|
|  | 43 | this.state = BARGE; | 
|---|
| [5ea06d6] | 44 | } | 
|---|
|  | 45 |  | 
|---|
| [8638cef] | 46 | void ^?{} ( global_data_t & mutex this ) {} | 
|---|
| [5ea06d6] | 47 |  | 
|---|
| [102a58b] | 48 | //------------------------------------------------------------------------------ | 
|---|
|  | 49 | // Barging logic | 
|---|
| [83a071f9] | 50 | void barge( global_data_t & mutex d ) { | 
|---|
|  | 51 | d.state = BARGE; | 
|---|
| [5ea06d6] | 52 | } | 
|---|
|  | 53 |  | 
|---|
| [102a58b] | 54 | thread Barger {}; | 
|---|
| [5ea06d6] | 55 |  | 
|---|
| [83a071f9] | 56 | void main( Barger & this ) { | 
|---|
| [0322865c] | 57 | while( !all_done ) { | 
|---|
| [83a071f9] | 58 | barge( data ); | 
|---|
| [0322865c] | 59 | yield(); | 
|---|
| [102a58b] | 60 | } | 
|---|
| [5ea06d6] | 61 | } | 
|---|
|  | 62 |  | 
|---|
| [102a58b] | 63 | //------------------------------------------------------------------------------ | 
|---|
|  | 64 | // Waiting logic | 
|---|
| [83a071f9] | 65 | bool wait( global_t & mutex m, global_data_t & mutex d ) { | 
|---|
| [4cedd9f] | 66 | wait( cond ); | 
|---|
| [83a071f9] | 67 | if( d.state != SIGNAL ) { | 
|---|
| [200fcb3] | 68 | sout | "ERROR barging!"; | 
|---|
| [102a58b] | 69 | } | 
|---|
|  | 70 |  | 
|---|
| [ef952d7] | 71 | #if !defined(TEST_FOREVER) | 
|---|
|  | 72 | d.counter++; | 
|---|
| [200fcb3] | 73 | if( (d.counter % 1000) == 0 ) sout | d.counter; | 
|---|
| [ef952d7] | 74 | #endif | 
|---|
| [102a58b] | 75 |  | 
|---|
| [7bdcac1] | 76 | return TEST(d.counter < N); | 
|---|
| [5ea06d6] | 77 | } | 
|---|
|  | 78 |  | 
|---|
| [102a58b] | 79 | thread Waiter {}; | 
|---|
|  | 80 |  | 
|---|
| [83a071f9] | 81 | void main( Waiter & this ) { | 
|---|
| [7bdcac1] | 82 | while( wait( mut, data ) ) { KICK_WATCHDOG; yield(); } | 
|---|
| [102a58b] | 83 | } | 
|---|
|  | 84 |  | 
|---|
|  | 85 |  | 
|---|
|  | 86 | //------------------------------------------------------------------------------ | 
|---|
|  | 87 | // Signalling logic | 
|---|
| [4cedd9f] | 88 | void signal( condition & cond, global_t & mutex a, global_data_t & mutex b ) { | 
|---|
| [83a071f9] | 89 | b.state = SIGNAL; | 
|---|
| [102a58b] | 90 | signal( cond ); | 
|---|
|  | 91 | } | 
|---|
|  | 92 |  | 
|---|
| [83a071f9] | 93 | void logic( global_t & mutex a ) { | 
|---|
| [4cedd9f] | 94 | signal( cond, a, data ); | 
|---|
| [102a58b] | 95 |  | 
|---|
| [6c7b1e7] | 96 | yield( random( 10 ) ); | 
|---|
| [5ea06d6] | 97 |  | 
|---|
| [102a58b] | 98 | //This is technically a mutual exclusion violation but the mutex monitor protects us | 
|---|
| [7bdcac1] | 99 | bool running = TEST(data.counter < N) && data.counter > 0; | 
|---|
| [102a58b] | 100 | if( data.state != SIGNAL && running ) { | 
|---|
| [200fcb3] | 101 | sout | "ERROR Eager signal" | data.state; | 
|---|
| [102a58b] | 102 | } | 
|---|
| [5ea06d6] | 103 | } | 
|---|
|  | 104 |  | 
|---|
| [102a58b] | 105 | thread Signaller {}; | 
|---|
|  | 106 |  | 
|---|
| [83a071f9] | 107 | void main( Signaller & this ) { | 
|---|
| [0322865c] | 108 | while( !all_done ) { | 
|---|
| [83a071f9] | 109 | logic( mut ); | 
|---|
| [0322865c] | 110 | yield(); | 
|---|
| [102a58b] | 111 | } | 
|---|
|  | 112 | } | 
|---|
|  | 113 |  | 
|---|
|  | 114 | //------------------------------------------------------------------------------ | 
|---|
|  | 115 | // Main loop | 
|---|
| [5ea06d6] | 116 | int main(int argc, char* argv[]) { | 
|---|
| [54aba8d] | 117 | srandom( time( NULL ) ); | 
|---|
| [102a58b] | 118 | all_done = false; | 
|---|
| [5ea06d6] | 119 | processor p; | 
|---|
|  | 120 | { | 
|---|
| [102a58b] | 121 | Signaller s; | 
|---|
|  | 122 | Barger b[17]; | 
|---|
|  | 123 | { | 
|---|
|  | 124 | Waiter w[4]; | 
|---|
|  | 125 | } | 
|---|
| [200fcb3] | 126 | sout | "All waiter done"; | 
|---|
| [102a58b] | 127 | all_done = true; | 
|---|
| [0322865c] | 128 | } | 
|---|
| [2afec66] | 129 | } | 
|---|