source: src/tests/sched-int-barge.c @ 201aeb9

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 201aeb9 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
Line 
1#include <fstream>
2#include <kernel>
3#include <monitor>
4#include <stdlib>
5#include <thread>
6
7static const unsigned long N = 50_000ul;
8
9#ifndef PREEMPTION_RATE
10#define PREEMPTION_RATE 10_000ul
11#endif
12
13unsigned int default_preemption() {
14        return 0;
15}
16enum state_t { WAIT, SIGNAL, BARGE };
17
18monitor global_t {};
19
20monitor global_data_t {
21        volatile bool done;
22        int counter;
23        state_t state;
24
25        unsigned short do_signal;
26        unsigned short do_wait2;
27        unsigned short do_wait1;
28};
29
30void ?{} ( 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;
38}
39
40void ^?{} ( global_data_t & this ) {}
41
42global_t globalA;
43global_t globalB;
44global_data_t globalC;
45
46condition cond;
47
48thread Threads {};
49
50bool logicC( global_t & mutex a, global_t & mutex b, global_data_t & mutex c ) {
51        c.counter++;
52
53        if( (c.counter % 1000) == 0 ) sout | c.counter | endl;
54
55        int action = c.counter % 10;
56
57        if( action == 0 ) {
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);
61
62                if(c.do_wait1 == c.do_wait2) sout | "Same" | endl;
63        }
64
65        if( action == c.do_wait1 || action == c.do_wait2 ) {
66                c.state = WAIT;
67                wait( &cond );
68
69                if(c.state != SIGNAL) {
70                        sout | "ERROR Barging detected" | c.counter | endl;
71                        abort();
72                }
73        }
74        else if( action == c.do_signal ) {
75                c.state = SIGNAL;
76
77                signal( &cond );
78                signal( &cond );
79        }
80        else {
81                c.state = BARGE;
82        }
83
84        if( c.counter >= N ) c.done = true;
85        return !c.done;
86}
87
88bool logicB( global_t & mutex a, global_t & mutex b ) {
89        return logicC(a, b, globalC);
90}
91
92bool logicA( global_t & mutex a ) {
93        return logicB(a, globalB);
94}
95
96void main( Threads & this ) {
97        while( logicA(globalA) ) { yield(); };
98}
99
100static thread_desc * volatile the_threads;
101
102int main(int argc, char* argv[]) {
103        rand48seed(0);
104        processor p;
105        {
106                Threads t[17];
107                the_threads = (thread_desc*)t;
108        }
109}
Note: See TracBrowser for help on using the repository browser.