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

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

Merge branch 'master' into references

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