Ignore:
Timestamp:
Oct 30, 2017, 6:22:50 PM (5 years ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
aaron-thesis, arm-eh, 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, resolv-new, with_gc
Children:
2ba0bc7
Parents:
bfc0f40
Message:

change calls to rand48, formatting

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/tests/boundedBuffer.c

    rbfc0f40 r9f865d1  
     1//
     2// The contents of this file are covered under the licence agreement in the
     3// file "LICENCE" distributed with Cforall.
     4//
     5// boundedBuffer.c --
     6//
     7// Author           : Peter A. Buhr
     8// Created On       : Mon Oct 30 12:45:13 2017
     9// Last Modified By : Peter A. Buhr
     10// Last Modified On : Mon Oct 30 18:00:10 2017
     11// Update Count     : 7
     12//
     13
    114#include <stdlib>
    215#include <fstream>
     
    619
    720monitor Buffer {
    8     condition full, empty;
    9     int front, back, count;
    10     int elements[20];
     21        condition full, empty;
     22        int front, back, count;
     23        int elements[20];
    1124};
    1225
    1326void ?{}( Buffer & buffer ) {
    14     buffer.front = buffer.back = buffer.count = 0;
     27        buffer.front = buffer.back = buffer.count = 0;
    1528}
    1629
     
    1831
    1932void insert( Buffer & mutex buffer, int elem ) {
    20     if ( buffer.count == 20 ) wait( &buffer.empty );
    21     buffer.elements[buffer.back] = elem;
    22     buffer.back = ( buffer.back + 1 ) % 20;
    23     buffer.count += 1;
    24     signal( &buffer.full );
     33        if ( buffer.count == 20 ) wait( &buffer.empty );
     34        buffer.elements[buffer.back] = elem;
     35        buffer.back = ( buffer.back + 1 ) % 20;
     36        buffer.count += 1;
     37        signal( &buffer.full );
    2538}
    2639int remove( Buffer & mutex buffer ) {
    27     if ( buffer.count == 0 ) wait( &buffer.full );
    28     int elem = buffer.elements[buffer.front];
    29     buffer.front = ( buffer.front + 1 ) % 20;
    30     buffer.count -= 1;
    31     signal( &buffer.empty );
    32     return elem;
     40        if ( buffer.count == 0 ) wait( &buffer.full );
     41        int elem = buffer.elements[buffer.front];
     42        buffer.front = ( buffer.front + 1 ) % 20;
     43        buffer.count -= 1;
     44        signal( &buffer.empty );
     45        return elem;
    3346}
    3447
    3548thread Producer {
    36     Buffer * buffer;
    37     unsigned int N;
     49        Buffer & buffer;
     50        unsigned int N;
    3851};
    3952void main( Producer & prod ) {
    40     for ( int i = 1; i <= prod.N; i += 1 ) {
    41         yield( (unsigned int)rand48() % 5 );
    42         insert( *prod.buffer, 1 );
    43     } // for
    44     insert( *prod.buffer, -1 );
     53        for ( int i = 1; i <= prod.N; i += 1 ) {
     54                yield( rand48( 5 ) );
     55                insert( prod.buffer, 1 );
     56        } // for
     57        insert( prod.buffer, -1 );
    4558}
    4659void ?{}( Producer & prod, Buffer * buffer, unsigned int N ) {
    47     prod.buffer = buffer;
    48     prod.N = N;
    49 }
     60        &prod.buffer = buffer;
     61        prod.N = N;
     62                }
    5063
    5164thread Consumer {
    52     Buffer * buffer;
    53     int * sum;                                          // summation of producer values
     65        Buffer & buffer;
     66        int & sum;                                              // summation of producer values
    5467};
    5568void main( Consumer & cons ) {
    56     *cons.sum = 0;
    57     for ( ;; ) {
    58         yield( (unsigned int)rand48() % 5 );
    59         int item = remove( *cons.buffer );
    60       if ( item == -1 ) break;                          // sentinel ?
    61         *cons.sum += item;
    62     } // for
     69        cons.sum = 0;
     70        for ( ;; ) {
     71                yield( rand48( 5 ) );
     72                int item = remove( cons.buffer );
     73                if ( item == -1 ) break;                                // sentinel ?
     74                cons.sum += item;
     75        } // for
    6376}
    6477void ?{}( Consumer & cons, Buffer * buffer, int * sum ) {
    65     cons.buffer = buffer;
    66     cons.sum = sum;
     78        &cons.buffer = buffer;
     79        &cons.sum = sum;
    6780}
    6881
    6982int main() {
    70     Buffer buffer;
    71     enum { Prods = 5, Cons = 5 };
    72     Producer * prods[Prods];
    73     Consumer * cons[Cons];
    74     const int Sentinel = -1;
    75     int sums[Cons];
    76     int i;
    77     processor p;
     83        Buffer buffer;
     84        enum { Prods = 5, Cons = 5 };
     85        Producer * prods[Prods];
     86        Consumer * cons[Cons];
     87        const int Sentinel = -1;
     88        int sums[Cons];
     89        int i;
     90        processor p;
    7891
    79     //rand48seed( getpid() );
    80     rand48seed( 1003 );
     92        //rand48seed( getpid() );
     93        rand48seed( 1003 );
    8194
    82     for ( i = 0; i < Cons; i += 1 ) {                   // create consumers
    83         cons[i] = new( &buffer, &sums[i] );
    84     } // for
    85     for ( i = 0; i < Prods; i += 1 ) {                  // create producers
    86         prods[i] = new( &buffer, 100000u );
    87     } // for
     95        for ( i = 0; i < Cons; i += 1 ) {                       // create consumers
     96                cons[i] = new( &buffer, &sums[i] );
     97        } // for
     98        for ( i = 0; i < Prods; i += 1 ) {                      // create producers
     99                prods[i] = new( &buffer, 100000u );
     100        } // for
    88101
    89     for ( i = 0; i < Prods; i += 1 ) {                  // wait for producers to finish
    90         delete( prods[i] );
    91     } // for
    92     for ( i = 0; i < Cons; i += 1 ) {                   // generate sentinal values to stop consumers
    93         insert( buffer, Sentinel );
    94     } // for
    95     int sum = 0;
    96     for ( i = 0; i < Cons; i += 1 ) {                   // wait for consumers to finish
    97         delete( cons[i] );
    98         sum += sums[i];
    99     } // for
    100     sout | "total:" | sum | endl;
     102        for ( i = 0; i < Prods; i += 1 ) {                      // wait for producers to finish
     103                delete( prods[i] );
     104        } // for
     105        for ( i = 0; i < Cons; i += 1 ) {                       // generate sentinal values to stop consumers
     106                insert( buffer, Sentinel );
     107        } // for
     108        int sum = 0;
     109        for ( i = 0; i < Cons; i += 1 ) {                       // wait for consumers to finish
     110                delete( cons[i] );
     111                sum += sums[i];
     112        } // for
     113        sout | "total:" | sum | endl;
    101114}
     115
     116// Local Variables: //
     117// tab-width: 4 //
     118// compile-command: "cfa boundedBuffer.c" //
     119// End: //
Note: See TracChangeset for help on using the changeset viewer.