source: src/tests/sched-int-disjoint.c @ 7c40a24

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 7c40a24 was 6b224a52, checked in by Thierry Delisle <tdelisle@…>, 7 years ago

Merge branch 'master' of plg.uwaterloo.ca:software/cfa/cfa-cc

  • Property mode set to 100644
File size: 2.3 KB
Line 
1#include <fstream>
2#include <kernel>
3#include <monitor>
4#include <thread>
5
6#include <time.h>
7
8static const unsigned long N = 10_000ul;
9
10#ifndef PREEMPTION_RATE
11#define PREEMPTION_RATE 10_000ul
12#endif
13
14unsigned int default_preemption() {
15        return PREEMPTION_RATE;
16}
17
18enum state_t { WAIT, SIGNAL, BARGE };
19
20monitor global_t {};
21global_t mut;
22
23monitor global_data_t;
24void ?{}( global_data_t & this );
25void ^?{} ( global_data_t & this );
26
27monitor global_data_t {
28        int counter;
29        state_t state;
30} data;
31
32condition cond;
33
34volatile bool all_done;
35
36void ?{}( global_data_t & this ) {
37        this.counter == 0;
38        this.state = BARGE;
39}
40
41void ^?{} ( global_data_t & this ) {}
42
43//------------------------------------------------------------------------------
44// Barging logic
45void barge( global_data_t & mutex d ) {
46        d.state = BARGE;
47}
48
49thread Barger {};
50
51void main( Barger & this ) {
52        while( !all_done ) {
53                barge( data );
54                yield();
55        }
56}
57
58//------------------------------------------------------------------------------
59// Waiting logic
60bool wait( global_t & mutex m, global_data_t & mutex d ) {
61        wait( &cond );
62        if( d.state != SIGNAL ) {
63                sout | "ERROR barging!" | endl;
64        }
65
66        d.counter++;
67
68        if( (d.counter % 1000) == 0 ) sout | d.counter | endl;
69
70        return d.counter < N;
71}
72
73thread Waiter {};
74
75void main( Waiter & this ) {
76        while( wait( mut, data ) ) { yield(); }
77}
78
79
80//------------------------------------------------------------------------------
81// Signalling logic
82void signal( condition * cond, global_t & mutex a, global_data_t & mutex b ) {
83        b.state = SIGNAL;
84        signal( cond );
85}
86
87void logic( global_t & mutex a ) {
88        signal( &cond, a, data );
89
90        yield( (unsigned)rand48() % 10 );
91
92        //This is technically a mutual exclusion violation but the mutex monitor protects us
93        bool running = data.counter < N && data.counter > 0;
94        if( data.state != SIGNAL && running ) {
95                sout | "ERROR Eager signal" | data.state | endl;
96        }
97}
98
99thread Signaller {};
100
101void main( Signaller & this ) {
102        while( !all_done ) {
103                logic( mut );
104                yield();
105        }
106}
107
108//------------------------------------------------------------------------------
109// Main loop
110int main(int argc, char* argv[]) {
111        rand48seed( time( NULL ) );
112        all_done = false;
113        processor p;
114        {
115                Signaller s;
116                Barger b[17];
117                {
118                        Waiter w[4];
119                }
120                sout | "All waiter done" | endl;
121                all_done = true;
122        }
123}
Note: See TracBrowser for help on using the repository browser.