source:
src/tests/sched-int-multi.c@
dba6db9
| Last change on this file since dba6db9 was a933dcf4, checked in by , 8 years ago | |
|---|---|
|
|
| File size: 1.5 KB | |
| Rev | Line | |
|---|---|---|
| [f80ab45] | 1 | #include <fstream> |
| 2 | #include <kernel> | |
| 3 | #include <monitor> | |
| 4 | #include <thread> | |
| 5 | ||
| [a933dcf4] | 6 | enum state_t { WAIT, SIGNAL, BARGE }; |
| 7 | ||
| [f80ab45] | 8 | monitor global_t {}; |
| 9 | ||
| [a933dcf4] | 10 | monitor global_data_t { |
| 11 | bool done; | |
| 12 | int counter; | |
| 13 | state_t state; | |
| 14 | }; | |
| 15 | ||
| 16 | void ?{} ( global_data_t * this ) { | |
| 17 | this->done = false; | |
| 18 | this->counter = 0; | |
| 19 | this->state = BARGE; | |
| 20 | } | |
| 21 | ||
| 22 | void ^?{} ( global_data_t * this ) {} | |
| 23 | ||
| [f80ab45] | 24 | global_t globalA; |
| [a933dcf4] | 25 | global_t globalB; |
| 26 | global_data_t globalC; | |
| [f80ab45] | 27 | |
| 28 | condition cond; | |
| 29 | ||
| [a933dcf4] | 30 | thread Threads {}; |
| [f80ab45] | 31 | |
| [a933dcf4] | 32 | bool logicC( global_t * mutex a, global_t * mutex b, global_data_t * mutex c ) { |
| 33 | c->counter++; | |
| [f80ab45] | 34 | |
| [a933dcf4] | 35 | int action = c->counter % 10; |
| 36 | ||
| 37 | if( action == 1 || action == 3 ) { | |
| 38 | if(c->state != BARGE) { | |
| 39 | sout | "ERROR Mutual exclusion is inconsistent for wait" | endl; | |
| 40 | abort(); | |
| 41 | } | |
| [f80ab45] | 42 | |
| [a933dcf4] | 43 | c->state = WAIT; |
| 44 | wait( &cond ); | |
| 45 | ||
| 46 | if(c->state != SIGNAL) { | |
| 47 | sout | "ERROR Barging detected" | endl; | |
| 48 | abort(); | |
| 49 | } | |
| 50 | } | |
| 51 | else if( action == 6 ) { | |
| 52 | if(c->state != BARGE) { | |
| 53 | sout | "ERROR Mutual exclusion is inconsistent for signal" | endl; | |
| 54 | abort(); | |
| 55 | } | |
| 56 | ||
| 57 | c->state = SIGNAL; | |
| 58 | ||
| 59 | signal( &cond ); | |
| 60 | signal( &cond ); | |
| 61 | } | |
| 62 | else { | |
| 63 | c->state = BARGE; | |
| 64 | } | |
| 65 | ||
| 66 | if( (c->counter % 1000) == 0 ) sout | c->counter | endl; | |
| 67 | if( c->counter == 100_000 ) c->done = true; | |
| 68 | return !c->done; | |
| [f80ab45] | 69 | } |
| 70 | ||
| [a933dcf4] | 71 | bool logicB( global_t * mutex a, global_t * mutex b ) { |
| 72 | return logicC(a, b, &globalC); | |
| [f80ab45] | 73 | } |
| 74 | ||
| [a933dcf4] | 75 | bool logicA( global_t * mutex a ) { |
| 76 | return logicB(a, &globalB); | |
| 77 | } | |
| [f80ab45] | 78 | |
| [a933dcf4] | 79 | void main( Threads* this ) { |
| 80 | while( logicA(&globalA) ) { yield(); }; | |
| [f80ab45] | 81 | } |
| 82 | ||
| 83 | int main(int argc, char* argv[]) { | |
| [a933dcf4] | 84 | processor p[3]; |
| [f80ab45] | 85 | { |
| [a933dcf4] | 86 | Threads t[20]; |
| [f80ab45] | 87 | } |
| 88 | } |
Note:
See TracBrowser
for help on using the repository browser.