ADT
        aaron-thesis
        arm-eh
        ast-experimental
        cleanup-dtors
        deferred_resn
        demangler
        enum
        forall-pointer-decay
        jacob/cs343-translation
        jenkins-sandbox
        new-ast
        new-ast-unique-expr
        new-env
        no_list
        persistent-indexer
        pthread-emulation
        qualifiedEnum
        resolv-new
        with_gc
      
      
        
          | Last change
 on this file since 921fe6a was             0764cfb, checked in by Thierry Delisle <tdelisle@…>, 8 years ago | 
        
          | 
Preemption tests now clean properly and all use de same syntax for N
 | 
        
          | 
              
Property                 mode
 set to                 100644 | 
        
          | File size:
            1.7 KB | 
      
      
| Line |  | 
|---|
| 1 | #include <fstream> | 
|---|
| 2 | #include <kernel> | 
|---|
| 3 | #include <monitor> | 
|---|
| 4 | #include <stdlib> | 
|---|
| 5 | #include <thread> | 
|---|
| 6 |  | 
|---|
| 7 | #ifndef N | 
|---|
| 8 | #define N 100_000 | 
|---|
| 9 | #endif | 
|---|
| 10 |  | 
|---|
| 11 | enum state_t { WAIT, SIGNAL, BARGE }; | 
|---|
| 12 |  | 
|---|
| 13 | monitor global_t {}; | 
|---|
| 14 |  | 
|---|
| 15 | monitor global_data_t { | 
|---|
| 16 | bool done; | 
|---|
| 17 | int counter; | 
|---|
| 18 | state_t state; | 
|---|
| 19 |  | 
|---|
| 20 | unsigned short do_signal; | 
|---|
| 21 | unsigned short do_wait2; | 
|---|
| 22 | unsigned short do_wait1; | 
|---|
| 23 | }; | 
|---|
| 24 |  | 
|---|
| 25 | void ?{} ( global_data_t * this ) { | 
|---|
| 26 | this->done = false; | 
|---|
| 27 | this->counter = 0; | 
|---|
| 28 | this->state = BARGE; | 
|---|
| 29 |  | 
|---|
| 30 | this->do_signal = 6; | 
|---|
| 31 | this->do_wait1  = 1; | 
|---|
| 32 | this->do_wait2  = 3; | 
|---|
| 33 | } | 
|---|
| 34 |  | 
|---|
| 35 | void ^?{} ( global_data_t * this ) {} | 
|---|
| 36 |  | 
|---|
| 37 | global_t globalA; | 
|---|
| 38 | global_t globalB; | 
|---|
| 39 | global_data_t globalC; | 
|---|
| 40 |  | 
|---|
| 41 | condition cond; | 
|---|
| 42 |  | 
|---|
| 43 | thread Threads {}; | 
|---|
| 44 |  | 
|---|
| 45 | bool logicC( global_t * mutex a, global_t * mutex b, global_data_t * mutex c ) { | 
|---|
| 46 | c->counter++; | 
|---|
| 47 |  | 
|---|
| 48 | if( (c->counter % 1000) == 0 ) sout | c->counter | endl; | 
|---|
| 49 |  | 
|---|
| 50 | int action = c->counter % 10; | 
|---|
| 51 |  | 
|---|
| 52 | if( action == 0 ) { | 
|---|
| 53 | c->do_signal = max( ((unsigned)rand48()) % 10, 1); | 
|---|
| 54 | c->do_wait1 = ((unsigned)rand48()) % (c->do_signal); | 
|---|
| 55 | c->do_wait2 = ((unsigned)rand48()) % (c->do_signal); | 
|---|
| 56 |  | 
|---|
| 57 | // if(c->do_wait1 == c->do_wait2) sout | "Same" | endl; | 
|---|
| 58 | } | 
|---|
| 59 |  | 
|---|
| 60 | if( action == c->do_wait1 || action == c->do_wait2 ) { | 
|---|
| 61 | c->state = WAIT; | 
|---|
| 62 | wait( &cond ); | 
|---|
| 63 |  | 
|---|
| 64 | if(c->state != SIGNAL) { | 
|---|
| 65 | sout | "ERROR Barging detected" | c->counter | endl; | 
|---|
| 66 | abort(); | 
|---|
| 67 | } | 
|---|
| 68 | } | 
|---|
| 69 | else if( action == c->do_signal ) { | 
|---|
| 70 | c->state = SIGNAL; | 
|---|
| 71 |  | 
|---|
| 72 | signal( &cond ); | 
|---|
| 73 | signal( &cond ); | 
|---|
| 74 | } | 
|---|
| 75 | else { | 
|---|
| 76 | c->state = BARGE; | 
|---|
| 77 | } | 
|---|
| 78 |  | 
|---|
| 79 | if( c->counter >= N ) c->done = true; | 
|---|
| 80 | return !c->done; | 
|---|
| 81 | } | 
|---|
| 82 |  | 
|---|
| 83 | bool logicB( global_t * mutex a, global_t * mutex b ) { | 
|---|
| 84 | return logicC(a, b, &globalC); | 
|---|
| 85 | } | 
|---|
| 86 |  | 
|---|
| 87 | bool logicA( global_t * mutex a ) { | 
|---|
| 88 | return logicB(a, &globalB); | 
|---|
| 89 | } | 
|---|
| 90 |  | 
|---|
| 91 | void main( Threads* this ) { | 
|---|
| 92 | while( logicA(&globalA) ) { yield(); }; | 
|---|
| 93 | } | 
|---|
| 94 |  | 
|---|
| 95 | int main(int argc, char* argv[]) { | 
|---|
| 96 | rand48seed(0); | 
|---|
| 97 | processor p; | 
|---|
| 98 | { | 
|---|
| 99 | Threads t[17]; | 
|---|
| 100 | } | 
|---|
| 101 | } | 
|---|
       
      
  Note:
 See   
TracBrowser
 for help on using the repository browser.