source: src/tests/concurrent/signal/wait.c@ 84276ba

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 84276ba was 54aba8d, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

change name of random_seed to srandom, and make all random calls through rand48

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