Changeset 1417f6b


Ignore:
Timestamp:
Apr 1, 2022, 1:08:24 PM (2 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
ADT, ast-experimental, enum, master, pthread-emulation, qualifiedEnum
Children:
e71e94a
Parents:
31ef267
Message:

Changed comp_fair to use a barrier rather than a semaphore.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • tests/io/comp_fair.cfa

    r31ef267 r1417f6b  
    1616
    1717
    18 #include <concurrency/locks.hfa>
     18#include <concurrency/barrier.hfa>
    1919#include <fstream.hfa>
    2020#include <iofwd.hfa>
     
    2727
    2828struct {
    29       semaphore sem;
     29      barrier & bar;
    3030      int pipe[2];
    3131
     
    5959                yield();
    6060        }
    61 
    62       mutex(sout) sout | "Spinner done";
    6361}
    6462
     
    7068
    7169      for(TIMES) {
    72                 yield( prng( this, 15 ) );
    73 
    7470            io_future_t f;
    7571            if ( do_read ) {
     
    8076            }
    8177
    82             P( globals.sem );
     78            block( globals.bar );
     79
     80                yield( prng( this, 15 ) );
    8381
    8482            unsigned i = __atomic_add_fetch( &counter, 1, __ATOMIC_SEQ_CST );
     
    8987            if(f.result < 0)
    9088                  abort | "Read error" | -f.result | ":" | strerror(-f.result);
     89
     90            block( globals.bar );
    9191      }
    92 
    93       P( globals.sem );
    94 
    95       mutex(sout) sout | "Reader done";
    9692}
    9793
     
    10298void main(Writer & this) {
    10399      for(TIMES) {
    104             yield( prng( this, 15 ) );
    105 
    106             V( globals.sem );
     100            block( globals.bar );
    107101
    108102            sleep( 1`us );
     
    113107                  abort | "Write error" | errno | ":" | strerror(errno);
    114108
     109            block( globals.bar );
    115110      }
    116 
    117       mutex(sout) sout | "Writer done";
    118111}
    119112
     
    126119                yield();
    127120        }
    128 
    129       mutex(sout) sout | "Yielder done";
    130121}
    131122
    132123int main() {
     124      barrier bar = { 2 };
     125      &globals.bar = &bar;
    133126      int ret = pipe(globals.pipe);
    134127      if(ret != 0)
Note: See TracChangeset for help on using the changeset viewer.