1 | #include <locks.hfa>
|
---|
2 | #include <fstream.hfa>
|
---|
3 | #include <stdio.h>
|
---|
4 | #include <string.h>
|
---|
5 | #include "channel.hfa"
|
---|
6 | #include <thread.hfa>
|
---|
7 | #include <time.hfa>
|
---|
8 | #include <stats.hfa>
|
---|
9 | size_t Processors = 1, Channels = 4, Producers = 1, Consumers = 1, ChannelSize = 128;
|
---|
10 |
|
---|
11 | owner_lock o;
|
---|
12 |
|
---|
13 | size_t total_operations = 0;
|
---|
14 | size_t cons_check = 0, prod_check = 0;
|
---|
15 |
|
---|
16 | channel( size_t ) * channels;
|
---|
17 | thread Consumer {};
|
---|
18 | bool cons_done = false, prod_done = false;
|
---|
19 | volatile int cons_done_count = 0;
|
---|
20 |
|
---|
21 | void getRandArray( int * chanIndices ) {
|
---|
22 | for ( int i = 0; i < Channels; i++ ) {
|
---|
23 | chanIndices[i] = i;
|
---|
24 | }
|
---|
25 | for ( int i = 0; i < Channels; i++ ) {
|
---|
26 | int loc_1 = prng() % Channels;
|
---|
27 | int loc_2 = prng() % Channels;
|
---|
28 | int temp = chanIndices[ loc_1 ];
|
---|
29 | chanIndices[ loc_1 ] = chanIndices[ loc_2 ];
|
---|
30 | chanIndices[ loc_2 ] = temp;
|
---|
31 | }
|
---|
32 | }
|
---|
33 |
|
---|
34 | void main(Consumer & this) {
|
---|
35 | size_t i = 0;
|
---|
36 | size_t runs = 0;
|
---|
37 | size_t my_check = 0;
|
---|
38 | int chanIndices[Channels];
|
---|
39 | getRandArray( chanIndices );
|
---|
40 |
|
---|
41 | for ( ;;i++ ) {
|
---|
42 | if ( cons_done ) break;
|
---|
43 | size_t j = remove( channels[ chanIndices[ i % Channels ] ] );
|
---|
44 | my_check = my_check ^ j;
|
---|
45 | if ( !prod_done ) runs++;
|
---|
46 | }
|
---|
47 | lock(o);
|
---|
48 | total_operations += runs;
|
---|
49 | cons_done_count++;
|
---|
50 | cons_check = cons_check ^ my_check;
|
---|
51 | // sout | "Cons: " | runs;
|
---|
52 | unlock(o);
|
---|
53 | }
|
---|
54 |
|
---|
55 | thread Producer {};
|
---|
56 |
|
---|
57 | void main(Producer & this) {
|
---|
58 | size_t i = 0;
|
---|
59 | size_t runs = 0;
|
---|
60 | size_t my_check = 0;
|
---|
61 | int chanIndices[Channels];
|
---|
62 | getRandArray( chanIndices );
|
---|
63 | for ( ;;i++ ) {
|
---|
64 | if ( prod_done ) break;
|
---|
65 | insert( channels[ chanIndices[ i % Channels ] ], i );
|
---|
66 | my_check = my_check ^ i;
|
---|
67 | runs++;
|
---|
68 | }
|
---|
69 | lock(o);
|
---|
70 | total_operations += runs;
|
---|
71 | prod_check = prod_check ^ my_check;
|
---|
72 | // sout | "Prods: " | runs;
|
---|
73 | unlock(o);
|
---|
74 | }
|
---|
75 |
|
---|
76 |
|
---|
77 | int main( int argc, char *argv[] ) {
|
---|
78 | switch( argc ) {
|
---|
79 | case 4:
|
---|
80 | if ( strcmp( argv[3], "d" ) != 0 ) { // default ?
|
---|
81 | if ( atoi( argv[3] ) < 1 ) goto Usage;
|
---|
82 | ChannelSize = atoi( argv[3] );
|
---|
83 | } // if
|
---|
84 | case 3:
|
---|
85 | if ( strcmp( argv[2], "d" ) != 0 ) { // default ?
|
---|
86 | if ( atoi( argv[2] ) < 1 ) goto Usage;
|
---|
87 | Channels = atoi( argv[2] );
|
---|
88 | } // if
|
---|
89 | case 2:
|
---|
90 | if ( strcmp( argv[1], "d" ) != 0 ) { // default ?
|
---|
91 | if ( atoi( argv[1] ) < 1 ) goto Usage;
|
---|
92 | Processors = atoi( argv[1] );
|
---|
93 | } // if
|
---|
94 | case 1: // use defaults
|
---|
95 | break;
|
---|
96 | default:
|
---|
97 | Usage:
|
---|
98 | sout | "Usage: " | argv[0]
|
---|
99 | | " [ processors > 0 | d ]"
|
---|
100 | | " [ producers > 0 | d ]"
|
---|
101 | | " [ consumers > 0 | d ]"
|
---|
102 | | " [ channels > 0 | d ]";
|
---|
103 | exit( EXIT_FAILURE );
|
---|
104 | }
|
---|
105 | processor p[Processors - 1];
|
---|
106 | channels = anew( Channels );
|
---|
107 | Producers = Processors / 2;
|
---|
108 | Consumers = Processors / 2;
|
---|
109 |
|
---|
110 | // sout | "Processors: " | Processors | " Producers: " | Producers | " Consumers: " | Consumers | "Channels: " | Channels | " Channel Size: " | ChannelSize;
|
---|
111 |
|
---|
112 | for ( i; Channels ) {
|
---|
113 | channels[i]{ ChannelSize };
|
---|
114 | }
|
---|
115 |
|
---|
116 | // sout | "start";
|
---|
117 | {
|
---|
118 | Consumer c[Consumers];
|
---|
119 | {
|
---|
120 | Producer p[Producers];
|
---|
121 | sleep(10`s);
|
---|
122 | prod_done = true;
|
---|
123 | // sout | "sleep";
|
---|
124 | }
|
---|
125 | // sout | "prods";
|
---|
126 | cons_done = true;
|
---|
127 | // for ( i; Channels ) {
|
---|
128 | // if ( get_count( channels[i] ) < Consumers ){
|
---|
129 | // for ( j; Consumers ) insert( channels[i], 0 );
|
---|
130 | // }
|
---|
131 | // }
|
---|
132 | while( cons_done_count != Consumers ) {
|
---|
133 | for ( i; Channels ) {
|
---|
134 | if ( has_waiters( channels[i] ) ){
|
---|
135 | insert( channels[i], 0 );
|
---|
136 | }
|
---|
137 | }
|
---|
138 | }
|
---|
139 | }
|
---|
140 | // sout | "cons";
|
---|
141 |
|
---|
142 | for ( i; Channels ) {
|
---|
143 | for ( ;; ) {
|
---|
144 | if ( get_count( channels[i] ) > 0 ) {
|
---|
145 | size_t j = remove( channels[ i ] );
|
---|
146 | cons_check = cons_check ^ j;
|
---|
147 | } else break;
|
---|
148 | }
|
---|
149 | }
|
---|
150 |
|
---|
151 | adelete( channels );
|
---|
152 |
|
---|
153 | if ( cons_check != prod_check )
|
---|
154 | sout | "CHECKSUM MISMATCH !!!";
|
---|
155 | // print_stats_now( *active_cluster(), CFA_STATS_READY_Q);
|
---|
156 |
|
---|
157 | // for ( i; Processors ) {
|
---|
158 | // delete(proc[i]);
|
---|
159 | // }
|
---|
160 |
|
---|
161 | sout | total_operations;
|
---|
162 | // print_stats_now( *active_cluster(), CFA_STATS_READY_Q );
|
---|
163 | return 0;
|
---|
164 | }
|
---|