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

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 f1bcd004 was 9236060, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

Merge branch 'master' into references

  • 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
7static const unsigned long N = 10_000ul;
8
9#ifndef PREEMPTION_RATE
10#define PREEMPTION_RATE 10_000ul
11#endif
12
13unsigned int default_preemption() {
14 return PREEMPTION_RATE;
15}
16
17monitor global_t {};
18
19global_t globalA;
20global_t globalB;
21global_t globalC;
22
23condition condAB, condAC, condBC, condABC;
24
25thread Signaler {};
26thread WaiterAB {};
27thread WaiterAC {};
28thread WaiterBC {};
29thread WaiterABC{};
30
31volatile int waiter_left;
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( waiter_left != 0 ) {
56 unsigned action = (unsigned)rand48() % 4;
57 switch( action ) {
58 case 0:
59 signal( &condABC, globalA, globalB, globalC );
60 break;
61 case 1:
62 signal( &condAB , globalA, globalB );
63 break;
64 case 2:
65 signal( &condBC , globalB, globalC );
66 break;
67 case 3:
68 signal( &condAC , globalA, globalC );
69 break;
70 default:
71 sout | "Something went wrong" | endl;
72 abort();
73 }
74 yield();
75 }
76}
77
78//----------------------------------------------------------------------------------------------------
79// Waiter ABC
80void main( WaiterABC & this ) {
81 for( int i = 0; i < N; i++ ) {
82 wait( &condABC, globalA, globalB, globalC );
83 }
84
85 __sync_fetch_and_sub_4( &waiter_left, 1);
86}
87
88//----------------------------------------------------------------------------------------------------
89// Waiter AB
90void main( WaiterAB & this ) {
91 for( int i = 0; i < N; i++ ) {
92 wait( &condAB , globalA, globalB );
93 }
94
95 __sync_fetch_and_sub_4( &waiter_left, 1);
96}
97
98//----------------------------------------------------------------------------------------------------
99// Waiter AC
100void main( WaiterAC & this ) {
101 for( int i = 0; i < N; i++ ) {
102 wait( &condAC , globalA, globalC );
103 }
104
105 __sync_fetch_and_sub_4( &waiter_left, 1);
106}
107
108//----------------------------------------------------------------------------------------------------
109// Waiter BC
110void main( WaiterBC & this ) {
111 for( int i = 0; i < N; i++ ) {
112 wait( &condBC , globalB, globalC );
113 }
114
115 __sync_fetch_and_sub_4( &waiter_left, 1);
116}
117
118//----------------------------------------------------------------------------------------------------
119// Main
120int main(int argc, char* argv[]) {
121 waiter_left = 4;
122 processor p[2];
123 sout | "Starting" | endl;
124 {
125 Signaler e;
126 {
127 WaiterABC a;
128 WaiterAB b;
129 WaiterBC c;
130 WaiterAC d;
131 }
132 }
133 sout | "Done" | endl;
134}
Note: See TracBrowser for help on using the repository browser.