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

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 5527759 was 0f56058, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

divide "time" into type and functions

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