source: src/tests/sched-int-barge.c @ 83a071f9

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 83a071f9 was 83a071f9, checked in by Rob Schluntz <rschlunt@…>, 7 years ago

Fix concurrency library, tests, and keywords for references

  • 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
7enum state_t { WAIT, SIGNAL, BARGE };
8
9monitor global_t {};
10
11monitor global_data_t {
12        bool done;
13        int counter;
14        state_t state;
15
16        unsigned short do_signal;
17        unsigned short do_wait2;
18        unsigned short do_wait1;
19};
20
21void ?{} ( global_data_t & this ) {
22        this.done = false;
23        this.counter = 0;
24        this.state = BARGE;
25
26        this.do_signal = 6;
27        this.do_wait1  = 1;
28        this.do_wait2  = 3;
29}
30
31void ^?{} ( global_data_t & this ) {}
32
33global_t globalA;
34global_t globalB;
35global_data_t globalC;
36
37condition cond;
38
39thread Threads {};
40
41bool logicC( global_t & mutex a, global_t & mutex b, global_data_t & mutex c ) {
42        c.counter++;
43
44        if( (c.counter % 1000) == 0 ) sout | c.counter | endl;
45
46        int action = c.counter % 10;
47
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);
52
53                // if(c.do_wait1 == c.do_wait2) sout | "Same" | endl;
54        }
55
56        if( action == c.do_wait1 || action == c.do_wait2 ) {
57                c.state = WAIT;
58                wait( &cond );
59
60                if(c.state != SIGNAL) {
61                        sout | "ERROR Barging detected" | c.counter | endl;
62                        abort();
63                }
64        }
65        else if( action == c.do_signal ) {
66                c.state = SIGNAL;
67
68                signal( &cond );
69                signal( &cond );
70        }
71        else {
72                c.state = BARGE;
73        }
74
75        if( c.counter >= 100_000 ) c.done = true;
76        return !c.done;
77}
78
79bool logicB( global_t & mutex a, global_t & mutex b ) {
80        return logicC(a, b, globalC);
81}
82
83bool logicA( global_t & mutex a ) {
84        return logicB(a, globalB);
85}
86
87void main( Threads & this ) {
88        while( logicA(globalA) ) { yield(); };
89}
90
91int main(int argc, char* argv[]) {
92        rand48seed(0);
93        processor p;
94        {
95                Threads t[17];
96        }
97}
Note: See TracBrowser for help on using the repository browser.