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

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 33218c6 was cd99ef1, checked in by Thierry Delisle <tdelisle@…>, 8 years ago

Clean-up longrunning tests to be more consistent

  • 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
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 ) {
[4845ae2]54
[19801aa]55 while( waiter_left != 0 ) {
56 unsigned action = (unsigned)rand48() % 4;
[4845ae2]57 switch( action ) {
[f3c1737]58 case 0:
[4845ae2]59 signal( &condABC, &globalA, &globalB, &globalC );
60 break;
[f3c1737]61 case 1:
[4845ae2]62 signal( &condAB , &globalA, &globalB );
63 break;
[f3c1737]64 case 2:
[4845ae2]65 signal( &condBC , &globalB, &globalC );
66 break;
[f3c1737]67 case 3:
[4845ae2]68 signal( &condAC , &globalA, &globalC );
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
80void main( WaiterABC* this ) {
[19801aa]81 for( int i = 0; i < N; i++ ) {
[4845ae2]82 wait( &condABC, &globalA, &globalB, &globalC );
83 }
[19801aa]84
85 __sync_fetch_and_sub_4( &waiter_left, 1);
[2e5ad9f]86}
87
88//----------------------------------------------------------------------------------------------------
89// Waiter AB
90void main( WaiterAB* this ) {
[19801aa]91 for( int i = 0; i < N; i++ ) {
[4845ae2]92 wait( &condAB , &globalA, &globalB );
93 }
[19801aa]94
95 __sync_fetch_and_sub_4( &waiter_left, 1);
[2e5ad9f]96}
97
98//----------------------------------------------------------------------------------------------------
99// Waiter AC
100void main( WaiterAC* this ) {
[19801aa]101 for( int i = 0; i < N; i++ ) {
[4845ae2]102 wait( &condAC , &globalA, &globalC );
103 }
[19801aa]104
105 __sync_fetch_and_sub_4( &waiter_left, 1);
[2e5ad9f]106}
107
108//----------------------------------------------------------------------------------------------------
109// Waiter BC
110void main( WaiterBC* this ) {
[19801aa]111 for( int i = 0; i < N; i++ ) {
[4845ae2]112 wait( &condBC , &globalB, &globalC );
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;
[2e5ad9f]134}
Note: See TracBrowser for help on using the repository browser.