1 | #include <select.hfa>
|
---|
2 | #include <thread.hfa>
|
---|
3 | #include <channel.hfa>
|
---|
4 | #include <locks.hfa>
|
---|
5 | #include <fstream.hfa>
|
---|
6 | #include <stdio.h>
|
---|
7 | #include <time.hfa>
|
---|
8 | #include <string.h>
|
---|
9 |
|
---|
10 | size_t Sets = 1, ChannelSize = 100, Channels = 2;
|
---|
11 |
|
---|
12 | channel(size_t) * chans;
|
---|
13 |
|
---|
14 | size_t globalTotal = 0;
|
---|
15 | int cons_counter = 0, prod_counter = 0;
|
---|
16 |
|
---|
17 | thread SelectConsumer {};
|
---|
18 | void main( SelectConsumer & this ) {
|
---|
19 | size_t val, i = 0;
|
---|
20 | try {
|
---|
21 | for(;; i++ ) {
|
---|
22 | waituntil( val << chans[0] ) {} or waituntil( val << chans[1] ) {}
|
---|
23 | }
|
---|
24 | } catch( channel_closed * e ) {}
|
---|
25 | __atomic_fetch_add( &globalTotal, i, __ATOMIC_SEQ_CST );
|
---|
26 | }
|
---|
27 |
|
---|
28 | thread SelectProducer {};
|
---|
29 | void main( SelectProducer & this ) {
|
---|
30 | try {
|
---|
31 | for( size_t i = 0;; i++ ) {
|
---|
32 | waituntil( chans[0] << i ) {} or waituntil( chans[1] << i ) {}
|
---|
33 | }
|
---|
34 | } catch( channel_closed * e ) {}
|
---|
35 | }
|
---|
36 |
|
---|
37 | thread Consumer {};
|
---|
38 | void main( Consumer & this ) {
|
---|
39 | const int idx = __atomic_fetch_add( &cons_counter, 1, __ATOMIC_SEQ_CST ) % Channels;
|
---|
40 | size_t val, i = 0;
|
---|
41 | try {
|
---|
42 | for(;; i++ ) {
|
---|
43 | remove( chans[idx] );
|
---|
44 | }
|
---|
45 | } catch( channel_closed * e ) {}
|
---|
46 | __atomic_fetch_add( &globalTotal, i, __ATOMIC_SEQ_CST );
|
---|
47 | }
|
---|
48 |
|
---|
49 | thread Producer {};
|
---|
50 | void main( Producer & this ) {
|
---|
51 | const int idx = __atomic_fetch_add( &prod_counter, 1, __ATOMIC_SEQ_CST ) % Channels;
|
---|
52 | try {
|
---|
53 | for( size_t i = 0;; i++ ) {
|
---|
54 | insert( chans[idx], i );
|
---|
55 | }
|
---|
56 | } catch( channel_closed * e ) {}
|
---|
57 | }
|
---|
58 |
|
---|
59 | int main( int argc, char * argv[] ) {
|
---|
60 | switch ( argc ) {
|
---|
61 | case 3:
|
---|
62 | if ( strcmp( argv[2], "d" ) != 0 ) { // default ?
|
---|
63 | ChannelSize = atoi( argv[2] );
|
---|
64 | } // if
|
---|
65 | case 2:
|
---|
66 | if ( strcmp( argv[1], "d" ) != 0 ) { // default ?
|
---|
67 | Sets = atoi( argv[1] );
|
---|
68 | if ( Sets < 1 ) goto Usage;
|
---|
69 | } // if
|
---|
70 | case 1: // use defaults
|
---|
71 | break;
|
---|
72 | default:
|
---|
73 | Usage:
|
---|
74 | sout | "Usage: " | argv[0]
|
---|
75 | | " [ sets (> 0) | 'd' (default " | Sets
|
---|
76 | | ") ] [ channel size (>= 0) | 'd' (default " | ChannelSize
|
---|
77 | | ") ]" ;
|
---|
78 | exit( EXIT_FAILURE );
|
---|
79 | } // switch
|
---|
80 |
|
---|
81 | processor p[Sets * 2 + Sets * Channels * 2 - 1];
|
---|
82 |
|
---|
83 | chans = aalloc( Channels );
|
---|
84 | for ( i; Channels )
|
---|
85 | chans[i]{ ChannelSize };
|
---|
86 |
|
---|
87 | {
|
---|
88 | Producer p[Sets * Channels];
|
---|
89 | SelectProducer sp[Sets];
|
---|
90 | Consumer c[Sets * Channels];
|
---|
91 | SelectConsumer sc[Sets];
|
---|
92 |
|
---|
93 | sleep(10`s);
|
---|
94 |
|
---|
95 | for ( i; Channels )
|
---|
96 | close( chans[i] );
|
---|
97 | }
|
---|
98 | adelete( chans );
|
---|
99 | printf("%zu\n", globalTotal);
|
---|
100 | }
|
---|