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

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

Added the option to make longrun tests run until failure

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