Ignore:
Timestamp:
May 4, 2017, 4:13:36 PM (4 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
aaron-thesis, arm-eh, cleanup-dtors, deferred_resn, demangler, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, resolv-new, with_gc
Children:
9737ffe
Parents:
4845ae2
Message:

-fixed error in concurrency keywords
-implemented more agressive test for internal scheduling

File:
1 edited

Legend:

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

    r4845ae2 r102a58b  
    44#include <thread>
    55
    6 monitor global_t {
    7         int value;
    8 };
     6#define N 100_000
    97
    10 global_t global;
     8enum state_t { WAIT, SIGNAL, BARGE };
     9
     10monitor global_t {};
     11global_t mut;
     12
     13monitor global_data_t;
     14void ?{}( global_data_t * this );
     15void ^?{} ( global_data_t * this );
     16
     17monitor global_data_t {
     18        int counter;
     19        state_t state;
     20} data;
    1121
    1222condition cond;
    1323
    14 thread Signalee {};
    15 thread Signaler {};
     24volatile bool all_done;
    1625
    17 void step1( global_t * mutex this ) {
    18         sout | "Step 1" | endl;
    19         this->value = 1;
    20         wait( &cond );
     26void ?{}( global_data_t * this ) {
     27        this->counter == 0;
     28        this->state = BARGE;
    2129}
    2230
    23 void step2( global_t * mutex this ) {
    24         if( this->value != 1) abort();
     31void ^?{} ( global_data_t * this ) {}
    2532
    26         sout | "Step 2" | endl;
    27         this->value = 2;
    28         signal( &cond );
     33//------------------------------------------------------------------------------
     34// Barging logic
     35void barge( global_data_t * mutex d ) {
     36        d->state = BARGE;
    2937}
    3038
    31 void step3( global_t * mutex this ) {
    32         if( this->value != 2) abort();
     39thread Barger {};
    3340
    34         sout | "Step 3" | endl;
    35         this->value = 3;
    36         signal( &cond );
     41void main( Barger * this ) {
     42        while( !all_done ) {
     43                barge( &data );
     44                yield();
     45        }
    3746}
    3847
    39 void main( Signalee* this ) {
    40         step1( &global );
    41         step3( &global );
     48//------------------------------------------------------------------------------
     49// Waiting logic
     50bool wait( global_t * mutex m, global_data_t * mutex d ) {
     51        wait( &cond );
     52        if( d->state != SIGNAL ) {
     53                sout | "ERROR barging!" | endl;
     54        }
     55
     56        d->counter++;
     57
     58        if( (d->counter % 1000) == 0 ) sout | d->counter | endl;
     59
     60        return d->counter < N;
    4261}
    4362
    44 void main( Signaler* this ) {
    45         for(int i = 0; i < 10_000; i++) {
    46                 asm volatile ("" : : : "memory");
     63thread Waiter {};
     64
     65void main( Waiter * this ) {
     66        while( wait( &mut, &data ) ) { yield(); }
     67}
     68
     69
     70//------------------------------------------------------------------------------
     71// Signalling logic
     72void signal( condition * cond, global_t * mutex a, global_data_t * mutex b ) {
     73        b->state = SIGNAL;
     74        signal( cond );
     75}
     76
     77void logic( global_t * mutex a ) {
     78        signal( &cond, a, &data );
     79
     80        int pauses = (unsigned)rand48() % 10;
     81        for(int i = 0; i < pauses; i++) {
     82                yield();
    4783        }
    4884
    49         step2( &global );
     85        //This is technically a mutual exclusion violation but the mutex monitor protects us
     86        bool running = data.counter < N && data.counter > 0;
     87        if( data.state != SIGNAL && running ) {
     88                sout | "ERROR Eager signal" | data.state | endl;
     89        }
    5090}
    5191
     92thread Signaller {};
     93
     94void main( Signaller * this ) {
     95        while( !all_done ) {
     96                logic( &mut );
     97                yield();
     98        }
     99}
     100
     101//------------------------------------------------------------------------------
     102// Main loop
    52103int main(int argc, char* argv[]) {
    53         assert( global.__mon.entry_queue.tail != NULL );
     104        all_done = false;
    54105        processor p;
    55106        {
    56                 Signalee a;
    57                 Signaler b;
    58         }
    59         if( global.value != 3) abort();
     107                Signaller s;
     108                Barger b[17];
     109                {
     110                        Waiter w[4];
     111                }
     112                sout | "All waiter done" | endl;
     113                all_done = true;
     114        }       
    60115}
Note: See TracChangeset for help on using the changeset viewer.