1 | #include <select.hfa> |
---|
2 | #include <thread.hfa> |
---|
3 | #include <channel.hfa> |
---|
4 | |
---|
5 | channel(long long int) A, B, C; |
---|
6 | |
---|
7 | volatile bool done = false; |
---|
8 | long long int globalTotal = 0; |
---|
9 | |
---|
10 | thread Server1 {}; |
---|
11 | void main( Server1 & this ) { |
---|
12 | long long int a, b, c, i = 0, myTotal = 0; |
---|
13 | for( ;;i++ ) { |
---|
14 | waituntil( a << A ) { myTotal += a; } |
---|
15 | or waituntil( b << B ) { myTotal += b; } |
---|
16 | or waituntil( c << C ) { if ( c == -1 ) break; myTotal += c; } |
---|
17 | } |
---|
18 | __atomic_fetch_add( &globalTotal, myTotal, __ATOMIC_SEQ_CST ); |
---|
19 | } |
---|
20 | |
---|
21 | size_t numtimes = 100000; |
---|
22 | size_t numServers = 1; |
---|
23 | int main( int argc, char * argv[] ) { |
---|
24 | if ( argc == 2 ) |
---|
25 | numtimes = atoi( argv[1] ); |
---|
26 | |
---|
27 | processor p[numServers]; |
---|
28 | A{5}; |
---|
29 | B{5}; |
---|
30 | C{5}; |
---|
31 | |
---|
32 | long long int total = 0; |
---|
33 | printf("start\n"); |
---|
34 | { |
---|
35 | Server1 s[numServers]; |
---|
36 | for( long long int j = 0; j < numtimes; j++ ) { |
---|
37 | waituntil( j >> A ) { total += j; } |
---|
38 | or waituntil( j >> B ) { total += j; } |
---|
39 | and waituntil( j >> C ) { total += j; } |
---|
40 | } |
---|
41 | printf("waiting for empty channels\n"); |
---|
42 | size_t A_count = get_count( A ); |
---|
43 | size_t B_count = get_count( B ); |
---|
44 | size_t C_count = get_count( C ); |
---|
45 | while( A_count > 0 || B_count > 0 || C_count > 0 ) { |
---|
46 | A_count = get_count( A ); |
---|
47 | B_count = get_count( B ); |
---|
48 | C_count = get_count( C ); |
---|
49 | } |
---|
50 | printf("sending sentinels\n"); |
---|
51 | for ( i; numServers ) insert( C, -1 ); |
---|
52 | printf("joining servers\n"); |
---|
53 | } |
---|
54 | if ( total != globalTotal ) |
---|
55 | printf("CHECKSUM MISMATCH!! Main thread got %lld, server sum is %lld\n", total, globalTotal); |
---|
56 | printf("done\n"); |
---|
57 | } |
---|