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

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

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

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