Changeset 83a071f9 for src/tests


Ignore:
Timestamp:
Aug 11, 2017, 10:10:26 AM (8 years ago)
Author:
Rob Schluntz <rschlunt@…>
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:
fd344aa
Parents:
8499c707
Message:

Fix concurrency library, tests, and keywords for references

Location:
src/tests
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • src/tests/coroutine.c

    r8499c707 r83a071f9  
    2525}
    2626
    27 void main( Fibonacci * this ) {
     27void main( Fibonacci & this ) {
    2828        int fn1, fn2;                                   // retained between resumes
    29         this->fn = 0;                                   // case 0
    30         fn1 = this->fn;
     29        this.fn = 0;                                    // case 0
     30        fn1 = this.fn;
    3131        suspend();                                              // return to last resume
    3232
    33         this->fn = 1;                                   // case 1
     33        this.fn = 1;                                    // case 1
    3434        fn2 = fn1;
    35         fn1 = this->fn;
     35        fn1 = this.fn;
    3636        suspend();                                              // return to last resume
    3737
    3838        for ( ;; ) {                                    // general case
    39                 this->fn = fn1 + fn2;
     39                this.fn = fn1 + fn2;
    4040                fn2 = fn1;
    41                 fn1 = this->fn;
     41                fn1 = this.fn;
    4242                suspend();                                      // return to last resume
    4343        } // for
    4444}
    4545
    46 int next( Fibonacci * this ) {
     46int next( Fibonacci & this ) {
    4747        resume( this );                                 // transfer to last suspend
    48         return this->fn;
     48        return this.fn;
    4949}
    5050
     
    5252        Fibonacci f1, f2;
    5353        for ( int i = 1; i <= 10; i += 1 ) {
    54                 sout | next( &f1 ) | ' ' | next( &f2 ) | endl;
     54                sout | next( f1 ) | ' ' | next( f2 ) | endl;
    5555        } // for
    5656}
  • src/tests/monitor.c

    r8499c707 r83a071f9  
    1414static global_t global;
    1515
    16 void increment3( global_t * mutex this ) {
    17         this->value += 1;
     16void increment3( global_t & mutex this ) {
     17        this.value += 1;
    1818}
    1919
    20 void increment2( global_t * mutex this ) {
     20void increment2( global_t & mutex this ) {
    2121        increment3( this );
    2222}
    2323
    24 void increment( global_t * mutex this ) {
     24void increment( global_t & mutex this ) {
    2525        increment2( this );
    2626}
     
    2828thread MyThread {};
    2929
    30 void main( MyThread* this ) {
     30void main( MyThread & this ) {
    3131        for(int i = 0; i < 1_000_000; i++) {
    32                 increment( &global );
     32                increment( global );
    3333        }
    3434}
  • src/tests/multi-monitor.c

    r8499c707 r83a071f9  
    1010static monitor_t m1, m2, m3;
    1111
    12 void increment( monitor_t * mutex p1, monitor_t * mutex p2, int * value ) {
    13         *value += 1;
     12void increment( monitor_t & mutex p1, monitor_t & mutex p2, int & value ) {
     13        value += 1;
    1414}
    1515
    16 struct MyThread {
    17         thread_desc __thrd;
     16thread MyThread {
    1817        int target;
    1918};
    20 
    21 DECL_THREAD(MyThread);
    2219
    2320void ?{}( MyThread & this, int target ) {
     
    2724void ^?{}( MyThread & mutex this ) {}
    2825
    29 void main( MyThread* this ) {
     26void main( MyThread & this ) {
    3027        for(int i = 0; i < 1000000; i++) {
    31                 choose(this->target) {
    32                         case 0: increment( &m1, &m2, &global12 );
    33                         case 1: increment( &m2, &m3, &global23 );
    34                         case 2: increment( &m1, &m3, &global13 );
     28                choose(this.target) {
     29                        case 0: increment( m1, m2, global12 );
     30                        case 1: increment( m2, m3, global23 );
     31                        case 2: increment( m1, m3, global13 );
    3532                }
    3633        }
     34}
     35
     36forall(dtype T | sized(T) | { void ^?{}(T & mutex); })
     37void delete_mutex(T * x) {
     38        ^(*x){};
     39        free(x);
    3740}
    3841
     
    4043        processor p;
    4144        {
    42                 scoped(MyThread) * f[6];
     45                MyThread * f[6];
    4346                for(int i = 0; i < 6; i++) {
    44                         f[i] = (*(scoped(MyThread) *)malloc()){ i % 3 };
     47                        f[i] = new(i % 3);
    4548                }
    4649
    4750                for(int i = 0; i < 6; i++) {
    48                         delete( f[i] );
     51                        delete_mutex( f[i] );
    4952                }
    5053        }
  • src/tests/preempt.c

    r8499c707 r83a071f9  
    2020}
    2121
    22 void main(worker_t * this) {
     22void main(worker_t & this) {
    2323        while(counter < 1000) {
    24                 if( (counter % 7) == this->value ) {
     24                if( (counter % 7) == this.value ) {
    2525                        int next = __atomic_add_fetch_4(&counter, 1, __ATOMIC_SEQ_CST);
    2626                        if( (next % 100) == 0 ) printf("%d\n", next);
  • src/tests/sched-int-barge.c

    r8499c707 r83a071f9  
    3939thread Threads {};
    4040
    41 bool logicC( global_t * mutex a, global_t * mutex b, global_data_t * mutex c ) {
    42         c->counter++;
     41bool logicC( global_t & mutex a, global_t & mutex b, global_data_t & mutex c ) {
     42        c.counter++;
    4343
    44         if( (c->counter % 1000) == 0 ) sout | c->counter | endl;
     44        if( (c.counter % 1000) == 0 ) sout | c.counter | endl;
    4545
    46         int action = c->counter % 10;
     46        int action = c.counter % 10;
    4747
    4848        if( action == 0 ) {
    49                 c->do_signal = max( ((unsigned)rand48()) % 10, 1);
    50                 c->do_wait1 = ((unsigned)rand48()) % (c->do_signal);
    51                 c->do_wait2 = ((unsigned)rand48()) % (c->do_signal);
     49                c.do_signal = max( ((unsigned)rand48()) % 10, 1);
     50                c.do_wait1 = ((unsigned)rand48()) % (c.do_signal);
     51                c.do_wait2 = ((unsigned)rand48()) % (c.do_signal);
    5252
    53                 // if(c->do_wait1 == c->do_wait2) sout | "Same" | endl;
     53                // if(c.do_wait1 == c.do_wait2) sout | "Same" | endl;
    5454        }
    5555
    56         if( action == c->do_wait1 || action == c->do_wait2 ) {
    57                 c->state = WAIT;
     56        if( action == c.do_wait1 || action == c.do_wait2 ) {
     57                c.state = WAIT;
    5858                wait( &cond );
    5959
    60                 if(c->state != SIGNAL) {
    61                         sout | "ERROR Barging detected" | c->counter | endl;
     60                if(c.state != SIGNAL) {
     61                        sout | "ERROR Barging detected" | c.counter | endl;
    6262                        abort();
    6363                }
    6464        }
    65         else if( action == c->do_signal ) {
    66                 c->state = SIGNAL;
     65        else if( action == c.do_signal ) {
     66                c.state = SIGNAL;
    6767
    6868                signal( &cond );
     
    7070        }
    7171        else {
    72                 c->state = BARGE;
     72                c.state = BARGE;
    7373        }
    7474
    75         if( c->counter >= 100_000 ) c->done = true;
    76         return !c->done;
     75        if( c.counter >= 100_000 ) c.done = true;
     76        return !c.done;
    7777}
    7878
    79 bool logicB( global_t * mutex a, global_t * mutex b ) {
    80         return logicC(a, b, &globalC);
     79bool logicB( global_t & mutex a, global_t & mutex b ) {
     80        return logicC(a, b, globalC);
    8181}
    8282
    83 bool logicA( global_t * mutex a ) {
    84         return logicB(a, &globalB);
     83bool logicA( global_t & mutex a ) {
     84        return logicB(a, globalB);
    8585}
    8686
    87 void main( Threads* this ) {
    88         while( logicA(&globalA) ) { yield(); };
     87void main( Threads & this ) {
     88        while( logicA(globalA) ) { yield(); };
    8989}
    9090
  • src/tests/sched-int-block.c

    r8499c707 r83a071f9  
    3030
    3131//------------------------------------------------------------------------------
    32 void wait_op( global_data_t * mutex a, global_data_t * mutex b, unsigned i ) {
     32void wait_op( global_data_t & mutex a, global_data_t & mutex b, unsigned i ) {
    3333        wait( &cond, (uintptr_t)this_thread );
    3434
    3535        yield( ((unsigned)rand48()) % 10 );
    3636
    37         if(a->last_thread != a->last_signaller || b->last_thread != b->last_signaller ) {
    38                 sout | "ERROR Barging detected, expected" | a->last_signaller | b->last_signaller | "got" | a->last_thread | b->last_thread | endl;
     37        if(a.last_thread != a.last_signaller || b.last_thread != b.last_signaller ) {
     38                sout | "ERROR Barging detected, expected" | a.last_signaller | b.last_signaller | "got" | a.last_thread | b.last_thread | endl;
    3939                abort();
    4040        }
    4141
    42         a->last_thread = b->last_thread = this_thread;
     42        a.last_thread = b.last_thread = this_thread;
    4343
    4444        yield( ((unsigned)rand48()) % 10 );
     
    4646
    4747thread Waiter {};
    48 void main( Waiter* this ) {
     48void main( Waiter & this ) {
    4949        for( int i = 0; i < N; i++ ) {
    50                 wait_op( &globalA, &globalB, i );
     50                wait_op( globalA, globalB, i );
    5151        }
    5252}
    5353
    5454//------------------------------------------------------------------------------
    55 void signal_op( global_data_t * mutex a, global_data_t * mutex b ) {
     55void signal_op( global_data_t & mutex a, global_data_t & mutex b ) {
    5656        yield( ((unsigned)rand48()) % 10 );
    5757
    58         a->last_thread = b->last_thread = a->last_signaller = b->last_signaller = this_thread;
     58        [a.last_thread, b.last_thread, a.last_signaller, b.last_signaller] = this_thread;
    5959
    6060        if( !is_empty( &cond ) ) {
     
    6969                yield( ((unsigned)rand48()) % 10 );
    7070
    71                 if(a->last_thread != next || b->last_thread != next) {
    72                         sout | "ERROR Barging detected, expected" | next | "got" | a->last_thread | b->last_thread | endl;
     71                if(a.last_thread != next || b.last_thread != next) {
     72                        sout | "ERROR Barging detected, expected" | next | "got" | a.last_thread | b.last_thread | endl;
    7373                        abort();
    7474                }
     
    7878
    7979thread Signaller {};
    80 void main( Signaller* this ) {
     80void main( Signaller & this ) {
    8181        while( !done ) {
    82                 signal_op( &globalA, &globalB );
     82                signal_op( globalA, globalB );
    8383        }
    8484}
    8585
    8686//------------------------------------------------------------------------------
    87 void barge_op( global_data_t * mutex a ) {
    88         a->last_thread = this_thread;
     87void barge_op( global_data_t & mutex a ) {
     88        a.last_thread = this_thread;
    8989}
    9090
    9191thread Barger {};
    92 void main( Barger* this ) {
     92void main( Barger & this ) {
    9393        for( unsigned i = 0; !done; i++ ) {
    9494                //Choose some monitor to barge into with some irregular pattern
    9595                bool choose_a = (i % 13) > (i % 17);
    96                 barge_op( choose_a ? &globalA : &globalB );
     96                if ( choose_a ) barge_op( globalA );
     97                else barge_op( globalB );
    9798        }
    9899}
  • src/tests/sched-int-disjoint.c

    r8499c707 r83a071f9  
    3535//------------------------------------------------------------------------------
    3636// Barging logic
    37 void barge( global_data_t * mutex d ) {
    38         d->state = BARGE;
     37void barge( global_data_t & mutex d ) {
     38        d.state = BARGE;
    3939}
    4040
    4141thread Barger {};
    4242
    43 void main( Barger * this ) {
     43void main( Barger & this ) {
    4444        while( !all_done ) {
    45                 barge( &data );
     45                barge( data );
    4646                yield();
    4747        }
     
    5050//------------------------------------------------------------------------------
    5151// Waiting logic
    52 bool wait( global_t * mutex m, global_data_t * mutex d ) {
     52bool wait( global_t & mutex m, global_data_t & mutex d ) {
    5353        wait( &cond );
    54         if( d->state != SIGNAL ) {
     54        if( d.state != SIGNAL ) {
    5555                sout | "ERROR barging!" | endl;
    5656        }
    5757
    58         d->counter++;
     58        d.counter++;
    5959
    60         if( (d->counter % 1000) == 0 ) sout | d->counter | endl;
     60        if( (d.counter % 1000) == 0 ) sout | d.counter | endl;
    6161
    62         return d->counter < N;
     62        return d.counter < N;
    6363}
    6464
    6565thread Waiter {};
    6666
    67 void main( Waiter * this ) {
    68         while( wait( &mut, &data ) ) { yield(); }
     67void main( Waiter & this ) {
     68        while( wait( mut, data ) ) { yield(); }
    6969}
    7070
     
    7272//------------------------------------------------------------------------------
    7373// Signalling logic
    74 void signal( condition * cond, global_t * mutex a, global_data_t * mutex b ) {
    75         b->state = SIGNAL;
     74void signal( condition * cond, global_t & mutex a, global_data_t & mutex b ) {
     75        b.state = SIGNAL;
    7676        signal( cond );
    7777}
    7878
    79 void logic( global_t * mutex a ) {
    80         signal( &cond, a, &data );
     79void logic( global_t & mutex a ) {
     80        signal( &cond, a, data );
    8181
    8282        yield( (unsigned)rand48() % 10 );
     
    9191thread Signaller {};
    9292
    93 void main( Signaller * this ) {
     93void main( Signaller & this ) {
    9494        while( !all_done ) {
    95                 logic( &mut );
     95                logic( mut );
    9696                yield();
    9797        }
  • src/tests/sched-int-wait.c

    r8499c707 r83a071f9  
    2727//----------------------------------------------------------------------------------------------------
    2828// Tools
    29 void signal( condition * cond, global_t * mutex a, global_t * mutex b ) {
     29void signal( condition * cond, global_t & mutex a, global_t & mutex b ) {
    3030        signal( cond );
    3131}
    3232
    33 void signal( condition * cond, global_t * mutex a, global_t * mutex b, global_t * mutex c ) {
     33void signal( condition * cond, global_t & mutex a, global_t & mutex b, global_t & mutex c ) {
    3434        signal( cond );
    3535}
    3636
    37 void wait( condition * cond, global_t * mutex a, global_t * mutex b ) {
     37void wait( condition * cond, global_t & mutex a, global_t & mutex b ) {
    3838        wait( cond );
    3939}
    4040
    41 void wait( condition * cond, global_t * mutex a, global_t * mutex b, global_t * mutex c ) {
     41void wait( condition * cond, global_t & mutex a, global_t & mutex b, global_t & mutex c ) {
    4242        wait( cond );
    4343}
     
    4545//----------------------------------------------------------------------------------------------------
    4646// Signaler
    47 void main( Signaler* this ) {
     47void main( Signaler & this ) {
    4848
    4949        while( waiter_left != 0 ) {
     
    5151                switch( action ) {
    5252                        case 0:
    53                                 signal( &condABC, &globalA, &globalB, &globalC );
     53                                signal( &condABC, globalA, globalB, globalC );
    5454                                break;
    5555                        case 1:
    56                                 signal( &condAB , &globalA, &globalB );
     56                                signal( &condAB , globalA, globalB );
    5757                                break;
    5858                        case 2:
    59                                 signal( &condBC , &globalB, &globalC );
     59                                signal( &condBC , globalB, globalC );
    6060                                break;
    6161                        case 3:
    62                                 signal( &condAC , &globalA, &globalC );
     62                                signal( &condAC , globalA, globalC );
    6363                                break;
    6464                        default:
     
    7272//----------------------------------------------------------------------------------------------------
    7373// Waiter ABC
    74 void main( WaiterABC* this ) {
     74void main( WaiterABC & this ) {
    7575        for( int i = 0; i < N; i++ ) {
    76                 wait( &condABC, &globalA, &globalB, &globalC );
     76                wait( &condABC, globalA, globalB, globalC );
    7777        }
    7878
     
    8282//----------------------------------------------------------------------------------------------------
    8383// Waiter AB
    84 void main( WaiterAB* this ) {
     84void main( WaiterAB & this ) {
    8585        for( int i = 0; i < N; i++ ) {
    86                 wait( &condAB , &globalA, &globalB );
     86                wait( &condAB , globalA, globalB );
    8787        }
    8888
     
    9292//----------------------------------------------------------------------------------------------------
    9393// Waiter AC
    94 void main( WaiterAC* this ) {
     94void main( WaiterAC & this ) {
    9595        for( int i = 0; i < N; i++ ) {
    96                 wait( &condAC , &globalA, &globalC );
     96                wait( &condAC , globalA, globalC );
    9797        }
    9898
     
    102102//----------------------------------------------------------------------------------------------------
    103103// Waiter BC
    104 void main( WaiterBC* this ) {
     104void main( WaiterBC & this ) {
    105105        for( int i = 0; i < N; i++ ) {
    106                 wait( &condBC , &globalB, &globalC );
     106                wait( &condBC , globalB, globalC );
    107107        }
    108108
  • src/tests/thread.c

    r8499c707 r83a071f9  
    77thread Second { semaphore* lock; };
    88
    9 void ?{}( First & this, semaphore* lock ) { this.lock = lock; }
    10 void ?{}( Second & this, semaphore* lock ) { this.lock = lock; }
     9void ?{}( First & this, semaphore & lock ) { this.lock = &lock; }
     10void ?{}( Second & this, semaphore & lock ) { this.lock = &lock; }
    1111
    12 void main(First* this) {
     12void main(First& this) {
    1313        for(int i = 0; i < 10; i++) {
    1414                sout | "First : Suspend No." | i + 1 | endl;
    1515                yield();
    1616        }
    17         V(this->lock);
     17        V(this.lock);
    1818}
    1919
    20 void main(Second* this) {
    21         P(this->lock);
     20void main(Second& this) {
     21        P(this.lock);
    2222        for(int i = 0; i < 10; i++) {
    2323                sout | "Second : Suspend No." | i + 1 | endl;
     
    3333                processor p;
    3434                {
    35                         First  f = { &lock };
    36                         Second s = { &lock };
     35                        First  f = { lock };
     36                        Second s = { lock };
    3737                }
    3838        }
Note: See TracChangeset for help on using the changeset viewer.