source: src/tests/concurrent/signal/wait.c @ ca37445

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 ca37445 was 8ad6533, checked in by Peter A. Buhr <pabuhr@…>, 6 years ago

remove cfatime.h, move itimerval constructor to "time", update concurrent examples to use Duration

  • Property mode set to 100644
File size: 3.2 KB
Line 
1//---------------------------------------------------------
2// Multi wait test
3// Ensures that no deadlock from waiting/signalling conditions
4//---------------------------------------------------------
5
6
7#include <fstream>
8#include <kernel>
9#include <monitor>
10#include <stdlib>
11#include <thread>
12
13#include <time.h>
14
15#ifdef LONG_TEST
16static const unsigned long N = 375_000ul;
17#else
18static const unsigned long N = 2_500ul;
19#endif
20
21#ifndef PREEMPTION_RATE
22#define PREEMPTION_RATE 10`ms
23#endif
24
25Duration default_preemption() {
26        return PREEMPTION_RATE;
27}
28
29monitor global_t {};
30
31global_t globalA;
32global_t globalB;
33global_t globalC;
34
35condition condAB, condAC, condBC, condABC;
36
37thread Signaler {};
38thread WaiterAB {};
39thread WaiterAC {};
40thread WaiterBC {};
41thread WaiterABC{};
42
43volatile int waiter_left;
44
45//----------------------------------------------------------------------------------------------------
46// Tools
47void signal( condition & cond, global_t & mutex a, global_t & mutex b ) {
48        signal( cond );
49}
50
51void signal( condition & cond, global_t & mutex a, global_t & mutex b, global_t & mutex c ) {
52        signal( cond );
53}
54
55void wait( condition & cond, global_t & mutex a, global_t & mutex b ) {
56        wait( cond );
57}
58
59void wait( condition & cond, global_t & mutex a, global_t & mutex b, global_t & mutex c ) {
60        wait( cond );
61}
62
63//----------------------------------------------------------------------------------------------------
64// Signaler
65void main( Signaler & this ) {
66
67        while( waiter_left != 0 ) {
68                unsigned action = random( 4 );
69                switch( action ) {
70                        case 0:
71                                signal( condABC, globalA, globalB, globalC );
72                                break;
73                        case 1:
74                                signal( condAB , globalA, globalB );
75                                break;
76                        case 2:
77                                signal( condBC , globalB, globalC );
78                                break;
79                        case 3:
80                                signal( condAC , globalA, globalC );
81                                break;
82                        default:
83                                sout | "Something went wrong" | endl;
84                                abort();
85                }
86                yield();
87        }
88}
89
90//----------------------------------------------------------------------------------------------------
91// Waiter ABC
92void main( WaiterABC & this ) {
93        for( int i = 0; i < N; i++ ) {
94                wait( condABC, globalA, globalB, globalC );
95        }
96
97        __sync_fetch_and_sub_4( &waiter_left, 1);
98}
99
100//----------------------------------------------------------------------------------------------------
101// Waiter AB
102void main( WaiterAB & this ) {
103        for( int i = 0; i < N; i++ ) {
104                wait( condAB , globalA, globalB );
105        }
106
107        __sync_fetch_and_sub_4( &waiter_left, 1);
108}
109
110//----------------------------------------------------------------------------------------------------
111// Waiter AC
112void main( WaiterAC & this ) {
113        for( int i = 0; i < N; i++ ) {
114                wait( condAC , globalA, globalC );
115        }
116
117        __sync_fetch_and_sub_4( &waiter_left, 1);
118}
119
120//----------------------------------------------------------------------------------------------------
121// Waiter BC
122void main( WaiterBC & this ) {
123        for( int i = 0; i < N; i++ ) {
124                wait( condBC , globalB, globalC );
125        }
126
127        __sync_fetch_and_sub_4( &waiter_left, 1);
128}
129
130//----------------------------------------------------------------------------------------------------
131// Main
132int main(int argc, char* argv[]) {
133        srandom( time( NULL ) );
134        waiter_left = 4;
135        processor p[2];
136        sout | "Starting" | endl;
137        {
138                Signaler  e;
139                {
140                        WaiterABC a;
141                        WaiterAB  b;
142                        WaiterBC  c;
143                        WaiterAC  d;
144                }
145        }
146        sout | "Done" | endl;
147}
Note: See TracBrowser for help on using the repository browser.