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

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 3eab0ef6 was 303406a, checked in by Thierry Delisle <tdelisle@…>, 8 years ago

Fixed randomness in internal scheduling tests

  • 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
37void signal( condition * cond, global_t * mutex a, global_t * mutex b ) {
38 signal( cond );
39}
40
41void signal( condition * cond, global_t * mutex a, global_t * mutex b, global_t * mutex c ) {
42 signal( cond );
43}
44
45void wait( condition * cond, global_t * mutex a, global_t * mutex b ) {
46 wait( cond );
47}
48
49void wait( condition * cond, global_t * mutex a, global_t * mutex b, global_t * mutex c ) {
50 wait( cond );
51}
52
53//----------------------------------------------------------------------------------------------------
54// Signaler
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:
[4845ae2]61 signal( &condABC, &globalA, &globalB, &globalC );
62 break;
[f3c1737]63 case 1:
[4845ae2]64 signal( &condAB , &globalA, &globalB );
65 break;
[f3c1737]66 case 2:
[4845ae2]67 signal( &condBC , &globalB, &globalC );
68 break;
[f3c1737]69 case 3:
[4845ae2]70 signal( &condAC , &globalA, &globalC );
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
82void main( WaiterABC* this ) {
[19801aa]83 for( int i = 0; i < N; i++ ) {
[4845ae2]84 wait( &condABC, &globalA, &globalB, &globalC );
85 }
[19801aa]86
87 __sync_fetch_and_sub_4( &waiter_left, 1);
[2e5ad9f]88}
89
90//----------------------------------------------------------------------------------------------------
91// Waiter AB
92void main( WaiterAB* this ) {
[19801aa]93 for( int i = 0; i < N; i++ ) {
[4845ae2]94 wait( &condAB , &globalA, &globalB );
95 }
[19801aa]96
97 __sync_fetch_and_sub_4( &waiter_left, 1);
[2e5ad9f]98}
99
100//----------------------------------------------------------------------------------------------------
101// Waiter AC
102void main( WaiterAC* this ) {
[19801aa]103 for( int i = 0; i < N; i++ ) {
[4845ae2]104 wait( &condAC , &globalA, &globalC );
105 }
[19801aa]106
107 __sync_fetch_and_sub_4( &waiter_left, 1);
[2e5ad9f]108}
109
110//----------------------------------------------------------------------------------------------------
111// Waiter BC
112void main( WaiterBC* this ) {
[19801aa]113 for( int i = 0; i < N; i++ ) {
[4845ae2]114 wait( &condBC , &globalB, &globalC );
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;
[2e5ad9f]137}
Note: See TracBrowser for help on using the repository browser.