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 | // #define NUM_CHANS 1 |
---|
11 | #define GLUE_HELPER(x, y) x##y |
---|
12 | #define FN_GLUE(x, y) GLUE_HELPER(x, y) |
---|
13 | |
---|
14 | size_t Processors = 2, Producers = 1, Consumers = 1, ChannelSize = 10; |
---|
15 | |
---|
16 | channel(size_t) * chans; |
---|
17 | |
---|
18 | static inline void cons_wait_1( size_t & val, size_t & i ) { waituntil( val << chans[0] ) {} or else { i--; } } |
---|
19 | static inline void cons_wait_2( size_t & val, size_t & i ) { waituntil( val << chans[0] ) {} or waituntil( val << chans[1] ) {} or else { i--; } } |
---|
20 | static inline void cons_wait_3( size_t & val, size_t & i ) { waituntil( val << chans[0] ) {} or waituntil( val << chans[1] ) {} or waituntil( val << chans[2] ) {} or else { i--; } } |
---|
21 | static inline void cons_wait_4( size_t & val, size_t & i ) { waituntil( val << chans[0] ) {} or waituntil( val << chans[1] ) {} or waituntil( val << chans[2] ) {} or waituntil( val << chans[3] ) {} or else { i--; } } |
---|
22 | static inline void cons_wait_5( size_t & val, size_t & i ) { waituntil( val << chans[0] ) {} or waituntil( val << chans[1] ) {} or waituntil( val << chans[2] ) {} or waituntil( val << chans[3] ) {} or waituntil( val << chans[4] ) {} or else { i--; } } |
---|
23 | static inline void cons_wait_6( size_t & val, size_t & i ) { waituntil( val << chans[0] ) {} or waituntil( val << chans[1] ) {} or waituntil( val << chans[2] ) {} or waituntil( val << chans[3] ) {} or waituntil( val << chans[4] ) {} or waituntil( val << chans[5] ) {} or else { i--; } } |
---|
24 | static inline void cons_wait_7( size_t & val, size_t & i ) { waituntil( val << chans[0] ) {} or waituntil( val << chans[1] ) {} or waituntil( val << chans[2] ) {} or waituntil( val << chans[3] ) {} or waituntil( val << chans[4] ) {} or waituntil( val << chans[5] ) {} or waituntil( val << chans[6] ) {} or else { i--; } } |
---|
25 | static inline void cons_wait_8( size_t & val, size_t & i ) { waituntil( val << chans[0] ) {} or waituntil( val << chans[1] ) {} or waituntil( val << chans[2] ) {} or waituntil( val << chans[3] ) {} or waituntil( val << chans[4] ) {} or waituntil( val << chans[5] ) {} or waituntil( val << chans[6] ) {} or waituntil( val << chans[7] ) {} or else { i--; } } |
---|
26 | |
---|
27 | static inline void prods_wait_1( size_t val ) { waituntil( chans[0] << val ) {} or else {} } |
---|
28 | static inline void prods_wait_2( size_t val ) { waituntil( chans[0] << val ) {} or waituntil( chans[1] << val ) {} or else {} } |
---|
29 | static inline void prods_wait_3( size_t val ) { waituntil( chans[0] << val ) {} or waituntil( chans[1] << val ) {} or waituntil( chans[2] << val ) {} or else {} } |
---|
30 | static inline void prods_wait_4( size_t val ) { waituntil( chans[0] << val ) {} or waituntil( chans[1] << val ) {} or waituntil( chans[2] << val ) {} or waituntil( chans[3] << val ) {} or else {} } |
---|
31 | static inline void prods_wait_5( size_t val ) { waituntil( chans[0] << val ) {} or waituntil( chans[1] << val ) {} or waituntil( chans[2] << val ) {} or waituntil( chans[3] << val ) {} or waituntil( chans[4] << val ) {} or else {} } |
---|
32 | static inline void prods_wait_6( size_t val ) { waituntil( chans[0] << val ) {} or waituntil( chans[1] << val ) {} or waituntil( chans[2] << val ) {} or waituntil( chans[3] << val ) {} or waituntil( chans[4] << val ) {} or waituntil( chans[5] << val ) {} or else {} } |
---|
33 | static inline void prods_wait_7( size_t val ) { waituntil( chans[0] << val ) {} or waituntil( chans[1] << val ) {} or waituntil( chans[2] << val ) {} or waituntil( chans[3] << val ) {} or waituntil( chans[4] << val ) {} or waituntil( chans[5] << val ) {} or waituntil( chans[6] << val ) {} or else {} } |
---|
34 | static inline void prods_wait_8( size_t val ) { waituntil( chans[0] << val ) {} or waituntil( chans[1] << val ) {} or waituntil( chans[2] << val ) {} or waituntil( chans[3] << val ) {} or waituntil( chans[4] << val ) {} or waituntil( chans[5] << val ) {} or waituntil( chans[6] << val ) {} or waituntil( chans[7] << val ) {} or else {} } |
---|
35 | |
---|
36 | size_t globalTotal = 0; |
---|
37 | |
---|
38 | thread Consumer {}; |
---|
39 | void main( Consumer & this ) { |
---|
40 | size_t val, i = 0; |
---|
41 | try { |
---|
42 | for(;; i++ ) { |
---|
43 | FN_GLUE(cons_wait_, NUM_CHANS)(val, i); |
---|
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 | try { |
---|
52 | for( size_t i = 0;; i++ ) { |
---|
53 | FN_GLUE(prods_wait_, NUM_CHANS)(i); |
---|
54 | } |
---|
55 | } catch( channel_closed * e ) {} |
---|
56 | } |
---|
57 | |
---|
58 | int main( int argc, char * argv[] ) { |
---|
59 | switch ( argc ) { |
---|
60 | case 3: |
---|
61 | if ( strcmp( argv[2], "d" ) != 0 ) { // default ? |
---|
62 | ChannelSize = atoi( argv[2] ); |
---|
63 | } // if |
---|
64 | case 2: |
---|
65 | if ( strcmp( argv[1], "d" ) != 0 ) { // default ? |
---|
66 | Processors = atoi( argv[1] ); |
---|
67 | if ( Processors < 1 ) goto Usage; |
---|
68 | } // if |
---|
69 | case 1: // use defaults |
---|
70 | break; |
---|
71 | default: |
---|
72 | Usage: |
---|
73 | sout | "Usage: " | argv[0] |
---|
74 | | " [ processors (> 0) | 'd' (default " | Processors |
---|
75 | | ") ] [ channel size (>= 0) | 'd' (default " | ChannelSize |
---|
76 | | ") ]" ; |
---|
77 | exit( EXIT_FAILURE ); |
---|
78 | } // switch |
---|
79 | Producers = Processors / 2; |
---|
80 | Consumers = Processors / 2; |
---|
81 | |
---|
82 | processor p[Processors - 1]; |
---|
83 | |
---|
84 | chans = aalloc( NUM_CHANS ); |
---|
85 | for ( i; NUM_CHANS ) |
---|
86 | chans[i]{ ChannelSize }; |
---|
87 | |
---|
88 | { |
---|
89 | Producer p[Producers]; |
---|
90 | Consumer c[Consumers]; |
---|
91 | |
---|
92 | sleep(10`s); |
---|
93 | |
---|
94 | for ( i; NUM_CHANS ) |
---|
95 | close( chans[i] ); |
---|
96 | } |
---|
97 | adelete( chans ); |
---|
98 | printf("%zu\n", globalTotal); |
---|
99 | } |
---|