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
Line 
1#include <fstream>
2#include <kernel>
3#include <monitor>
4#include <stdlib>
5#include <thread>
6
7#ifndef N
8#define N 10_000
9#endif
10
11monitor global_t {};
12
13global_t globalA;
14global_t globalB;
15global_t globalC;
16
17condition condAB, condAC, condBC, condABC;
18
19thread Signaler {};
20thread WaiterAB {};
21thread WaiterAC {};
22thread WaiterBC {};
23thread WaiterABC{};
24
25volatile int waiter_left;
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 ) {
48
49        while( waiter_left != 0 ) {
50                unsigned action = (unsigned)rand48() % 4;
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                }
68                yield();
69        }       
70}
71
72//----------------------------------------------------------------------------------------------------
73// Waiter ABC
74void main( WaiterABC* this ) {
75        for( int i = 0; i < N; i++ ) {
76                wait( &condABC, &globalA, &globalB, &globalC );
77        }
78
79        __sync_fetch_and_sub_4( &waiter_left, 1);
80}
81
82//----------------------------------------------------------------------------------------------------
83// Waiter AB
84void main( WaiterAB* this ) {
85        for( int i = 0; i < N; i++ ) {
86                wait( &condAB , &globalA, &globalB );
87        }
88
89        __sync_fetch_and_sub_4( &waiter_left, 1);
90}
91
92//----------------------------------------------------------------------------------------------------
93// Waiter AC
94void main( WaiterAC* this ) {
95        for( int i = 0; i < N; i++ ) {
96                wait( &condAC , &globalA, &globalC );
97        }
98
99        __sync_fetch_and_sub_4( &waiter_left, 1);
100}
101
102//----------------------------------------------------------------------------------------------------
103// Waiter BC
104void main( WaiterBC* this ) {
105        for( int i = 0; i < N; i++ ) {
106                wait( &condBC , &globalB, &globalC );
107        }
108
109        __sync_fetch_and_sub_4( &waiter_left, 1);
110}
111
112//----------------------------------------------------------------------------------------------------
113// Main
114int main(int argc, char* argv[]) {
115        waiter_left = 4;
116        processor p;
117        sout | "Starting" | endl;
118        {
119                Signaler  e;
120                {
121                        WaiterABC a;
122                        WaiterAB  b;
123                        WaiterBC  c;
124                        WaiterAC  d;
125                }
126        }
127        sout | "Done" | endl;
128}
Note: See TracBrowser for help on using the repository browser.