ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change
on this file since e3e16bc was
9236060,
checked in by Rob Schluntz <rschlunt@…>, 7 years ago
|
Merge branch 'master' into references
|
-
Property mode set to
100644
|
File size:
1.9 KB
|
Rev | Line | |
---|
[f80ab45] | 1 | #include <fstream> |
---|
| 2 | #include <kernel> |
---|
| 3 | #include <monitor> |
---|
[1ed841f] | 4 | #include <stdlib> |
---|
[f80ab45] | 5 | #include <thread> |
---|
| 6 | |
---|
[cd99ef1] | 7 | static const unsigned long N = 50_000ul; |
---|
| 8 | |
---|
| 9 | #ifndef PREEMPTION_RATE |
---|
| 10 | #define PREEMPTION_RATE 10_000ul |
---|
[0764cfb] | 11 | #endif |
---|
| 12 | |
---|
[cd99ef1] | 13 | unsigned int default_preemption() { |
---|
| 14 | return 0; |
---|
| 15 | } |
---|
[a933dcf4] | 16 | enum state_t { WAIT, SIGNAL, BARGE }; |
---|
| 17 | |
---|
[f80ab45] | 18 | monitor global_t {}; |
---|
| 19 | |
---|
[a933dcf4] | 20 | monitor global_data_t { |
---|
[cd99ef1] | 21 | volatile bool done; |
---|
[a933dcf4] | 22 | int counter; |
---|
| 23 | state_t state; |
---|
[1ed841f] | 24 | |
---|
| 25 | unsigned short do_signal; |
---|
| 26 | unsigned short do_wait2; |
---|
| 27 | unsigned short do_wait1; |
---|
[a933dcf4] | 28 | }; |
---|
| 29 | |
---|
[2afec66] | 30 | void ?{} ( global_data_t & this ) { |
---|
| 31 | this.done = false; |
---|
| 32 | this.counter = 0; |
---|
| 33 | this.state = BARGE; |
---|
| 34 | |
---|
| 35 | this.do_signal = 6; |
---|
| 36 | this.do_wait1 = 1; |
---|
| 37 | this.do_wait2 = 3; |
---|
[a933dcf4] | 38 | } |
---|
| 39 | |
---|
[2afec66] | 40 | void ^?{} ( global_data_t & this ) {} |
---|
[a933dcf4] | 41 | |
---|
[f80ab45] | 42 | global_t globalA; |
---|
[a933dcf4] | 43 | global_t globalB; |
---|
| 44 | global_data_t globalC; |
---|
[f80ab45] | 45 | |
---|
| 46 | condition cond; |
---|
| 47 | |
---|
[a933dcf4] | 48 | thread Threads {}; |
---|
[f80ab45] | 49 | |
---|
[83a071f9] | 50 | bool logicC( global_t & mutex a, global_t & mutex b, global_data_t & mutex c ) { |
---|
| 51 | c.counter++; |
---|
[f80ab45] | 52 | |
---|
[83a071f9] | 53 | if( (c.counter % 1000) == 0 ) sout | c.counter | endl; |
---|
[1ed841f] | 54 | |
---|
[83a071f9] | 55 | int action = c.counter % 10; |
---|
[a933dcf4] | 56 | |
---|
[1ed841f] | 57 | if( action == 0 ) { |
---|
[83a071f9] | 58 | c.do_signal = max( ((unsigned)rand48()) % 10, 1); |
---|
| 59 | c.do_wait1 = ((unsigned)rand48()) % (c.do_signal); |
---|
| 60 | c.do_wait2 = ((unsigned)rand48()) % (c.do_signal); |
---|
[f80ab45] | 61 | |
---|
[9236060] | 62 | if(c.do_wait1 == c.do_wait2) sout | "Same" | endl; |
---|
[1ed841f] | 63 | } |
---|
| 64 | |
---|
[83a071f9] | 65 | if( action == c.do_wait1 || action == c.do_wait2 ) { |
---|
| 66 | c.state = WAIT; |
---|
[a933dcf4] | 67 | wait( &cond ); |
---|
| 68 | |
---|
[83a071f9] | 69 | if(c.state != SIGNAL) { |
---|
| 70 | sout | "ERROR Barging detected" | c.counter | endl; |
---|
[a933dcf4] | 71 | abort(); |
---|
| 72 | } |
---|
| 73 | } |
---|
[83a071f9] | 74 | else if( action == c.do_signal ) { |
---|
| 75 | c.state = SIGNAL; |
---|
[a933dcf4] | 76 | |
---|
| 77 | signal( &cond ); |
---|
| 78 | signal( &cond ); |
---|
| 79 | } |
---|
| 80 | else { |
---|
[83a071f9] | 81 | c.state = BARGE; |
---|
[a933dcf4] | 82 | } |
---|
| 83 | |
---|
[9236060] | 84 | if( c.counter >= N ) c.done = true; |
---|
[83a071f9] | 85 | return !c.done; |
---|
[f80ab45] | 86 | } |
---|
| 87 | |
---|
[83a071f9] | 88 | bool logicB( global_t & mutex a, global_t & mutex b ) { |
---|
| 89 | return logicC(a, b, globalC); |
---|
[f80ab45] | 90 | } |
---|
| 91 | |
---|
[83a071f9] | 92 | bool logicA( global_t & mutex a ) { |
---|
| 93 | return logicB(a, globalB); |
---|
[a933dcf4] | 94 | } |
---|
[f80ab45] | 95 | |
---|
[83a071f9] | 96 | void main( Threads & this ) { |
---|
| 97 | while( logicA(globalA) ) { yield(); }; |
---|
[f80ab45] | 98 | } |
---|
| 99 | |
---|
[cd99ef1] | 100 | static thread_desc * volatile the_threads; |
---|
| 101 | |
---|
[f80ab45] | 102 | int main(int argc, char* argv[]) { |
---|
[1ed841f] | 103 | rand48seed(0); |
---|
| 104 | processor p; |
---|
[f80ab45] | 105 | { |
---|
[1ed841f] | 106 | Threads t[17]; |
---|
[9236060] | 107 | the_threads = (thread_desc*)t; |
---|
[f80ab45] | 108 | } |
---|
[2afec66] | 109 | } |
---|
Note: See
TracBrowser
for help on using the repository browser.