source: src/tests/sched-int-block.c@ 5c69a1e

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 5c69a1e was 736fe25, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

remove global_t monitor declaration

  • Property mode set to 100644
File size: 2.2 KB
RevLine 
[2c9ebab]1#include <fstream>
2#include <kernel>
3#include <monitor>
4#include <stdlib>
5#include <thread>
6
7static const unsigned N = 100_000;
8
9enum state_t { WAITED, SIGNAL, BARGE };
10
11monitor global_data_t {
12 state_t state;
13 bool ran;
14};
15
16void ?{} ( global_data_t * this ) {
17 this->state = BARGE;
18}
19
20void ^?{} ( global_data_t * this ) {}
21
22global_data_t globalA, globalB;
23
24condition cond;
25
26volatile bool done;
27
28//------------------------------------------------------------------------------
29void wait_op( global_data_t * mutex a, global_data_t * mutex b, unsigned i ) {
30 wait( &cond );
31 a->ran = b->ran = true;
32
33 yield( ((unsigned)rand48()) % 10 );
34
35 if(a->state != SIGNAL || b->state != SIGNAL) {
36 sout | "ERROR Barging detected" | a->state | b->state | endl;
37 abort();
38 }
39
40 a->state = b->state = WAITED;
41
42 yield( ((unsigned)rand48()) % 10 );
43}
44
45thread Waiter {};
46void main( Waiter* this ) {
47 for( int i = 0; i < N; i++ ) {
48 wait_op( &globalA, &globalB, i );
49 }
50}
51
52//------------------------------------------------------------------------------
53void signal_op( global_data_t * mutex a, global_data_t * mutex b ) {
54 yield( ((unsigned)rand48()) % 10 );
55
56 a->ran = b->ran = false;
57 a->state = b->state = SIGNAL;
58
59 signal_block( &cond );
60
61 yield( ((unsigned)rand48()) % 10 );
62
63 assert(a->ran == b->ran);
64 if(a->ran)
65 {
66 if(a->state != WAITED || b->state != WAITED) {
67 sout | "ERROR Barging detected" | a->state | b->state | endl;
68 abort();
69 }
70 }
71
72}
73
74thread Signaller {};
75void main( Signaller* this ) {
76 while( !done ) {
77 signal_op( &globalA, &globalB );
78 }
79}
80
81//------------------------------------------------------------------------------
82void barge_op( global_data_t * mutex a ) {
83 a->state = BARGE;
84}
85
86thread Barger {};
87void main( Barger* this ) {
88 for( unsigned i = 0; !done; i++ ) {
89 //Choose some monitor to barge into with some irregular pattern
90 bool choose_a = (i % 13) > (i % 17);
91 barge_op( choose_a ? &globalA : &globalB );
92 }
93}
94
95//------------------------------------------------------------------------------
96
97int main(int argc, char* argv[]) {
98 rand48seed(0);
99 done = false;
100 processor p;
101 {
102 Signaller s[4];
103 Barger b[13];
104 sout | "Starting waiters" | endl;
105 {
106 Waiter w[3];
107 }
108 sout | "Waiters done" | endl;
109 done = true;
110 }
[736fe25]111}
Note: See TracBrowser for help on using the repository browser.