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

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since 7c0ef42 was 9737ffe, checked in by Thierry Delisle <tdelisle@…>, 8 years ago

Renamed internal scheduling tests to be more evocative

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