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

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

Merge branch 'master' of plg.uwaterloo.ca:software/cfa/cfa-cc

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