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 307a732 was             9737ffe, checked in by Thierry Delisle <tdelisle@…>, 8 years ago | 
        
          | 
Renamed internal scheduling tests to be more evocative
 | 
        
          | 
              
Property                 mode
 set to                 100644 | 
        
          | File size:
            1.7 KB | 
      
      
| Rev | Line |  | 
|---|
| [f80ab45] | 1 | #include <fstream> | 
|---|
|  | 2 | #include <kernel> | 
|---|
|  | 3 | #include <monitor> | 
|---|
| [1ed841f] | 4 | #include <stdlib> | 
|---|
| [f80ab45] | 5 | #include <thread> | 
|---|
|  | 6 |  | 
|---|
| [a933dcf4] | 7 | enum state_t { WAIT, SIGNAL, BARGE }; | 
|---|
|  | 8 |  | 
|---|
| [f80ab45] | 9 | monitor global_t {}; | 
|---|
|  | 10 |  | 
|---|
| [a933dcf4] | 11 | monitor global_data_t { | 
|---|
|  | 12 | bool done; | 
|---|
|  | 13 | int counter; | 
|---|
|  | 14 | state_t state; | 
|---|
| [1ed841f] | 15 |  | 
|---|
|  | 16 | unsigned short do_signal; | 
|---|
|  | 17 | unsigned short do_wait2; | 
|---|
|  | 18 | unsigned short do_wait1; | 
|---|
| [a933dcf4] | 19 | }; | 
|---|
|  | 20 |  | 
|---|
|  | 21 | void ?{} ( global_data_t * this ) { | 
|---|
|  | 22 | this->done = false; | 
|---|
|  | 23 | this->counter = 0; | 
|---|
|  | 24 | this->state = BARGE; | 
|---|
| [1ed841f] | 25 |  | 
|---|
|  | 26 | this->do_signal = 6; | 
|---|
|  | 27 | this->do_wait1  = 1; | 
|---|
|  | 28 | this->do_wait2  = 3; | 
|---|
| [a933dcf4] | 29 | } | 
|---|
|  | 30 |  | 
|---|
|  | 31 | void ^?{} ( global_data_t * this ) {} | 
|---|
|  | 32 |  | 
|---|
| [f80ab45] | 33 | global_t globalA; | 
|---|
| [a933dcf4] | 34 | global_t globalB; | 
|---|
|  | 35 | global_data_t globalC; | 
|---|
| [f80ab45] | 36 |  | 
|---|
|  | 37 | condition cond; | 
|---|
|  | 38 |  | 
|---|
| [a933dcf4] | 39 | thread Threads {}; | 
|---|
| [f80ab45] | 40 |  | 
|---|
| [a933dcf4] | 41 | bool logicC( global_t * mutex a, global_t * mutex b, global_data_t * mutex c ) { | 
|---|
|  | 42 | c->counter++; | 
|---|
| [f80ab45] | 43 |  | 
|---|
| [1ed841f] | 44 | if( (c->counter % 1000) == 0 ) sout | c->counter | endl; | 
|---|
|  | 45 |  | 
|---|
| [a933dcf4] | 46 | int action = c->counter % 10; | 
|---|
|  | 47 |  | 
|---|
| [1ed841f] | 48 | if( action == 0 ) { | 
|---|
|  | 49 | c->do_signal = max( ((unsigned)rand48()) % 10, 1); | 
|---|
|  | 50 | c->do_wait1 = ((unsigned)rand48()) % (c->do_signal); | 
|---|
|  | 51 | c->do_wait2 = ((unsigned)rand48()) % (c->do_signal); | 
|---|
| [f80ab45] | 52 |  | 
|---|
| [1ed841f] | 53 | // if(c->do_wait1 == c->do_wait2) sout | "Same" | endl; | 
|---|
|  | 54 | } | 
|---|
|  | 55 |  | 
|---|
|  | 56 | if( action == c->do_wait1 || action == c->do_wait2 ) { | 
|---|
| [a933dcf4] | 57 | c->state = WAIT; | 
|---|
|  | 58 | wait( &cond ); | 
|---|
|  | 59 |  | 
|---|
|  | 60 | if(c->state != SIGNAL) { | 
|---|
| [1ed841f] | 61 | sout | "ERROR Barging detected" | c->counter | endl; | 
|---|
| [a933dcf4] | 62 | abort(); | 
|---|
|  | 63 | } | 
|---|
|  | 64 | } | 
|---|
| [1ed841f] | 65 | else if( action == c->do_signal ) { | 
|---|
| [a933dcf4] | 66 | c->state = SIGNAL; | 
|---|
|  | 67 |  | 
|---|
|  | 68 | signal( &cond ); | 
|---|
|  | 69 | signal( &cond ); | 
|---|
|  | 70 | } | 
|---|
|  | 71 | else { | 
|---|
|  | 72 | c->state = BARGE; | 
|---|
|  | 73 | } | 
|---|
|  | 74 |  | 
|---|
| [1ed841f] | 75 | if( c->counter >= 100_000 ) c->done = true; | 
|---|
| [a933dcf4] | 76 | return !c->done; | 
|---|
| [f80ab45] | 77 | } | 
|---|
|  | 78 |  | 
|---|
| [a933dcf4] | 79 | bool logicB( global_t * mutex a, global_t * mutex b ) { | 
|---|
|  | 80 | return logicC(a, b, &globalC); | 
|---|
| [f80ab45] | 81 | } | 
|---|
|  | 82 |  | 
|---|
| [a933dcf4] | 83 | bool logicA( global_t * mutex a ) { | 
|---|
|  | 84 | return logicB(a, &globalB); | 
|---|
|  | 85 | } | 
|---|
| [f80ab45] | 86 |  | 
|---|
| [a933dcf4] | 87 | void main( Threads* this ) { | 
|---|
|  | 88 | while( logicA(&globalA) ) { yield(); }; | 
|---|
| [f80ab45] | 89 | } | 
|---|
|  | 90 |  | 
|---|
|  | 91 | int main(int argc, char* argv[]) { | 
|---|
| [1ed841f] | 92 | rand48seed(0); | 
|---|
|  | 93 | processor p; | 
|---|
| [f80ab45] | 94 | { | 
|---|
| [1ed841f] | 95 | Threads t[17]; | 
|---|
| [f80ab45] | 96 | } | 
|---|
|  | 97 | } | 
|---|
       
      
  Note:
 See   
TracBrowser
 for help on using the repository browser.