[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 |
|
---|
| 26 | void ?{} ( global_data_t * this ) {
|
---|
[ccd349d] | 27 | this->last_thread = NULL;
|
---|
| 28 | this->last_signaller = NULL;
|
---|
[2c9ebab] | 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 ) {
|
---|
[1c273d0] | 41 | wait( &cond, (uintptr_t)this_thread );
|
---|
[2c9ebab] | 42 |
|
---|
| 43 | yield( ((unsigned)rand48()) % 10 );
|
---|
| 44 |
|
---|
[ccd349d] | 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 |
|
---|
[1c273d0] | 50 | a->last_thread = b->last_thread = this_thread;
|
---|
[2c9ebab] | 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 |
|
---|
[1c273d0] | 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 |
|
---|
| 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 {};
|
---|
| 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 ) {
|
---|
[1c273d0] | 96 | a->last_thread = this_thread;
|
---|
[2c9ebab] | 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 | barge_op( choose_a ? &globalA : &globalB );
|
---|
| 105 | }
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | //------------------------------------------------------------------------------
|
---|
| 109 |
|
---|
| 110 | int main(int argc, char* argv[]) {
|
---|
[cd99ef1] | 111 | rand48seed( time( NULL ) );
|
---|
[2c9ebab] | 112 | done = false;
|
---|
| 113 | processor p;
|
---|
| 114 | {
|
---|
| 115 | Signaller s[4];
|
---|
| 116 | Barger b[13];
|
---|
| 117 | sout | "Starting waiters" | endl;
|
---|
| 118 | {
|
---|
| 119 | Waiter w[3];
|
---|
| 120 | }
|
---|
| 121 | sout | "Waiters done" | endl;
|
---|
| 122 | done = true;
|
---|
| 123 | }
|
---|
[736fe25] | 124 | }
|
---|