source: src/tests/concurrent/signal/disjoint.c @ 4c11fce

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 4c11fce was 54aba8d, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

change name of random_seed to srandom, and make all random calls through rand48

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