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