Changeset 19801aa


Ignore:
Timestamp:
May 9, 2017, 4:24:55 PM (7 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
66ef082
Parents:
f408e1a
Message:

Waiting test for monitor is more robust

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/tests/sched-int-wait.c

    rf408e1a r19801aa  
    44#include <stdlib>
    55#include <thread>
     6
     7static const int N = 10_000;
    68
    79monitor global_t {};
     
    1315condition condAB, condAC, condBC, condABC;
    1416
    15 thread Signaler {
    16         int signals[4];
    17 };
    18 
    19 void ?{}( Signaler * this ){
    20         this->signals[0] = 0;
    21         this->signals[1] = 0;
    22         this->signals[2] = 0;
    23         this->signals[3] = 0;
    24 }
    25 
     17thread Signaler {};
    2618thread WaiterAB {};
    2719thread WaiterAC {};
     
    2921thread WaiterABC{};
    3022
    31 volatile bool done;
     23volatile int waiter_left;
    3224
    3325//----------------------------------------------------------------------------------------------------
     
    5345void main( Signaler* this ) {
    5446
    55         while( true ) {
    56                 int action = (unsigned)rand48() % 4;
    57                 bool finished = true;
    58 
    59                 for(int i = 0; i < 4; i++) {
    60                         if( this->signals[action] < 10_000 ) {
    61                                 finished = false;
    62                                 break;
    63                         }
    64                         else {
    65                                 action = (action + 1) % 4;
    66                         }
    67                 }
    68 
    69                 this->signals[action]++;
    70                 if( finished ) break;
    71 
    72                 //sout | action | this->signals[0] | this->signals[1] | this->signals[2] | this->signals[3] | endl;
    73 
     47        while( waiter_left != 0 ) {
     48                unsigned action = (unsigned)rand48() % 4;
    7449                switch( action ) {
    7550                        case 0:
     
    8964                                abort();
    9065                }
     66                yield();
    9167        }       
    9268}
     
    9571// Waiter ABC
    9672void main( WaiterABC* this ) {
    97         while( !done ) {
     73        for( int i = 0; i < N; i++ ) {
    9874                wait( &condABC, &globalA, &globalB, &globalC );
    9975        }
     76
     77        __sync_fetch_and_sub_4( &waiter_left, 1);
    10078}
    10179
     
    10381// Waiter AB
    10482void main( WaiterAB* this ) {
    105         while( !done ) {
     83        for( int i = 0; i < N; i++ ) {
    10684                wait( &condAB , &globalA, &globalB );
    10785        }
     86
     87        __sync_fetch_and_sub_4( &waiter_left, 1);
    10888}
    10989
     
    11191// Waiter AC
    11292void main( WaiterAC* this ) {
    113         while( !done ) {
     93        for( int i = 0; i < N; i++ ) {
    11494                wait( &condAC , &globalA, &globalC );
    11595        }
     96
     97        __sync_fetch_and_sub_4( &waiter_left, 1);
    11698}
    11799
     
    119101// Waiter BC
    120102void main( WaiterBC* this ) {
    121         while( !done ) {
     103        for( int i = 0; i < N; i++ ) {
    122104                wait( &condBC , &globalB, &globalC );
    123105        }
     106
     107        __sync_fetch_and_sub_4( &waiter_left, 1);
    124108}
    125109
     
    127111// Main
    128112int main(int argc, char* argv[]) {
    129         done = false;
     113        waiter_left = 4;
    130114        processor p;
    131115        {
    132                 WaiterABC a;
    133                 WaiterAB  b;
    134                 WaiterBC  c;
    135                 WaiterAC  d;
     116                Signaler  e;
    136117                {
    137                         Signaler  e;
     118                        WaiterABC a;
     119                        WaiterAB  b;
     120                        WaiterBC  c;
     121                        WaiterAC  d;
    138122                }
    139                 done = true;
    140                 signal( &condABC, &globalA, &globalB, &globalC );
    141                 signal( &condAB , &globalA, &globalB );
    142                 signal( &condBC , &globalB, &globalC );
    143                 signal( &condAC , &globalA, &globalC );
    144123        }
    145124}
Note: See TracChangeset for help on using the changeset viewer.