source: src/tests/sched-int-wait.c @ 11dbfe1

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

Added monitor tests to preempt longrun tests

  • Property mode set to 100644
File size: 2.8 KB
RevLine 
[2e5ad9f]1#include <fstream>
2#include <kernel>
3#include <monitor>
[4845ae2]4#include <stdlib>
[2e5ad9f]5#include <thread>
6
[e1c1829]7#ifndef N
8#define N 10_000
9#endif
[19801aa]10
[2e5ad9f]11monitor global_t {};
12
13global_t globalA;
14global_t globalB;
15global_t globalC;
16
17condition condAB, condAC, condBC, condABC;
18
[19801aa]19thread Signaler {};
[2e5ad9f]20thread WaiterAB {};
21thread WaiterAC {};
22thread WaiterBC {};
23thread WaiterABC{};
24
[19801aa]25volatile int waiter_left;
[2e5ad9f]26
27//----------------------------------------------------------------------------------------------------
28// Tools
29void signal( condition * cond, global_t * mutex a, global_t * mutex b ) {
30        signal( cond );
31}
32
33void signal( condition * cond, global_t * mutex a, global_t * mutex b, global_t * mutex c ) {
34        signal( cond );
35}
36
37void wait( condition * cond, global_t * mutex a, global_t * mutex b ) {
38        wait( cond );
39}
40
41void wait( condition * cond, global_t * mutex a, global_t * mutex b, global_t * mutex c ) {
42        wait( cond );
43}
44
45//----------------------------------------------------------------------------------------------------
46// Signaler
47void main( Signaler* this ) {
[4845ae2]48
[19801aa]49        while( waiter_left != 0 ) {
50                unsigned action = (unsigned)rand48() % 4;
[4845ae2]51                switch( action ) {
52                        case 0: 
53                                signal( &condABC, &globalA, &globalB, &globalC );
54                                break;
55                        case 1: 
56                                signal( &condAB , &globalA, &globalB );
57                                break;
58                        case 2: 
59                                signal( &condBC , &globalB, &globalC );
60                                break;
61                        case 3: 
62                                signal( &condAC , &globalA, &globalC );
63                                break;
64                        default:
65                                sout | "Something went wrong" | endl;
66                                abort();
67                }
[19801aa]68                yield();
[4845ae2]69        }       
[2e5ad9f]70}
71
72//----------------------------------------------------------------------------------------------------
73// Waiter ABC
74void main( WaiterABC* this ) {
[19801aa]75        for( int i = 0; i < N; i++ ) {
[4845ae2]76                wait( &condABC, &globalA, &globalB, &globalC );
77        }
[19801aa]78
79        __sync_fetch_and_sub_4( &waiter_left, 1);
[2e5ad9f]80}
81
82//----------------------------------------------------------------------------------------------------
83// Waiter AB
84void main( WaiterAB* this ) {
[19801aa]85        for( int i = 0; i < N; i++ ) {
[4845ae2]86                wait( &condAB , &globalA, &globalB );
87        }
[19801aa]88
89        __sync_fetch_and_sub_4( &waiter_left, 1);
[2e5ad9f]90}
91
92//----------------------------------------------------------------------------------------------------
93// Waiter AC
94void main( WaiterAC* this ) {
[19801aa]95        for( int i = 0; i < N; i++ ) {
[4845ae2]96                wait( &condAC , &globalA, &globalC );
97        }
[19801aa]98
99        __sync_fetch_and_sub_4( &waiter_left, 1);
[2e5ad9f]100}
101
102//----------------------------------------------------------------------------------------------------
103// Waiter BC
104void main( WaiterBC* this ) {
[19801aa]105        for( int i = 0; i < N; i++ ) {
[4845ae2]106                wait( &condBC , &globalB, &globalC );
107        }
[19801aa]108
109        __sync_fetch_and_sub_4( &waiter_left, 1);
[2e5ad9f]110}
111
112//----------------------------------------------------------------------------------------------------
113// Main
114int main(int argc, char* argv[]) {
[19801aa]115        waiter_left = 4;
[2e5ad9f]116        processor p;
[43123e0]117        sout | "Starting" | endl;
[2e5ad9f]118        {
[19801aa]119                Signaler  e;
[4845ae2]120                {
[19801aa]121                        WaiterABC a;
122                        WaiterAB  b;
123                        WaiterBC  c;
124                        WaiterAC  d;
[4845ae2]125                }
[2e5ad9f]126        }
[43123e0]127        sout | "Done" | endl;
[2e5ad9f]128}
Note: See TracBrowser for help on using the repository browser.