| 1 | //----------------------------------------------------------------
 | 
|---|
| 2 | // When test
 | 
|---|
| 3 | // Ensures that when clauses on waitfor are respected
 | 
|---|
| 4 | //-----------------------------------------------------------------
 | 
|---|
| 5 | 
 | 
|---|
| 6 | #include <fstream>
 | 
|---|
| 7 | #include <kernel>
 | 
|---|
| 8 | #include <monitor>
 | 
|---|
| 9 | #include <stdlib>
 | 
|---|
| 10 | #include <thread>
 | 
|---|
| 11 | 
 | 
|---|
| 12 | #include <stdbool.h>
 | 
|---|
| 13 | #include <time.h>
 | 
|---|
| 14 | 
 | 
|---|
| 15 | static const unsigned long N = 4_998ul;
 | 
|---|
| 16 | 
 | 
|---|
| 17 | static inline void rand_yield() { yield(random( 10 )); }
 | 
|---|
| 18 | 
 | 
|---|
| 19 | monitor global_t {
 | 
|---|
| 20 |         int last_call;
 | 
|---|
| 21 |         bool done;
 | 
|---|
| 22 | };
 | 
|---|
| 23 | 
 | 
|---|
| 24 | void ?{} ( global_t & this ) {
 | 
|---|
| 25 |         this.last_call = 6;
 | 
|---|
| 26 |         this.done = false;
 | 
|---|
| 27 | }
 | 
|---|
| 28 | 
 | 
|---|
| 29 | void ^?{} ( global_t & mutex this ) {}
 | 
|---|
| 30 | 
 | 
|---|
| 31 | global_t global;
 | 
|---|
| 32 | 
 | 
|---|
| 33 | bool call1( global_t & mutex this ) { this.last_call = 1; return this.done; }
 | 
|---|
| 34 | bool call2( global_t & mutex this ) { this.last_call = 2; return this.done; }
 | 
|---|
| 35 | bool call3( global_t & mutex this ) { this.last_call = 3; return this.done; }
 | 
|---|
| 36 | bool call4( global_t & mutex this ) { this.last_call = 4; return this.done; }
 | 
|---|
| 37 | bool call5( global_t & mutex this ) { this.last_call = 5; return this.done; }
 | 
|---|
| 38 | bool call6( global_t & mutex this ) { this.last_call = 6; return this.done; }
 | 
|---|
| 39 | 
 | 
|---|
| 40 | thread caller_t{};
 | 
|---|
| 41 | void main( caller_t & this ) {
 | 
|---|
| 42 |         while( true ) {
 | 
|---|
| 43 |                 rand_yield();
 | 
|---|
| 44 |                 if( call1( global ) ) return;
 | 
|---|
| 45 |                 rand_yield();
 | 
|---|
| 46 |                 if( call2( global ) ) return;
 | 
|---|
| 47 |                 rand_yield();
 | 
|---|
| 48 |                 if( call3( global ) ) return;
 | 
|---|
| 49 |                 rand_yield();
 | 
|---|
| 50 |                 if( call4( global ) ) return;
 | 
|---|
| 51 |                 rand_yield();
 | 
|---|
| 52 |                 if( call5( global ) ) return;
 | 
|---|
| 53 |                 rand_yield();
 | 
|---|
| 54 |                 if( call6( global ) ) return;
 | 
|---|
| 55 |         }
 | 
|---|
| 56 | }
 | 
|---|
| 57 | 
 | 
|---|
| 58 | void arbiter( global_t & mutex this ) {
 | 
|---|
| 59 |         for( int i = 0; i < N; i++ ) {
 | 
|---|
| 60 |                    when( this.last_call == 6 ) waitfor( call1, this ) { if( this.last_call != 1) { serr | "Expected last_call to be 1 got" | this.last_call | endl; } }
 | 
|---|
| 61 |                 or when( this.last_call == 1 ) waitfor( call2, this ) { if( this.last_call != 2) { serr | "Expected last_call to be 2 got" | this.last_call | endl; } }
 | 
|---|
| 62 |                 or when( this.last_call == 2 ) waitfor( call3, this ) { if( this.last_call != 3) { serr | "Expected last_call to be 3 got" | this.last_call | endl; } }
 | 
|---|
| 63 |                 or when( this.last_call == 3 ) waitfor( call4, this ) { if( this.last_call != 4) { serr | "Expected last_call to be 4 got" | this.last_call | endl; } }
 | 
|---|
| 64 |                 or when( this.last_call == 4 ) waitfor( call5, this ) { if( this.last_call != 5) { serr | "Expected last_call to be 5 got" | this.last_call | endl; } }
 | 
|---|
| 65 |                 or when( this.last_call == 5 ) waitfor( call6, this ) { if( this.last_call != 6) { serr | "Expected last_call to be 6 got" | this.last_call | endl; } }
 | 
|---|
| 66 | 
 | 
|---|
| 67 |                 sout | this.last_call | endl;
 | 
|---|
| 68 |         }
 | 
|---|
| 69 | 
 | 
|---|
| 70 |         this.done = true;
 | 
|---|
| 71 | }
 | 
|---|
| 72 | 
 | 
|---|
| 73 | thread arbiter_t{};
 | 
|---|
| 74 | void main( arbiter_t & this ) {
 | 
|---|
| 75 |         arbiter( global );
 | 
|---|
| 76 | }
 | 
|---|
| 77 | 
 | 
|---|
| 78 | int main() {
 | 
|---|
| 79 |         random_seed( time(NULL) );
 | 
|---|
| 80 |         sout | "Starting" | endl;
 | 
|---|
| 81 |         {
 | 
|---|
| 82 |                 arbiter_t arbiter;
 | 
|---|
| 83 |                 caller_t callers[7];
 | 
|---|
| 84 | 
 | 
|---|
| 85 |         }
 | 
|---|
| 86 |         sout | "Stopping" | endl;
 | 
|---|
| 87 | }
 | 
|---|