source: src/tests/concurrent/signal/disjoint.c @ ce28c7b

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumwith_gc
Last change on this file since ce28c7b was 70969f8, checked in by Thierry Delisle <tdelisle@…>, 7 years ago

Updated longrun tests have a more consistent duration

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