source: src/tests/concurrent/signal/wait.c@ 8eb2018

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 with_gc
Last change on this file since 8eb2018 was 70969f8, checked in by Thierry Delisle <tdelisle@…>, 8 years ago

Updated longrun tests have a more consistent duration

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