source: tests/concurrent/signal/disjoint.cfa @ ab8a023

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since ab8a023 was ab8a023, checked in by Thierry Delisle <tdelisle@…>, 4 years ago

Added more thread names to disjoint

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