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 ) { waituntil( val << chans[0] ) {} } |
---|
19 | static inline void cons_wait_2( size_t & val ) { waituntil( val << chans[0] ) {} or waituntil( val << chans[1] ) {} } |
---|
20 | static inline void cons_wait_3( size_t & val ) { waituntil( val << chans[0] ) {} or waituntil( val << chans[1] ) {} or waituntil( val << chans[2] ) {} } |
---|
21 | static inline void cons_wait_4( size_t & val ) { waituntil( val << chans[0] ) {} or waituntil( val << chans[1] ) {} or waituntil( val << chans[2] ) {} or waituntil( val << chans[3] ) {} } |
---|
22 | static inline void cons_wait_5( size_t & val ) { waituntil( val << chans[0] ) {} or waituntil( val << chans[1] ) {} or waituntil( val << chans[2] ) {} or waituntil( val << chans[3] ) {} or waituntil( val << chans[4] ) {} } |
---|
23 | static inline void cons_wait_6( size_t & val ) { 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] ) {} } |
---|
24 | static inline void cons_wait_7( size_t & val ) { 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] ) {} } |
---|
25 | static inline void cons_wait_8( size_t & val ) { 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] ) {}} |
---|
26 | |
---|
27 | static inline void prods_wait_1( size_t val ) { waituntil( val >> chans[0] ) {} } |
---|
28 | static inline void prods_wait_2( size_t val ) { waituntil( val >> chans[0] ) {} or waituntil( val >> chans[1] ) {} } |
---|
29 | static inline void prods_wait_3( size_t val ) { waituntil( val >> chans[0] ) {} or waituntil( val >> chans[1] ) {} or waituntil( val >> chans[2] ) {} } |
---|
30 | static inline void prods_wait_4( size_t val ) { waituntil( val >> chans[0] ) {} or waituntil( val >> chans[1] ) {} or waituntil( val >> chans[2] ) {} or waituntil( val >> chans[3] ) {} } |
---|
31 | static inline void prods_wait_5( size_t val ) { waituntil( val >> chans[0] ) {} or waituntil( val >> chans[1] ) {} or waituntil( val >> chans[2] ) {} or waituntil( val >> chans[3] ) {} or waituntil( val >> chans[4] ) {} } |
---|
32 | static inline void prods_wait_6( size_t val ) { 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] ) {} } |
---|
33 | static inline void prods_wait_7( size_t val ) { 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] ) {} } |
---|
34 | static inline void prods_wait_8( size_t val ) { 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] ) {}} |
---|
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); |
---|
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 | } |
---|