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

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