1 | #include <select.hfa> |
---|
2 | #include <thread.hfa> |
---|
3 | #include <channel.hfa> |
---|
4 | #include <time.hfa> |
---|
5 | |
---|
6 | channel(long long int) A, B, C, D, E, F; |
---|
7 | |
---|
8 | volatile long long int inserts = 0; |
---|
9 | volatile long long int removes = 0; |
---|
10 | |
---|
11 | thread Producer {}; |
---|
12 | void main( Producer & ) { |
---|
13 | long long int A_i = 0, B_i = 0, C_i = 0, D_i = 0, E_i = 0, F_i = 0; |
---|
14 | try { |
---|
15 | for( long long int i = 0;;i++ ) { |
---|
16 | waituntil( A << i ) { A_i++; } |
---|
17 | and waituntil( B << i ) { B_i++; } |
---|
18 | and waituntil( C << i ) { C_i++; } |
---|
19 | and waituntil( D << i ) { D_i++; } |
---|
20 | and waituntil( E << i ) { E_i++; } |
---|
21 | and waituntil( F << i ) { F_i++; } |
---|
22 | } |
---|
23 | } catch ( channel_closed * e ) {} |
---|
24 | __atomic_fetch_add( &inserts, A_i + B_i + C_i + D_i + E_i + F_i, __ATOMIC_SEQ_CST ); |
---|
25 | } |
---|
26 | |
---|
27 | thread Consumer {}; |
---|
28 | void main( Consumer & ) { |
---|
29 | long long int in, A_removes = 0, B_removes = 0, C_removes = 0, D_removes = 0, E_removes = 0, F_removes = 0; |
---|
30 | try { |
---|
31 | for( ;; ) { |
---|
32 | waituntil( remove(F) ) { F_removes++; } |
---|
33 | or waituntil( remove(E) ) { E_removes++; } |
---|
34 | or waituntil( remove(D) ) { D_removes++; } |
---|
35 | or waituntil( remove(C) ) { C_removes++; } |
---|
36 | or waituntil( remove(B) ) { B_removes++; } |
---|
37 | or waituntil( remove(A) ) { A_removes++; } |
---|
38 | } |
---|
39 | } catchResume ( channel_closed * e ) { } // continue to remove until would block |
---|
40 | catch ( channel_closed * e ) {} |
---|
41 | try { |
---|
42 | for( ;; ) |
---|
43 | waituntil( (in << A) ) { A_removes++; } |
---|
44 | } catchResume ( channel_closed * e ) {} // continue to remove until would block |
---|
45 | catch ( channel_closed * e ) {} |
---|
46 | try { |
---|
47 | for( ;; ) |
---|
48 | waituntil( (in << B) ) { B_removes++; } |
---|
49 | } catchResume ( channel_closed * e ) {} // continue to remove until would block |
---|
50 | catch ( channel_closed * e ) {} |
---|
51 | try { |
---|
52 | for( ;; ) |
---|
53 | waituntil( (in << C) ) { C_removes++; } |
---|
54 | } catchResume ( channel_closed * e ) {} // continue to remove until would block |
---|
55 | catch ( channel_closed * e ) {} |
---|
56 | try { |
---|
57 | for( ;; ) |
---|
58 | waituntil( (in << D) ) { D_removes++; } |
---|
59 | } catchResume ( channel_closed * e ) {} // continue to remove until would block |
---|
60 | catch ( channel_closed * e ) {} |
---|
61 | try { |
---|
62 | for( ;; ) |
---|
63 | waituntil( (in << E) ) { E_removes++; } |
---|
64 | } catchResume ( channel_closed * e ) {} // continue to remove until would block |
---|
65 | catch ( channel_closed * e ) {} |
---|
66 | try { |
---|
67 | for( ;; ) |
---|
68 | waituntil( (in << F) ) { F_removes++; } |
---|
69 | } catchResume ( channel_closed * e ) {} // continue to remove until would block |
---|
70 | catch ( channel_closed * e ) {} |
---|
71 | __atomic_fetch_add( &removes, A_removes + B_removes + C_removes + D_removes + E_removes + F_removes, __ATOMIC_SEQ_CST ); |
---|
72 | } |
---|
73 | |
---|
74 | |
---|
75 | size_t time = 3, num_times = 10, chan_size = 0, num_thds = 2; |
---|
76 | int main( int argc, char * argv[] ) { |
---|
77 | if ( argc == 2 ) |
---|
78 | time = atoi( argv[1] ); |
---|
79 | |
---|
80 | processor p[ num_thds - 1 ]; |
---|
81 | |
---|
82 | printf("Start\n"); |
---|
83 | for ( i; num_times ) { |
---|
84 | printf("%zu\n", i); |
---|
85 | A{chan_size}; |
---|
86 | B{chan_size}; |
---|
87 | C{chan_size}; |
---|
88 | D{chan_size}; |
---|
89 | E{chan_size}; |
---|
90 | F{chan_size}; |
---|
91 | { |
---|
92 | Producer p[ num_thds / 2 ]; |
---|
93 | Consumer c[ num_thds / 2 ]; |
---|
94 | sleep(time`s); |
---|
95 | close(A); |
---|
96 | close(B); |
---|
97 | close(C); |
---|
98 | close(D); |
---|
99 | close(E); |
---|
100 | close(F); |
---|
101 | } |
---|
102 | if ( inserts != removes ) { |
---|
103 | printf("\n"); |
---|
104 | printf("CHECKSUM MISMATCH!! Producer got: %lld, Consumer got: %lld\n", inserts, removes); |
---|
105 | assert(false); |
---|
106 | } |
---|
107 | ^A{}; |
---|
108 | ^B{}; |
---|
109 | ^C{}; |
---|
110 | ^D{}; |
---|
111 | ^E{}; |
---|
112 | ^F{}; |
---|
113 | |
---|
114 | inserts = 0; |
---|
115 | removes = 0; |
---|
116 | } |
---|
117 | |
---|
118 | A{5}; |
---|
119 | B{5}; |
---|
120 | C{5}; |
---|
121 | D{5}; |
---|
122 | E{5}; |
---|
123 | F{5}; |
---|
124 | printf("Done\n"); |
---|
125 | } |
---|