source: tests/concurrency/waituntil/channels.cfa @ 1db6d70

ast-experimental
Last change on this file since 1db6d70 was c26bea2a, checked in by Peter A. Buhr <pabuhr@…>, 13 months ago

first attempt at renaming directory tests/concurrent to tests/concurrency to harmonize with other concurrency directory names

  • Property mode set to 100644
File size: 2.7 KB
Line 
1#include <select.hfa>
2#include <thread.hfa>
3#include <channel.hfa>
4
5channel(long long int) A, B, C;
6
7volatile bool done = false;
8long long int globalTotal = 0;
9
10thread Server1 {};
11void main( Server1 & this ) {
12    long long int a, b, c, i = 0, myTotal = 0;
13    for( ;;i++ ) {
14        when( i % 2 == 0 ) waituntil( a << A ) { myTotal += a; }
15        or when( i % 4 < 2 ) waituntil( b << B ) { myTotal += b; }
16        or waituntil( c << C ) { if ( c == -1 ) break; myTotal += c; }
17        or when( i % 8 < 4 ) else {}
18    }
19    __atomic_fetch_add( &globalTotal, myTotal, __ATOMIC_SEQ_CST );
20}
21
22thread Drainer {}; // ensures that the changing when states of Server1 don't result in a deadlock
23void main( Drainer & this ) {
24    long long int a, b, c, myTotal = 0;
25    for( ;; ) {
26        waituntil( a << A ) { myTotal += a; }
27        or waituntil( b << B ) { myTotal += b; }
28        or waituntil( c << C ) { if ( c == -1 ) break; myTotal += c; }
29        or else {}
30    }
31    __atomic_fetch_add( &globalTotal, myTotal, __ATOMIC_SEQ_CST );
32}
33
34thread Churner {}; // performs non-waituntil try insert/remove operations to add churn/interference
35void main( Churner & this ) {
36    long long int out, myTotal = 0;
37    bool success;
38    while( !done ) {
39        try_insert( A, 0 );
40        try_insert( B, 0 );
41        try_insert( C, 0 );
42        [out, success] = try_remove( A );
43        if ( success ) myTotal += out;
44        [out, success] = try_remove( B );
45        if ( success ) myTotal += out;
46        [out, success] = try_remove( C );
47        if ( success ) myTotal += out;
48    }
49    __atomic_fetch_add( &globalTotal, myTotal, __ATOMIC_SEQ_CST );
50}
51
52size_t numtimes = 100000;
53size_t numServers = 3;
54int main( int argc, char * argv[] ) {
55    if ( argc == 2 )
56        numtimes = atoi( argv[1] );
57
58    processor p[numServers + 2];
59    A{5};
60    B{5};
61    C{5};
62
63    long long int total = 0;
64    printf("start\n");
65    {
66        Server1 s[numServers];
67        Drainer d;
68        {
69            Churner c;
70            for( long long int j = 0; j < numtimes; j++ ) {
71                when( j % 2 == 0 ) waituntil( j >> A ) { total += j; }
72                or when( j % 4 < 2 ) waituntil( j >> B ) { total += j; }
73                and when( j % 8 < 4 ) waituntil( j >> C ) { total += j; }
74            }
75            done = true;
76            printf("terminating churner\n");
77        }
78        printf("waiting for empty channels\n");
79        while( get_count( A ) > 0 || get_count( B ) > 0 || get_count( C ) > 0 ) { }
80        printf("sending sentinels\n");
81        for ( i; numServers + 1 ) insert( C, -1 );
82        printf("joining servers\n");
83    }
84    if ( total != globalTotal )
85        printf("CHECKSUM MISMATCH!! Main thread got %lld, server sum is %lld\n", total, globalTotal);
86    printf("done\n");
87}
Note: See TracBrowser for help on using the repository browser.