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

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 b0e5593 was b0e5593, checked in by Thierry Delisle <tdelisle@…>, 6 years ago

Moved expected files for concurrent:

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