source: src/tests/concurrent/signal/wait.c @ 7bdcac1

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